diff options
| author | zhibog <zhibog-github@web.de> | 2021-10-15 20:29:25 +0200 |
|---|---|---|
| committer | zhibog <zhibog-github@web.de> | 2021-10-15 20:29:25 +0200 |
| commit | f48ee00c2fe358002db6d1a2b3baf2a7921ca945 (patch) | |
| tree | ca861d6df9120cd2b56b6720e53630959e36cf02 /core/crypto/sha2 | |
| parent | ced0ea515c311a370ba05b4fa40e0b6d3b060949 (diff) | |
Improved API. hash_file procs now just take a file handle instead of a path
Diffstat (limited to 'core/crypto/sha2')
| -rw-r--r-- | core/crypto/sha2/sha2.odin | 92 |
1 files changed, 40 insertions, 52 deletions
diff --git a/core/crypto/sha2/sha2.odin b/core/crypto/sha2/sha2.odin index ad23b473c..2f7d44a21 100644 --- a/core/crypto/sha2/sha2.odin +++ b/core/crypto/sha2/sha2.odin @@ -108,11 +108,11 @@ hash_stream_224 :: proc(s: io.Stream) -> ([28]byte, bool) { return _hash_impl->hash_stream_28(s) } -// hash_file_224 will try to open the file provided by the given -// path and pass it to hash_stream to compute a hash -hash_file_224 :: proc(path: string, load_at_once: bool) -> ([28]byte, bool) { +// hash_file_224 will read the file provided by the given handle +// and compute a hash +hash_file_224 :: proc(hd: os.Handle, load_at_once := false) -> ([28]byte, bool) { _create_sha256_ctx(true) - return _hash_impl->hash_file_28(path, load_at_once) + return _hash_impl->hash_file_28(hd, load_at_once) } hash_224 :: proc { @@ -142,11 +142,11 @@ hash_stream_256 :: proc(s: io.Stream) -> ([32]byte, bool) { return _hash_impl->hash_stream_32(s) } -// hash_file_256 will try to open the file provided by the given -// path and pass it to hash_stream to compute a hash -hash_file_256 :: proc(path: string, load_at_once: bool) -> ([32]byte, bool) { +// hash_file_256 will read the file provided by the given handle +// and compute a hash +hash_file_256 :: proc(hd: os.Handle, load_at_once := false) -> ([32]byte, bool) { _create_sha256_ctx(false) - return _hash_impl->hash_file_32(path, load_at_once) + return _hash_impl->hash_file_32(hd, load_at_once) } hash_256 :: proc { @@ -176,11 +176,11 @@ hash_stream_384 :: proc(s: io.Stream) -> ([48]byte, bool) { return _hash_impl->hash_stream_48(s) } -// hash_file_384 will try to open the file provided by the given -// path and pass it to hash_stream to compute a hash -hash_file_384 :: proc(path: string, load_at_once: bool) -> ([48]byte, bool) { +// hash_file_384 will read the file provided by the given handle +// and compute a hash +hash_file_384 :: proc(hd: os.Handle, load_at_once := false) -> ([48]byte, bool) { _create_sha512_ctx(true) - return _hash_impl->hash_file_48(path, load_at_once) + return _hash_impl->hash_file_48(hd, load_at_once) } hash_384 :: proc { @@ -210,11 +210,11 @@ hash_stream_512 :: proc(s: io.Stream) -> ([64]byte, bool) { return _hash_impl->hash_stream_64(s) } -// hash_file_512 will try to open the file provided by the given -// path and pass it to hash_stream to compute a hash -hash_file_512 :: proc(path: string, load_at_once: bool) -> ([64]byte, bool) { +// hash_file_512 will read the file provided by the given handle +// and compute a hash +hash_file_512 :: proc(hd: os.Handle, load_at_once := false) -> ([64]byte, bool) { _create_sha512_ctx(false) - return _hash_impl->hash_file_64(path, load_at_once) + return _hash_impl->hash_file_64(hd, load_at_once) } hash_512 :: proc { @@ -270,15 +270,12 @@ hash_stream_odin_28 :: #force_inline proc(ctx: ^_ctx.Hash_Context, fs: io.Stream } } -hash_file_odin_28 :: #force_inline proc(ctx: ^_ctx.Hash_Context, path: string, load_at_once: bool) -> ([28]byte, bool) { - if hd, err := os.open(path); err == os.ERROR_NONE { - defer os.close(hd) - if !load_at_once { - return hash_stream_odin_28(ctx, os.stream_from_handle(hd)) - } else { - if buf, read_ok := os.read_entire_file(path); read_ok { - return hash_bytes_odin_28(ctx, buf[:]), read_ok - } +hash_file_odin_28 :: #force_inline proc(ctx: ^_ctx.Hash_Context, hd: os.Handle, load_at_once := false) -> ([28]byte, bool) { + if !load_at_once { + return hash_stream_odin_28(ctx, os.stream_from_handle(hd)) + } else { + if buf, ok := util.read_entire_file(hd); ok { + return hash_bytes_odin_28(ctx, buf[:]), ok } } return [28]byte{}, false @@ -314,15 +311,12 @@ hash_stream_odin_32 :: #force_inline proc(ctx: ^_ctx.Hash_Context, fs: io.Stream } } -hash_file_odin_32 :: #force_inline proc(ctx: ^_ctx.Hash_Context, path: string, load_at_once: bool) -> ([32]byte, bool) { - if hd, err := os.open(path); err == os.ERROR_NONE { - defer os.close(hd) - if !load_at_once { - return hash_stream_odin_32(ctx, os.stream_from_handle(hd)) - } else { - if buf, read_ok := os.read_entire_file(path); read_ok { - return hash_bytes_odin_32(ctx, buf[:]), read_ok - } +hash_file_odin_32 :: #force_inline proc(ctx: ^_ctx.Hash_Context, hd: os.Handle, load_at_once := false) -> ([32]byte, bool) { + if !load_at_once { + return hash_stream_odin_32(ctx, os.stream_from_handle(hd)) + } else { + if buf, ok := util.read_entire_file(hd); ok { + return hash_bytes_odin_32(ctx, buf[:]), ok } } return [32]byte{}, false @@ -358,15 +352,12 @@ hash_stream_odin_48 :: #force_inline proc(ctx: ^_ctx.Hash_Context, fs: io.Stream } } -hash_file_odin_48 :: #force_inline proc(ctx: ^_ctx.Hash_Context, path: string, load_at_once: bool) -> ([48]byte, bool) { - if hd, err := os.open(path); err == os.ERROR_NONE { - defer os.close(hd) - if !load_at_once { - return hash_stream_odin_48(ctx, os.stream_from_handle(hd)) - } else { - if buf, read_ok := os.read_entire_file(path); read_ok { - return hash_bytes_odin_48(ctx, buf[:]), read_ok - } +hash_file_odin_48 :: #force_inline proc(ctx: ^_ctx.Hash_Context, hd: os.Handle, load_at_once := false) -> ([48]byte, bool) { + if !load_at_once { + return hash_stream_odin_48(ctx, os.stream_from_handle(hd)) + } else { + if buf, ok := util.read_entire_file(hd); ok { + return hash_bytes_odin_48(ctx, buf[:]), ok } } return [48]byte{}, false @@ -402,15 +393,12 @@ hash_stream_odin_64 :: #force_inline proc(ctx: ^_ctx.Hash_Context, fs: io.Stream } } -hash_file_odin_64 :: #force_inline proc(ctx: ^_ctx.Hash_Context, path: string, load_at_once: bool) -> ([64]byte, bool) { - if hd, err := os.open(path); err == os.ERROR_NONE { - defer os.close(hd) - if !load_at_once { - return hash_stream_odin_64(ctx, os.stream_from_handle(hd)) - } else { - if buf, read_ok := os.read_entire_file(path); read_ok { - return hash_bytes_odin_64(ctx, buf[:]), read_ok - } +hash_file_odin_64 :: #force_inline proc(ctx: ^_ctx.Hash_Context, hd: os.Handle, load_at_once := false) -> ([64]byte, bool) { + if !load_at_once { + return hash_stream_odin_64(ctx, os.stream_from_handle(hd)) + } else { + if buf, ok := util.read_entire_file(hd); ok { + return hash_bytes_odin_64(ctx, buf[:]), ok } } return [64]byte{}, false |