Commits

Eric Roshan Eisner  committed 5d253ce Draft

factor out pad and readState from Sum

  • Participants
  • Parent commits de81809

Comments (0)

Files changed (1)

 	block(&d.state)
 }
 
+// readState reads the first block of data from the state to the buf
+func (d *keccak) readState() {
+	for i := 0; i < d.blocksize; i += 8 {
+		writeLane(d.buf[i:i+8], d.state[i/8])
+	}
+	d.n = 0
+}
+
 // Write uses d.buf to buffer calls to d.apply from arbitrary sized inputs.
 func (d *keccak) Write(b []byte) (nn int, err error) {
 	nn = len(b)
 	return
 }
 
-func (d0 *keccak) Sum(in []byte) []byte {
-	// Work on a copy to keep the original unmodified.
-	d := *d0
-
+func (d *keccak) pad() {
 	// Pad remaining bytes in block with 10*1 padding rule
 	d.buf[d.n] = 0x01
 	for i := d.n + 1; i < len(d.buf); i++ {
 	d.buf[d.blocksize-1] |= 0x80
 
 	d.apply(d.buf[:d.blocksize])
+}
 
-	// read state back into buf
-	for i := 0; i < d.blocksize; i += 8 {
-		writeLane(d.buf[i:i+8], d.state[i/8])
-	}
-
+func (d0 *keccak) Sum(in []byte) []byte {
+	// Work on a copy to keep the original unmodified.
+	d := *d0
+	d.pad()
+	d.readState()
 	return append(in, d.buf[:d.size]...)
 }