Advertisement
Guest User

md5 pre processing

a guest
May 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 3.40 KB | None | 0 0
  1. #lang racket
  2.  
  3.  
  4. (define (bytes-length-in-bits data)
  5.   (* 8 (bytes-length data)))
  6.  
  7.  
  8. (define (byte->bitstring x)
  9.   ;; returns a string containing the given byte in binary
  10.   ;; returns #f when the input isn't a byte
  11.     (and
  12.      (byte? x)
  13.      (~a #:width 8 #:pad-string "0" #:align 'right
  14.         (number->string x 2))))
  15.  
  16. (define (bytes->bitstring data)
  17.   ;; returns a the data in binary format
  18.   (foldl
  19.    (λ (b str)
  20.      (string-append str (byte->bitstring b)))
  21.    ""
  22.    (bytes->list data)))
  23.  
  24.  
  25. ;;PRE-PROCESSING
  26.  
  27. (define (pre-process-complete-padding data)
  28.   (letrec ([add-the-bit-1-and-7-zeroes
  29.             (λ (x)
  30.               (bytes-append
  31.                x
  32.                (bytes (arithmetic-shift 1 7))))]
  33.            [complete-padding
  34.             (λ (x)
  35.               (if (= (modulo (bytes-length-in-bits x) 512) 448)
  36.                   x
  37.                   (complete-padding (bytes-append x (bytes 0)))))])
  38.     (complete-padding (add-the-bit-1-and-7-zeroes data))))
  39.  
  40.  
  41. (define (pre-process-add-length padded-data original-length)
  42.   (define str (~a #:width 64 #:pad-string "0" #:align 'right
  43.                   (number->string original-length 2)))
  44.  
  45.   (foldl
  46.    (λ (b data)
  47.      (bytes-append data b))
  48.    padded-data
  49.    (for/list ([start (in-range 0 64 8)])
  50.      (define end (+ start 8))
  51.      (bytes (string->number (substring str start end) 2)))))
  52.  
  53.  
  54. (define (complete-pre-process data-bytes)
  55.   (define original-length (bytes-length data-bytes))
  56.  
  57.   (pre-process-add-length
  58.    (pre-process-complete-padding data-bytes)
  59.    original-length))
  60.  
  61.  
  62.  
  63. (define (md5 data)
  64.   (if (system-big-endian?)
  65.       "Your system needs to be little endian"
  66.       (let* ([s (list 7 12 17 22  7 12 17 22  7 12 17
  67.                       22  7 12 17 22 5  9 14 20  5  9
  68.                       14 20  5  9 14 20  5  9 14 20 4
  69.                       11 16 23  4 11 16 23  4 11 16 23
  70.                       4 11 16 23 6 10 15 21  6 10 15
  71.                       21  6 10 15 21  6 10 15 21)]
  72.              [K (list #xd76aa478 #xe8c7b756 #x242070db #xc1bdceee
  73.                       #xf57c0faf #x4787c62a #xa8304613 #xfd469501
  74.                       #x698098d8 #x8b44f7af #xffff5bb1 #x895cd7be
  75.                       #x6b901122 #xfd987193 #xa679438e #x49b40821
  76.                       #xf61e2562 #xc040b340 #x265e5a51 #xe9b6c7aa
  77.                       #xd62f105d #x02441453 #xd8a1e681 #xe7d3fbc8
  78.                       #x21e1cde6 #xc33707d6 #xf4d50d87 #x455a14ed
  79.                       #xa9e3e905 #xfcefa3f8 #x676f02d9 #x8d2a4c8a
  80.                       #xfffa3942 #x8771f681 #x6d9d6122 #xfde5380c
  81.                       #xa4beea44 #x4bdecfa9 #xf6bb4b60 #xbebfbc70
  82.                       #x289b7ec6 #xeaa127fa #xd4ef3085 #x04881d05
  83.                       #xd9d4d039 #xe6db99e5 #x1fa27cf8 #xc4ac5665
  84.                       #xf4292244 #x432aff97 #xab9423a7 #xfc93a039
  85.                       #x655b59c3 #x8f0ccc92 #xffeff47d #x85845dd1
  86.                       #x6fa87e4f #xfe2ce6e0 #xa3014314 #x4e0811a1
  87.                       #xf7537e82 #xbd3af235 #x2ad7d2bb #xeb86d391)]
  88.              [a0 #x67452301]
  89.              [b0 #xefcdab89]
  90.              [c0 #x98badcfe]
  91.              [d0 #x10325476]
  92.  
  93.              [data-bytes (if (string? data)
  94.                              (string->bytes/utf-8 data)
  95.                              data)]
  96.              [message (complete-pre-process data-bytes)]
  97.              )
  98.         (bytes->bitstring message))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement