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.
This commit is contained in:
ilammy 2015-04-18 14:42:33 +03:00
parent 37d808e470
commit 1f8c0088a7

View file

@ -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