mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Add Botan crypto lib as a vendor library
This commit is contained in:
Vendored
+98
@@ -0,0 +1,98 @@
|
||||
package botan_blake2b
|
||||
|
||||
/*
|
||||
Copyright 2021 zhibog
|
||||
Made available under the BSD-3 license.
|
||||
|
||||
List of contributors:
|
||||
zhibog: Initial implementation.
|
||||
|
||||
Interface for the BLAKE2B hashing algorithm.
|
||||
The hash will be computed via bindings to the Botan crypto library
|
||||
*/
|
||||
|
||||
import "core:os"
|
||||
import "core:io"
|
||||
|
||||
import botan "../bindings"
|
||||
|
||||
/*
|
||||
High level API
|
||||
*/
|
||||
|
||||
// hash_string will hash the given input and return the
|
||||
// computed hash
|
||||
hash_string :: proc "contextless" (data: string) -> [64]byte {
|
||||
return hash_bytes(transmute([]byte)(data))
|
||||
}
|
||||
|
||||
// hash_bytes will hash the given input and return the
|
||||
// computed hash
|
||||
hash_bytes :: proc "contextless" (data: []byte) -> [64]byte {
|
||||
hash: [64]byte
|
||||
ctx: botan.hash_t
|
||||
botan.hash_init(&ctx, botan.HASH_BLAKE2B, 0)
|
||||
botan.hash_update(ctx, len(data) == 0 ? nil : &data[0], uint(len(data)))
|
||||
botan.hash_final(ctx, &hash[0])
|
||||
botan.hash_destroy(ctx)
|
||||
return hash
|
||||
}
|
||||
|
||||
// hash_stream will read the stream in chunks and compute a
|
||||
// hash from its contents
|
||||
hash_stream :: proc(s: io.Stream) -> ([64]byte, bool) {
|
||||
hash: [64]byte
|
||||
ctx: botan.hash_t
|
||||
botan.hash_init(&ctx, botan.HASH_BLAKE2B, 0)
|
||||
buf := make([]byte, 512)
|
||||
defer delete(buf)
|
||||
i := 1
|
||||
for i > 0 {
|
||||
i, _ = s->impl_read(buf)
|
||||
if i > 0 {
|
||||
botan.hash_update(ctx, len(buf) == 0 ? nil : &buf[0], uint(i))
|
||||
}
|
||||
}
|
||||
botan.hash_final(ctx, &hash[0])
|
||||
botan.hash_destroy(ctx)
|
||||
return hash, true
|
||||
}
|
||||
|
||||
// hash_file will read the file provided by the given handle
|
||||
// and compute a hash
|
||||
hash_file :: proc(hd: os.Handle, load_at_once := false) -> ([64]byte, bool) {
|
||||
if !load_at_once {
|
||||
return hash_stream(os.stream_from_handle(hd))
|
||||
} else {
|
||||
if buf, ok := os.read_entire_file(hd); ok {
|
||||
return hash_bytes(buf[:]), ok
|
||||
}
|
||||
}
|
||||
return [64]byte{}, false
|
||||
}
|
||||
|
||||
hash :: proc {
|
||||
hash_stream,
|
||||
hash_file,
|
||||
hash_bytes,
|
||||
hash_string,
|
||||
}
|
||||
|
||||
/*
|
||||
Low level API
|
||||
*/
|
||||
|
||||
Blake2b_Context :: botan.hash_t
|
||||
|
||||
init :: proc "contextless" (ctx: ^botan.hash_t) {
|
||||
botan.hash_init(ctx, botan.HASH_BLAKE2B, 0)
|
||||
}
|
||||
|
||||
update :: proc "contextless" (ctx: ^botan.hash_t, data: []byte) {
|
||||
botan.hash_update(ctx^, len(data) == 0 ? nil : &data[0], uint(len(data)))
|
||||
}
|
||||
|
||||
final :: proc "contextless" (ctx: ^botan.hash_t, hash: []byte) {
|
||||
botan.hash_final(ctx^, &hash[0])
|
||||
botan.hash_destroy(ctx^)
|
||||
}
|
||||
Reference in New Issue
Block a user