diff --git a/vlib/crypto/sha1/sha1.v b/vlib/crypto/sha1/sha1.v index 85cda4fbb0..061b077a82 100644 --- a/vlib/crypto/sha1/sha1.v +++ b/vlib/crypto/sha1/sha1.v @@ -64,6 +64,14 @@ pub fn (mut d Digest) reset() { d.len = 0 } +fn (d &Digest) clone() &Digest { + return &Digest{ + ...d + h: d.h.clone() + x: d.x.clone() + } +} + // new returns a new Digest (implementing hash.Hash) computing the SHA1 checksum. pub fn new() &Digest { mut d := &Digest{} @@ -110,7 +118,7 @@ pub fn (mut d Digest) write(p_ []u8) !int { // sum returns a copy of the generated sum of the bytes in `b_in`. pub fn (d &Digest) sum(b_in []u8) []u8 { // Make a copy of d so that caller can keep writing and summing. - mut d0 := *d + mut d0 := d.clone() hash := d0.checksum() mut b_out := b_in.clone() for b in hash { diff --git a/vlib/crypto/sha1/sha1_test.v b/vlib/crypto/sha1/sha1_test.v index b3b7fb0de4..96c8542ed4 100644 --- a/vlib/crypto/sha1/sha1_test.v +++ b/vlib/crypto/sha1/sha1_test.v @@ -11,7 +11,9 @@ fn test_crypto_sha1_writer() { mut digest := sha1.new() digest.write('This is a'.bytes()) or { assert false } digest.write(' sha1 checksum.'.bytes()) or { assert false } - sum := digest.sum([]) + mut sum := digest.sum([]) + assert sum.hex() == 'e100d74442faa5dcd59463b808983c810a8eb5a1' + sum = digest.sum([]) assert sum.hex() == 'e100d74442faa5dcd59463b808983c810a8eb5a1' } diff --git a/vlib/crypto/sha256/sha256.v b/vlib/crypto/sha256/sha256.v index 81898f451a..82c19dd34e 100644 --- a/vlib/crypto/sha256/sha256.v +++ b/vlib/crypto/sha256/sha256.v @@ -91,6 +91,14 @@ pub fn (mut d Digest) reset() { d.len = 0 } +fn (d &Digest) clone() &Digest { + return &Digest{ + ...d + h: d.h.clone() + x: d.x.clone() + } +} + // new returns a new Digest (implementing hash.Hash) computing the SHA256 checksum. pub fn new() &Digest { mut d := &Digest{} @@ -144,7 +152,7 @@ pub fn (mut d Digest) write(p_ []u8) !int { // sum returns the SHA256 or SHA224 checksum of digest with the data. pub fn (d &Digest) sum(b_in []u8) []u8 { // Make a copy of d so that caller can keep writing and summing. - mut d0 := *d + mut d0 := d.clone() hash := d0.checksum() mut b_out := b_in.clone() if d0.is224 { diff --git a/vlib/crypto/sha256/sha256_test.v b/vlib/crypto/sha256/sha256_test.v index c0a7ac77ef..6b1f8c959a 100644 --- a/vlib/crypto/sha256/sha256_test.v +++ b/vlib/crypto/sha256/sha256_test.v @@ -11,7 +11,9 @@ fn test_crypto_sha256_writer() { mut digest := sha256.new() digest.write('This is a'.bytes()) or { assert false } digest.write(' sha256 checksum.'.bytes()) or { assert false } - sum := digest.sum([]) + mut sum := digest.sum([]) + assert sum.hex() == 'dc7163299659529eae29683eb1ffec50d6c8fc7275ecb10c145fde0e125b8727' + sum = digest.sum([]) assert sum.hex() == 'dc7163299659529eae29683eb1ffec50d6c8fc7275ecb10c145fde0e125b8727' } diff --git a/vlib/crypto/sha512/sha512.v b/vlib/crypto/sha512/sha512.v index b8c66f7c1d..208e738605 100644 --- a/vlib/crypto/sha512/sha512.v +++ b/vlib/crypto/sha512/sha512.v @@ -136,6 +136,14 @@ pub fn (mut d Digest) reset() { d.len = 0 } +fn (d &Digest) clone() &Digest { + return &Digest{ + ...d + h: d.h.clone() + x: d.x.clone() + } +} + // internal fn new_digest(hash crypto.Hash) &Digest { mut d := &Digest{ @@ -203,7 +211,7 @@ pub fn (mut d Digest) write(p_ []u8) !int { // sum returns the SHA512 or SHA384 checksum of digest with the data bytes in `b_in` pub fn (d &Digest) sum(b_in []u8) []u8 { // Make a copy of d so that caller can keep writing and summing. - mut d0 := *d + mut d0 := d.clone() hash := d0.checksum() mut b_out := b_in.clone() match d0.function { diff --git a/vlib/crypto/sha512/sha512_test.v b/vlib/crypto/sha512/sha512_test.v index 598451688b..69af923fb4 100644 --- a/vlib/crypto/sha512/sha512_test.v +++ b/vlib/crypto/sha512/sha512_test.v @@ -11,8 +11,12 @@ fn test_crypto_sha512_writer() { mut digest := sha512.new_digest(.sha512) digest.write('This is a'.bytes()) or { assert false } digest.write(' sha512 checksum.'.bytes()) or { assert false } - sum := digest.checksum() + mut sum := digest.checksum() assert sum.hex() == '4143e55fcba7e39b20f62a1368e5eb28f64a8859458886117ac66027832e0f9f5263daec688c439d2d0fa07059334668d39e59543039703dbb7e03ec9da7f8d7' + sum = digest.sum([]) + assert sum.hex() == '64922a82583d4e364010e574ca5ce6a15686e0d268bd41ac1003dda924356d3552e276b7baedacd85a6884e744943a92d931cebca4738510b4568c5fb5a49723' + sum = digest.sum([]) + assert sum.hex() == '64922a82583d4e364010e574ca5ce6a15686e0d268bd41ac1003dda924356d3552e276b7baedacd85a6884e744943a92d931cebca4738510b4568c5fb5a49723' } fn test_crypto_sha512_writer_reset() {