core/crypto/keccak: API cleanup

- keccak.Keccak_Context -> keccak.Context
This commit is contained in:
Yawning Angel
2023-11-17 19:31:51 +09:00
parent aa821991b8
commit 841e73fcd5
2 changed files with 78 additions and 74 deletions
+18 -21
View File
@@ -32,11 +32,10 @@ hash_string_512 :: proc(data: string) -> [DIGEST_SIZE_512]byte {
// computed hash
hash_bytes_512 :: proc(data: []byte) -> [DIGEST_SIZE_512]byte {
hash: [DIGEST_SIZE_512]byte
ctx: botan.hash_t
botan.hash_init(&ctx, botan.HASH_KECCAK_512, 0)
botan.hash_update(ctx, len(data) == 0 ? nil : &data[0], uint(len(data)))
botan.hash_final(ctx, &hash[0])
botan.hash_destroy(ctx)
ctx: Context
init(&ctx)
update(&ctx, data)
final(&ctx, hash[:])
return hash
}
@@ -52,31 +51,29 @@ hash_string_to_buffer_512 :: proc(data: string, hash: []byte) {
// It requires that the destination buffer is at least as big as the digest size
hash_bytes_to_buffer_512 :: proc(data, hash: []byte) {
assert(len(hash) >= DIGEST_SIZE_512, "Size of destination buffer is smaller than the digest size")
ctx: botan.hash_t
botan.hash_init(&ctx, botan.HASH_KECCAK_512, 0)
botan.hash_update(ctx, len(data) == 0 ? nil : &data[0], uint(len(data)))
botan.hash_final(ctx, &hash[0])
botan.hash_destroy(ctx)
ctx: Context
init(&ctx)
update(&ctx, data)
final(&ctx, hash[:])
}
// hash_stream_512 will read the stream in chunks and compute a
// hash from its contents
hash_stream_512 :: proc(s: io.Stream) -> ([DIGEST_SIZE_512]byte, bool) {
hash: [DIGEST_SIZE_512]byte
ctx: botan.hash_t
botan.hash_init(&ctx, botan.HASH_KECCAK_512, 0)
ctx: Context
init(&ctx)
buf := make([]byte, 512)
defer delete(buf)
i := 1
for i > 0 {
i, _ = io.read(s, buf)
if i > 0 {
botan.hash_update(ctx, len(buf) == 0 ? nil : &buf[0], uint(i))
}
update(&ctx, buf[:i])
}
}
botan.hash_final(ctx, &hash[0])
botan.hash_destroy(ctx)
return hash, true
final(&ctx, hash[:])
return hash, true
}
// hash_file_512 will read the file provided by the given handle
@@ -105,17 +102,17 @@ hash_512 :: proc {
Low level API
*/
Keccak_Context :: botan.hash_t
Context :: botan.hash_t
init :: proc "contextless" (ctx: ^botan.hash_t) {
init :: proc "contextless" (ctx: ^Context) {
botan.hash_init(ctx, botan.HASH_KECCAK_512, 0)
}
update :: proc "contextless" (ctx: ^botan.hash_t, data: []byte) {
update :: proc "contextless" (ctx: ^Context, data: []byte) {
botan.hash_update(ctx^, len(data) == 0 ? nil : &data[0], uint(len(data)))
}
final :: proc "contextless" (ctx: ^botan.hash_t, hash: []byte) {
final :: proc "contextless" (ctx: ^Context, hash: []byte) {
botan.hash_final(ctx^, &hash[0])
botan.hash_destroy(ctx^)
}