diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2021-07-26 00:39:41 +0200 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2021-08-10 00:24:31 +0200 |
commit | 3382be7a49d37e00e07ab8a242236f755fb795cd (patch) | |
tree | 92c291549d761e4368090690b8981134cc1ee08a /module/crypto.scm | |
parent | HTML calendar list now links to list off all their events. (diff) | |
download | calp-3382be7a49d37e00e07ab8a242236f755fb795cd.tar.gz calp-3382be7a49d37e00e07ab8a242236f755fb795cd.tar.xz |
Add OpenSSL:s SHA256
Diffstat (limited to 'module/crypto.scm')
-rw-r--r-- | module/crypto.scm | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/module/crypto.scm b/module/crypto.scm new file mode 100644 index 00000000..0b18f240 --- /dev/null +++ b/module/crypto.scm @@ -0,0 +1,34 @@ +(define-module (crypto) + :use-module (rnrs bytevectors) + :use-module (system foreign) + :export (sha256 checksum->string)) + +(define-once libcrypto (dynamic-link "libcrypto")) + +(define SHA_DIGEST_LENGTH 20) +(define SHA224_DIGEST_LENGTH 28) +(define SHA256_DIGEST_LENGTH 32) +(define SHA384_DIGEST_LENGTH 48) +(define SHA512_DIGEST_LENGTH 64) + +(define SHA256 + ((@ (system foreign) pointer->procedure) + '* (dynamic-func "SHA256" libcrypto) + `(* ,(@ (system foreign) size_t) *))) + +(define (sha256 msg) + (define md (make-bytevector SHA256_DIGEST_LENGTH)) + (define bv (string->utf8 msg)) + (SHA256 ((@ (system foreign) bytevector->pointer) bv) + (bytevector-length bv) + ((@ (system foreign) bytevector->pointer) md)) + md) + +(define (checksum->string md) + (string-concatenate + (map (lambda (byte) + (format #f "~x~x" + (logand #xF (ash byte -4)) + (logand #xF byte))) + (bytevector->u8-list md)))) + |