cryptopals/cryptopals.lisp
2020-11-08 23:44:19 -06:00

26 lines
729 B
Common Lisp

(ql:quickload :cl-base64)
(ql:quickload :iterate)
(use-package :iterate)
(defun hex-to-dec (hex-chr)
(cond
((char<= #\0 hex-chr #\9)
(- (char-code hex-chr) (char-code #\0)))
((char<= #\a hex-chr #\f)
(+ (- (char-code hex-chr) (char-code #\a)) 10))
((char<= #\A hex-chr #\F)
(+ (- (char-code hex-chr) (char-code #\A)) 10))))
(defun hex-to-bytes (hex-str)
(iter
(for i below (length hex-str) by 2)
(let ((c1 (hex-to-dec (aref hex-str i)))
(c2 (hex-to-dec (aref hex-str (1+ i)))))
(collect (+ (* 16 c1) c2) result-type (vector (unsigned-byte 8))))))
;; set 1 challenge 1
(defun hex-to-base64 (hex-str)
(cl-base64:usb8-array-to-base64-string (hex-to-bytes hex-str)))