hash: make public the hash.Hash interface, add tests to current implementers (#21984)

This commit is contained in:
Coachonko 2024-08-03 18:52:39 +02:00 committed by GitHub
parent e906033e4e
commit 1a8eff8d2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 1 deletions

View File

@ -2,6 +2,14 @@
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
import crypto.md5 import crypto.md5
import hash
// verify md5.Digest implements hash.Hash
fn test_digest_implements_hash() {
get_digest := fn () hash.Hash {
return md5.new()
}
}
fn test_crypto_md5() { fn test_crypto_md5() {
assert md5.sum('this is a md5 checksum.'.bytes()).hex() == '6fb421ff99036547655984da12973431' assert md5.sum('this is a md5 checksum.'.bytes()).hex() == '6fb421ff99036547655984da12973431'

View File

@ -2,6 +2,14 @@
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
import crypto.sha1 import crypto.sha1
import hash
// verify sha1.Digest implements hash.Hash
fn test_digest_implements_hash() {
get_digest := fn () hash.Hash {
return sha1.new()
}
}
fn test_crypto_sha1() { fn test_crypto_sha1() {
assert sha1.sum('This is a sha1 checksum.'.bytes()).hex() == 'e100d74442faa5dcd59463b808983c810a8eb5a1' assert sha1.sum('This is a sha1 checksum.'.bytes()).hex() == 'e100d74442faa5dcd59463b808983c810a8eb5a1'

View File

@ -2,6 +2,14 @@
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
import crypto.sha256 import crypto.sha256
import hash
// verify sha256.Digest implements hash.Hash
fn test_digest_implements_hash() {
get_digest := fn () hash.Hash {
return sha256.new()
}
}
fn test_crypto_sha256() { fn test_crypto_sha256() {
assert sha256.sum('This is a sha256 checksum.'.bytes()).hex() == 'dc7163299659529eae29683eb1ffec50d6c8fc7275ecb10c145fde0e125b8727' assert sha256.sum('This is a sha256 checksum.'.bytes()).hex() == 'dc7163299659529eae29683eb1ffec50d6c8fc7275ecb10c145fde0e125b8727'

View File

@ -2,6 +2,14 @@
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
import crypto.sha512 import crypto.sha512
import hash
// verify sha512.Digest implements hash.Hash
fn test_digest_implements_hash() {
get_digest := fn () hash.Hash {
return sha512.new()
}
}
const final_result = '4143e55fcba7e39b20f62a1368e5eb28f64a8859458886117ac66027832e0f9f5263daec688c439d2d0fa07059334668d39e59543039703dbb7e03ec9da7f8d7' const final_result = '4143e55fcba7e39b20f62a1368e5eb28f64a8859458886117ac66027832e0f9f5263daec688c439d2d0fa07059334668d39e59543039703dbb7e03ec9da7f8d7'

View File

@ -3,7 +3,7 @@
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module hash module hash
interface Hasher { pub interface Hash {
// Sum appends the current hash to b and returns the resulting array. // Sum appends the current hash to b and returns the resulting array.
// It does not change the underlying hash state. // It does not change the underlying hash state.
sum(b []u8) []u8 sum(b []u8) []u8