(.module: [lux #* ["." host (#+ import:)] [data ["." binary (#+ Binary)] ["." text ["%" format (#+ format)]] [number ["." nat]]]]) ## TODO: Replace with pure-Lux implementations of these algorithms ## https://en.wikipedia.org/wiki/SHA-1#SHA-1_pseudocode ## https://en.wikipedia.org/wiki/MD5#Algorithm (import: java/lang/String) (import: java/security/MessageDigest (#static getInstance [java/lang/String] java/security/MessageDigest) (digest [[byte]] [byte])) (type: #export Hash Binary) (template [ ] [(def: #export ( value) (-> Binary Hash) (|> (java/security/MessageDigest::getInstance []) (java/security/MessageDigest::digest [value])))] [sha1 "SHA-1"] [md5 "MD5"] ) (def: #export representation (-> Hash Text) (binary.fold (function (_ byte representation) (let [hex (:: nat.hex encode byte) hex (case (text.size hex) 1 (format "0" hex) _ hex)] (format representation hex))) ""))