Login
7 branches 0 tags
Ben (X13/Arch) Added verbose flag to (assemble*) 70d148f 2 years ago 960 Commits
nujel / stdlib_modules / crypto / adler32.nuj
;;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;;; This project uses the MIT license, a copy should be included under /LICENSE
;;;
;;; Simple implementation of adler32

(defn hash (data)
      :export
      (def a 1)
      (def b 0)
      (when (string? data) (set! data (string->buffer data)))
      (when (not= (type-of data) :buffer) (exception :type-error "Can only hash buffers or strings"))
      (def v (buffer/u8* data))
      (dotimes (i (buffer/length data))
        (set! a (mod/int (add/int a (ref v i)) 65521))
        (set! b (mod/int (add/int a b) 65521)))
      (bit-or a (bit-shift-left b 16)))

(deftest "00620062" (fmt "{:08X}" (crypto/adler32/hash "a")))
(deftest "0F9D02BC" (fmt "{:08X}" (crypto/adler32/hash "asdQWE123")))
(deftest "796B110D" (fmt "{:08X}" (crypto/adler32/hash "DiesIstEinTestDerNujelAdler32Implementierung")))