Login
7 branches 0 tags
Ben (X13/Arch) Simplified things a little 0643405 9 days ago 1260 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 (and (not= (:type-name data) :buffer)
                 (not= (:type-name data) :string))
        (exception :type-error "Can only hash buffers or strings"))
      (dotimes (i (:length data))
        (set! a (mod/int (add/int a (ref data 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")))

(defn main (args)
      :export
      (when (not (car args))
        (efmtln "Usage: (...FILES)")
        (exit 1))
      (doseq (file args)
             (if (file/dir? file)
                 (pfmtln "nujel/adler32: {file}: Is a directory")
                 (if (file/file? file)
                     (pfmtln "{:08X} {file} ADLER32" (hash (slurp/buffer file)))
                     (pfmtln "nujel/adler32: {file}: No such file or directory")))))