From 1f8c0088a70a76167b0a5a8a8818293b3ab2692b Mon Sep 17 00:00:00 2001 From: ilammy Date: Sat, 18 Apr 2015 14:42:33 +0300 Subject: [PATCH] chibi.crypto: fix formatting bug in portable SHA-2 We can't use 'integer->hex-string' alone to print out SHA-224/256 digest because it rightly converts #x00001234 into "1234", while we need to keep the padding zero nibbles and get "00001234". 'hex' got renamed into 'hex32' because SHA-512 will need some different 'hex64' which returns 16-character-long strings. --- lib/chibi/crypto/sha2.scm | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/chibi/crypto/sha2.scm b/lib/chibi/crypto/sha2.scm index 2378b0e6..fa5af55f 100644 --- a/lib/chibi/crypto/sha2.scm +++ b/lib/chibi/crypto/sha2.scm @@ -40,7 +40,11 @@ (u32 (arithmetic-shift n (- 32 k))) (arithmetic-shift n (- k)))) -(define hex integer->hex-string) +(define (hex32 num) + (let ((res (make-string 8 #\0)) + (num (integer->hex-string num))) + (string-copy! res (- 8 (string-length num)) num) + res)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -152,8 +156,8 @@ (if (>= n 56) (chunk (+ i n) 0 a b c d e f g h) (string-append - (hex a) (hex b) (hex c) (hex d) - (hex e) (hex f) (hex g) (if full? (hex h) "")))) + (hex32 a) (hex32 b) (hex32 c) (hex32 d) + (hex32 e) (hex32 f) (hex32 g) (if full? (hex32 h) "")))) (else (chunk (+ i 64) pad a b c d e f g h))))) (else