Login
7 branches 0 tags
Benjamin Vincent Schulenburg Fixed standalone module loader 1170824 3 years ago 868 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

[export hash [defn hash [data]
                   [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 [buffer/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"]]]