diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2011-08-16 00:33:19 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2011-08-16 00:33:19 +0200 |
commit | 34a9c6bef7db450e0d8ea58dbc04740e3b261b12 (patch) | |
tree | 92fbf35538d24102245e74a94673ae62a0628fb1 /util.rkt | |
parent | 3aaa523f41df21b493133c4f3580c4cfed1611ff (diff) |
Implement HMAC.
Diffstat (limited to 'util.rkt')
-rw-r--r-- | util.rkt | 33 |
1 files changed, 32 insertions, 1 deletions
@@ -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))))) + |