aboutsummaryrefslogtreecommitdiff
path: root/module/crypto.scm
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2021-07-26 00:39:41 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2021-08-10 00:24:31 +0200
commit3382be7a49d37e00e07ab8a242236f755fb795cd (patch)
tree92c291549d761e4368090690b8981134cc1ee08a /module/crypto.scm
parentHTML calendar list now links to list off all their events. (diff)
downloadcalp-3382be7a49d37e00e07ab8a242236f755fb795cd.tar.gz
calp-3382be7a49d37e00e07ab8a242236f755fb795cd.tar.xz
Add OpenSSL:s SHA256
Diffstat (limited to 'module/crypto.scm')
-rw-r--r--module/crypto.scm34
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))))
+