Skip to content

Commit

Permalink
crypto.sha: fix calculating hash values again for sha256 and sha512(fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shove70 committed Oct 29, 2023
1 parent aa28efc commit fe0430e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
10 changes: 9 additions & 1 deletion vlib/crypto/sha256/sha256.v
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion vlib/crypto/sha256/sha256_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand Down
10 changes: 9 additions & 1 deletion vlib/crypto/sha512/sha512.v
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion vlib/crypto/sha512/sha512_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit fe0430e

Please sign in to comment.