Skip to content

Commit

Permalink
crypto: eliminate deprecating notifications from internal calls to ch…
Browse files Browse the repository at this point in the history
…ecksum() (#19707)
  • Loading branch information
shove70 authored Oct 30, 2023
1 parent 6609223 commit 880ce7a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
20 changes: 13 additions & 7 deletions vlib/crypto/md5/md5.v
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,17 @@ pub fn (mut d Digest) write(p_ []u8) !int {
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.clone()
hash := d0.checksum()
hash := d0.checksum_internal()
mut b_out := b_in.clone()
for b in hash {
b_out << b
}
return b_out
}

// checksum returns the byte checksum of the `Digest`,
// it is an internal method and is not recommended because its results are not idempotent.
[deprecated: 'checksum() will be changed to a private method, use sum() instead']
[deprecated_after: '2024-04-30']
pub fn (mut d Digest) checksum() []u8 {
// TODO:
// When the deprecated "checksum()" is finally removed, restore this function name as: "checksum()"
fn (mut d Digest) checksum_internal() []u8 {
// Append 0x80 to the end of the message and then append zeros
// until the length is a multiple of 56 bytes. Finally append
// 8 bytes representing the message length in bits.
Expand All @@ -149,11 +147,19 @@ pub fn (mut d Digest) checksum() []u8 {
return digest
}

// checksum returns the byte checksum of the `Digest`,
// it is an internal method and is not recommended because its results are not idempotent.
[deprecated: 'checksum() will be changed to a private method, use sum() instead']
[deprecated_after: '2024-04-30']
pub fn (mut d Digest) checksum() []u8 {
return d.checksum_internal()
}

// sum returns the MD5 checksum of the data.
pub fn sum(data []u8) []u8 {
mut d := new()
d.write(data) or { panic(err) }
return d.checksum()
return d.checksum_internal()
}

fn block(mut dig Digest, p []u8) {
Expand Down
20 changes: 13 additions & 7 deletions vlib/crypto/sha1/sha1.v
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,17 @@ pub fn (mut d Digest) write(p_ []u8) !int {
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.clone()
hash := d0.checksum()
hash := d0.checksum_internal()
mut b_out := b_in.clone()
for b in hash {
b_out << b
}
return b_out
}

// checksum returns the current byte checksum of the `Digest`,
// it is an internal method and is not recommended because its results are not idempotent.
[deprecated: 'checksum() will be changed to a private method, use sum() instead']
[deprecated_after: '2024-04-30']
pub fn (mut d Digest) checksum() []u8 {
// TODO:
// When the deprecated "checksum()" is finally removed, restore this function name as: "checksum()"
fn (mut d Digest) checksum_internal() []u8 {
mut len := d.len
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
mut tmp := []u8{len: (64)}
Expand All @@ -154,11 +152,19 @@ pub fn (mut d Digest) checksum() []u8 {
return digest
}

// checksum returns the current byte checksum of the `Digest`,
// it is an internal method and is not recommended because its results are not idempotent.
[deprecated: 'checksum() will be changed to a private method, use sum() instead']
[deprecated_after: '2024-04-30']
pub fn (mut d Digest) checksum() []u8 {
return d.checksum_internal()
}

// sum returns the SHA-1 checksum of the bytes passed in `data`.
pub fn sum(data []u8) []u8 {
mut d := new()
d.write(data) or { panic(err) }
return d.checksum()
return d.checksum_internal()
}

fn block(mut dig Digest, p []u8) {
Expand Down
22 changes: 14 additions & 8 deletions vlib/crypto/sha256/sha256.v
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn (mut d Digest) write(p_ []u8) !int {
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.clone()
hash := d0.checksum()
hash := d0.checksum_internal()
mut b_out := b_in.clone()
if d0.is224 {
for b in hash[..sha256.size224] {
Expand All @@ -167,11 +167,9 @@ pub fn (d &Digest) sum(b_in []u8) []u8 {
return b_out
}

// checksum returns the current byte checksum of the Digest,
// it is an internal method and is not recommended because its results are not idempotent.
[deprecated: 'checksum() will be changed to a private method, use sum() instead']
[deprecated_after: '2024-04-30']
pub fn (mut d Digest) checksum() []u8 {
// TODO:
// When the deprecated "checksum()" is finally removed, restore this function name as: "checksum()"
fn (mut d Digest) checksum_internal() []u8 {
mut len := d.len
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
mut tmp := []u8{len: (64)}
Expand Down Expand Up @@ -202,6 +200,14 @@ pub fn (mut d Digest) checksum() []u8 {
return digest
}

// checksum returns the current byte checksum of the Digest,
// it is an internal method and is not recommended because its results are not idempotent.
[deprecated: 'checksum() will be changed to a private method, use sum() instead']
[deprecated_after: '2024-04-30']
pub fn (mut d Digest) checksum() []u8 {
return d.checksum_internal()
}

// sum returns the SHA256 checksum of the bytes in `data`.
// Example: assert sha256.sum('V'.bytes()).len > 0 == true
pub fn sum(data []u8) []u8 {
Expand All @@ -212,14 +218,14 @@ pub fn sum(data []u8) []u8 {
pub fn sum256(data []u8) []u8 {
mut d := new()
d.write(data) or { panic(err) }
return d.checksum()
return d.checksum_internal()
}

// sum224 returns the SHA224 checksum of the data.
pub fn sum224(data []u8) []u8 {
mut d := new224()
d.write(data) or { panic(err) }
sum := d.checksum()
sum := d.checksum_internal()
mut sum224 := []u8{len: sha256.size224}
copy(mut sum224, sum[..sha256.size224])
return sum224
Expand Down
26 changes: 16 additions & 10 deletions vlib/crypto/sha512/sha512.v
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub fn (mut d Digest) write(p_ []u8) !int {
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.clone()
hash := d0.checksum()
hash := d0.checksum_internal()
mut b_out := b_in.clone()
match d0.function {
.sha384 {
Expand All @@ -239,11 +239,9 @@ pub fn (d &Digest) sum(b_in []u8) []u8 {
return b_out
}

// checksum returns the current byte checksum of the Digest,
// it is an internal method and is not recommended because its results are not idempotent.
[deprecated: 'checksum() will be changed to a private method, use sum() instead']
[deprecated_after: '2024-04-30']
pub fn (mut d Digest) checksum() []u8 {
// TODO:
// When the deprecated "checksum()" is finally removed, restore this function name as: "checksum()"
fn (mut d Digest) checksum_internal() []u8 {
// Padding. Add a 1 bit and 0 bits until 112 bytes mod 128.
mut len := d.len
mut tmp := []u8{len: (128)}
Expand Down Expand Up @@ -275,18 +273,26 @@ pub fn (mut d Digest) checksum() []u8 {
return digest
}

// checksum returns the current byte checksum of the Digest,
// it is an internal method and is not recommended because its results are not idempotent.
[deprecated: 'checksum() will be changed to a private method, use sum() instead']
[deprecated_after: '2024-04-30']
pub fn (mut d Digest) checksum() []u8 {
return d.checksum_internal()
}

// sum512 returns the SHA512 checksum of the data.
pub fn sum512(data []u8) []u8 {
mut d := new_digest(.sha512)
d.write(data) or { panic(err) }
return d.checksum()
return d.checksum_internal()
}

// sum384 returns the SHA384 checksum of the data.
pub fn sum384(data []u8) []u8 {
mut d := new_digest(.sha384)
d.write(data) or { panic(err) }
sum := d.checksum()
sum := d.checksum_internal()
mut sum384 := []u8{len: sha512.size384}
copy(mut sum384, sum[..sha512.size384])
return sum384
Expand All @@ -296,7 +302,7 @@ pub fn sum384(data []u8) []u8 {
pub fn sum512_224(data []u8) []u8 {
mut d := new_digest(.sha512_224)
d.write(data) or { panic(err) }
sum := d.checksum()
sum := d.checksum_internal()
mut sum224 := []u8{len: sha512.size224}
copy(mut sum224, sum[..sha512.size224])
return sum224
Expand All @@ -306,7 +312,7 @@ pub fn sum512_224(data []u8) []u8 {
pub fn sum512_256(data []u8) []u8 {
mut d := new_digest(.sha512_256)
d.write(data) or { panic(err) }
sum := d.checksum()
sum := d.checksum_internal()
mut sum256 := []u8{len: sha512.size256}
copy(mut sum256, sum[..sha512.size256])
return sum256
Expand Down

0 comments on commit 880ce7a

Please sign in to comment.