crypto: make sum methods safe

pull/2166/head
joe-conigliaro 2019-09-29 23:44:52 +10:00 committed by Alexander Medvednikov
parent a1f0e940b7
commit 918edad525
4 changed files with 20 additions and 16 deletions

View File

@ -87,14 +87,15 @@ pub fn (d mut Digest) write(p_ []byte) ?int {
return nn return nn
} }
pub fn (d &Digest) sum(b_in mut []byte) []byte { pub fn (d &Digest) sum(b_in []byte) []byte {
// Make a copy of d so that caller can keep writing and summing. // Make a copy of d so that caller can keep writing and summing.
mut d0 := *d mut d0 := *d
hash := d0.checksum() hash := d0.checksum()
mut b_out := b_in.clone()
for b in hash { for b in hash {
b_in << b b_out << b
} }
return *b_in return b_out
} }
pub fn (d mut Digest) checksum() []byte { pub fn (d mut Digest) checksum() []byte {

View File

@ -91,14 +91,15 @@ pub fn (d mut Digest) write(p_ []byte) ?int {
return nn return nn
} }
pub fn (d &Digest) sum(b_in mut []byte) []byte { pub fn (d &Digest) sum(b_in []byte) []byte {
// Make a copy of d so that caller can keep writing and summing. // Make a copy of d so that caller can keep writing and summing.
mut d0 := *d mut d0 := *d
hash := d0.checksum() hash := d0.checksum()
mut b_out := b_in.clone()
for b in hash { for b in hash {
b_in << b b_out << b
} }
return *b_in return b_out
} }
fn (d mut Digest) checksum() []byte { fn (d mut Digest) checksum() []byte {

View File

@ -124,20 +124,21 @@ fn (d mut Digest) write(p_ []byte) ?int {
return nn return nn
} }
fn (d &Digest) sum(b_in mut []byte) []byte { fn (d &Digest) sum(b_in []byte) []byte {
// Make a copy of d so that caller can keep writing and summing. // Make a copy of d so that caller can keep writing and summing.
mut d0 := *d mut d0 := *d
hash := d0.checksum() hash := d0.checksum()
mut b_out := b_in.clone()
if d0.is224 { if d0.is224 {
for b in hash.left(Size224) { for b in hash.left(Size224) {
b_in << b b_out << b
} }
} else { } else {
for b in hash { for b in hash {
b_in << b b_out << b
} }
} }
return *b_in return b_out
} }
fn (d mut Digest) checksum() []byte { fn (d mut Digest) checksum() []byte {

View File

@ -178,29 +178,30 @@ fn (d mut Digest) write(p_ []byte) ?int {
return nn return nn
} }
fn (d mut Digest) sum(b_in mut []byte) []byte { fn (d mut Digest) sum(b_in []byte) []byte {
// Make a copy of d so that caller can keep writing and summing. // Make a copy of d so that caller can keep writing and summing.
mut d0 := *d mut d0 := *d
hash := d0.checksum() hash := d0.checksum()
mut b_out := b_in.clone()
switch d0.function { switch d0.function {
case crypto.Hash.SHA384: case crypto.Hash.SHA384:
for b in hash.left(Size384) { for b in hash.left(Size384) {
b_in << b b_out << b
} }
case crypto.Hash.SHA512_224: case crypto.Hash.SHA512_224:
for b in hash.left(Size224) { for b in hash.left(Size224) {
b_in << b b_out << b
} }
case crypto.Hash.SHA512_256: case crypto.Hash.SHA512_256:
for b in hash.left(Size256) { for b in hash.left(Size256) {
b_in << b b_out << b
} }
default: default:
for b in hash { for b in hash {
b_in << b b_out << b
} }
} }
return *b_in return b_out
} }
fn (d mut Digest) checksum() []byte { fn (d mut Digest) checksum() []byte {