From 34a9c6bef7db450e0d8ea58dbc04740e3b261b12 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Tue, 16 Aug 2011 00:33:19 +0200 Subject: Implement HMAC. --- util.rkt | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'util.rkt') diff --git a/util.rkt b/util.rkt index 5c1f2d3..43dbe01 100644 --- a/util.rkt +++ b/util.rkt @@ -17,7 +17,13 @@ ;;;----------------------------------------------------------------------------- ;;; -(provide integer->bytes) +(provide integer->bytes + integer->bytes/size + bytes->integer + pad-bytes + Justification) + +(define-type Justification (U 'left 'right)) (define: (integer->bytes [x : Exact-Nonnegative-Integer]) : Bytes (let: loop : Bytes @@ -27,3 +33,28 @@ (list->bytes acc) (loop (cons (bitwise-and #xff x) acc) (arithmetic-shift x -8))))) + +(define: (integer->bytes/size [x : Exact-Nonnegative-Integer] + [size : Exact-Nonnegative-Integer]) + : Bytes + (pad-bytes (integer->bytes x) size #x0 'right)) + +(define: (bytes->integer [b : Bytes]) : Exact-Nonnegative-Integer + (for/fold: ([n : Exact-Nonnegative-Integer 0]) + ([byte : Byte (in-bytes b)]) + (bitwise-ior (arithmetic-shift n 8) + byte))) + +(define: (pad-bytes [b : Bytes] + [s : Exact-Nonnegative-Integer] + [fill : Byte] + [justify : Justification]) + : Bytes + (if (>= (bytes-length b) s) + b + (let* ([delta (- s (bytes-length b))] + [padding (make-bytes delta fill)]) + (if (eq? justify 'left) + (bytes-append b padding) + (bytes-append padding b))))) + -- cgit v1.2.3