-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cl-nixbase32 initial iteration #1
Comments
go-nix/nixbase32/nixbase32.go at dev · zombiezen/go-nix |
;; Define the alphabet used in Nix base32 encoding
(defparameter *alphabet* "0123456789abcdfghijklmnpqrsvwxyz")
;; Function to decode a nixbase32 string into bytes
(defun decode-string (s)
(let ((dst (make-array (decoded-len (length s)))))
(multiple-value-bind (n err)
(decode dst (coerce s 'list))
(subseq dst 0 n))))
;; Function to decode nixbase32 encoded data
(defun decode (dst src)
(let ((max-dst-size (decoded-len (length src))))
(loop for n from 0 below (length src)
for b = (* n 5)
for i = (floor b 8)
for j = (mod b 8)
for c = (elt (reverse src) n)
for digit = (position c *alphabet*)
do (if (null digit)
(return-from decode (values i (error "Decode base32: character not in Nix alphabet")))
(setf (aref dst i) (logior (aref dst i) (ash digit j)))
(let ((carry (ash digit (- 8 j))))
(if (< (1+ i) max-dst-size)
(setf (aref dst (1+ i)) (logior (aref dst (1+ i)) carry))
(if (plusp carry)
(return-from decode (values i (error "Decode base32: non-zero padding"))))))))))
;; Function to validate a nixbase32 encoded string
(defun validate-string (src)
(let ((max-dst-size (decoded-len (length src))))
(loop for n from 0 below (length src)
for b = (* n 5)
for i = (floor b 8)
for j = (mod b 8)
for c = (elt (reverse src) n)
for digit = (position c *alphabet*)
do (if (null digit)
(return-from validate-string (error "Decode base32: character not in Nix alphabet"))
(when (>= (1+ i) max-dst-size)
(let ((carry (ash digit (- 8 j))))
(when (plusp carry)
(return-from validate-string (error "Decode base32: non-zero padding")))))))))
;; Function to get the length in bytes of the base32 encoding
(defun encoded-len (n)
(ceiling (* (+ (* n 8) 4) 5)))
;; Function to get the length in bytes of the decoded data
(defun decoded-len (n)
(floor (* n 5) 8))
;; Function to encode data using nixbase32
(defun encode (dst src)
(let ((n (encoded-len (length src))))
(dotimes (n (- n 1) dst)
(let* ((b (* n 5))
(i (floor b 8))
(j (mod b 8))
(c (ash (aref src i) (- j))))
(when (< (1+ i) (length src))
(setf c (logior c (ash (aref src (1+ i)) (- 8 j)))))
(push (aref *alphabet* (logand c #x1f)) dst)))))
;; Function to encode data to nixbase32 string
(defun encode-to-string (src)
(let ((n (encoded-len (length src)))
(result ""))
(dotimes (n (- n 1) result)
(let* ((b (* n 5))
(i (floor b 8))
(j (mod b 8))
(c (ash (aref src i) (- j))))
(when (< (1+ i) (length src))
(setf c (logior c (ash (aref src (1+ i)) (- 8 j)))))
(setf result (concatenate 'string (string (aref *alphabet* (logand c #x1f))) result))))))
;; Predicate to check if a byte is part of the nixbase32 alphabet
(defun is-nixbase32-char-p (c)
(or (and (char<= #\0 c #\9))
(and (char<= #\a c #\z)
(not (member c '(#\e #\o #\u #\t)))))) |
nix-prefetch-url - Nix Reference Manual |
|
RFC 4648 - The Base16, Base32, and Base64 Data Encodings |
Document that base32 hashes are not the base32 everyone expects · Issue #4354 · NixOS/nix |
No description provided.
The text was updated successfully, but these errors were encountered: