Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

slim down the COPY-DIGEST code for SHA256 and SHA512

  • Loading branch information...
commit 19ea57786b634ee6ee95f41732d00cea39956b56 1 parent 6c1373b
@froydnj authored
Showing with 22 additions and 50 deletions.
  1. +11 −25 src/digests/sha256.lisp
  2. +11 −25 src/digests/sha512.lisp
View
36 src/digests/sha256.lisp
@@ -110,7 +110,6 @@
(defstruct (sha256
(:constructor %make-sha256-digest nil)
- (:constructor %make-sha256-state (regs amount block buffer buffer-index))
(:copier nil)
(:include mdx))
(regs (initial-sha256-regs) :type sha256-regs :read-only t)
@@ -120,7 +119,6 @@
(defstruct (sha224
(:include sha256)
(:constructor %make-sha224-digest (&aux (regs (initial-sha224-regs))))
- (:constructor %make-sha224-state (regs amount block buffer buffer-index))
(:copier nil))
;; No slots.
)
@@ -139,31 +137,19 @@
(sha224-buffer-index state) 0)
state)
-(defun copy-sha256 (state copy constructor)
- (declare (type function constructor))
- (declare (type (or cl:null sha256) copy))
- (cond
- (copy
- (replace (sha256-regs copy) (sha256-regs state))
- (replace (sha256-buffer copy) (sha256-buffer state))
- (setf (sha256-amount copy) (sha256-amount state)
- (sha256-buffer-index copy) (sha256-buffer-index state))
- copy)
- (t
- (funcall constructor
- (copy-seq (sha256-regs state))
- (sha256-amount state)
- (copy-seq (sha256-block state))
- (copy-seq (sha256-buffer state))
- (sha256-buffer-index state)))))
-
(defmethod copy-digest ((state sha256) &optional copy)
(declare (type (or cl:null sha256) copy))
- (copy-sha256 state copy #'%make-sha256-state))
-
-(defmethod copy-digest ((state sha224) &optional copy)
- (declare (type (or cl:null sha224) copy))
- (copy-sha256 state copy #'%make-sha224-state))
+ (let ((copy (if copy
+ copy
+ (etypecase state
+ (sha256 (%make-sha256-digest))
+ (sha224 (%make-sha224-digest))))))
+ (declare (type sha256 copy))
+ (replace (sha256-regs copy) (sha256-regs state))
+ (replace (sha256-buffer copy) (sha256-buffer state))
+ (setf (sha256-amount copy) (sha256-amount state)
+ (sha256-buffer-index copy) (sha256-buffer-index state))
+ copy))
(define-digest-updater sha256
(flet ((compress (state sequence offset)
View
36 src/digests/sha512.lisp
@@ -103,7 +103,6 @@
(defstruct (sha512
(:constructor %make-sha512-digest
(&aux (buffer (make-array 128 :element-type '(unsigned-byte 8)))))
- (:constructor %make-sha512-state (regs amount block buffer buffer-index))
(:copier nil)
(:include mdx))
(regs (initial-sha512-regs) :type sha512-regs :read-only t)
@@ -115,7 +114,6 @@
(:constructor %make-sha384-digest
(&aux (regs (initial-sha384-regs))
(buffer (make-array 128 :element-type '(unsigned-byte 8)))))
- (:constructor %make-sha384-state (regs amount block buffer buffer-index))
(:copier nil))
;; No slots.
)
@@ -134,31 +132,19 @@
(sha384-buffer-index state) 0)
state)
-(defun copy-sha512 (state copy constructor)
- (declare (type function constructor))
- (declare (type (or cl:null sha512) copy))
- (cond
- (copy
- (replace (sha512-regs copy) (sha512-regs state))
- (replace (sha512-buffer copy) (sha512-buffer state))
- (setf (sha512-amount copy) (sha512-amount state)
- (sha512-buffer-index copy) (sha512-buffer-index state))
- copy)
- (t
- (funcall constructor
- (copy-seq (sha512-regs state))
- (sha512-amount state)
- (copy-seq (sha512-block state))
- (copy-seq (sha512-buffer state))
- (sha512-buffer-index state)))))
-
(defmethod copy-digest ((state sha512) &optional copy)
(declare (type (or cl:null sha512) copy))
- (copy-sha512 state copy #'%make-sha512-state))
-
-(defmethod copy-digest ((state sha384) &optional copy)
- (declare (type (or cl:null sha384) copy))
- (copy-sha512 state copy #'%make-sha384-state))
+ (let ((copy (if copy
+ copy
+ (etypecase state
+ (sha512 (%make-sha512-digest))
+ (sha384 (%make-sha384-digest))))))
+ (declare (type sha512 copy))
+ (replace (sha512-regs copy) (sha512-regs state))
+ (replace (sha512-buffer copy) (sha512-buffer state))
+ (setf (sha512-amount copy) (sha512-amount state)
+ (sha512-buffer-index copy) (sha512-buffer-index state))
+ copy))
(define-digest-updater sha512
(flet ((compress (state sequence offset)
Please sign in to comment.
Something went wrong with that request. Please try again.