Skip to content

Commit

Permalink
[#1] Cl-nixbase32 initial iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
kisp committed May 30, 2024
1 parent 425e8f7 commit 5cb0477
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cl-nixbase32-test.asd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:components ((:module "test"
:components ((:file "package")
(:file "test" :depends-on ("package")))))
:depends-on (:cl-nixbase32 :myam :alexandria))
:depends-on (:cl-nixbase32 :myam :alexandria :ironclad))

(defmethod perform ((op test-op)
(system (eql (find-system :cl-nixbase32-test))))
Expand Down
2 changes: 1 addition & 1 deletion cl-nixbase32.asd
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
(:file "package")
(:file "cl-nixbase32" :depends-on ("package"))
)
:depends-on (:alexandria)
:depends-on (:alexandria :arrow-macros)
:in-order-to ((test-op (test-op :cl-nixbase32-test))))
37 changes: 37 additions & 0 deletions cl-nixbase32.lisp
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
;;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp; Coding:utf-8 -*-

(in-package :cl-nixbase32)

(defvar *alphabet* "0123456789abcdfghijklmnpqrsvwxyz")

(defun byte-to-bits (byte)
"Convert a byte to a list of its bits."
(loop for i from 7 downto 0
collect (ldb (byte 1 i) byte)))

(defun bits-to-int (bits)
"Convert a list of bits to an integer."
(reduce (lambda (acc bit) (+ (* 2 acc) bit)) bits))

(defun pad-in-front (list)
(ecase (mod (length list) 5)
(0 list)
(1 (append '(0 0 0 0) list))))

(defun group-by-5 (list)
(when list
(cons (subseq list 0 5)
(group-by-5 (nthcdr 5 list)))))

(defun lookup-alphabet (list)
(map 'string (lambda (x) (aref *alphabet* x)) list))

(defun bytes-to-nixbase32 (vector)
;; Maybe I'll implement this more efficiently at some point. For
;; now, this works.
(arrow-macros:->> vector
(map 'list #'identity)
reverse
(map 'list #'byte-to-bits)
alexandria:flatten
pad-in-front
group-by-5
(mapcar #'bits-to-int)
lookup-alphabet))
2 changes: 1 addition & 1 deletion package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

(defpackage :cl-nixbase32
(:use :common-lisp :alexandria)
(:export))
(:export #:bytes-to-nixbase32))
29 changes: 27 additions & 2 deletions test/test.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,30 @@

(defsuite* :cl-nixbase32-test)

(deftest dummy
(is (= 1 1)))
(deftest bytes-to-nixbase32.1
(is (equal "04000000000000000000000000000000"
(bytes-to-nixbase32
(ironclad:hex-string-to-byte-array
"0000000000000000000000000000000000000001"))))
(is (equal "08000000000000000000000000000000"
(bytes-to-nixbase32
(ironclad:hex-string-to-byte-array
"0000000000000000000000000000000000000002"))))
(is (equal "nvd61k9nalji1zl9rrdfmsmvyyjqpzg4"
(bytes-to-nixbase32
(ironclad:hex-string-to-byte-array
"e4fd8ba5f7bbeaea5ace89fe10255536cd60dab6")))))

(deftest bytes-to-nixbase32.2
(is (equal "0080000000000000000000000000000000000000000000000000"
(bytes-to-nixbase32
(ironclad:hex-string-to-byte-array
"0000000000000000000000000000000000000000000000000000000000000001"))))
(is (equal "00h0000000000000000000000000000000000000000000000000"
(bytes-to-nixbase32
(ironclad:hex-string-to-byte-array
"0000000000000000000000000000000000000000000000000000000000000002"))))
(is (equal "1ak0qjw1r1idnv0847fvyj3jsd3m7z8674c41p76hr36d98n9wk2"
(bytes-to-nixbase32
(ironclad:hex-string-to-byte-array
"62f264516a666468ce0d849163d03f75342d87f4db1d82c0b62d861cb8c460aa")))))

0 comments on commit 5cb0477

Please sign in to comment.