diff --git a/core/crypto/hash/hash_os.odin b/core/crypto/hash/hash_os.odin index d54e657ad..32347c452 100644 --- a/core/crypto/hash/hash_os.odin +++ b/core/crypto/hash/hash_os.odin @@ -2,25 +2,25 @@ package crypto_hash import "core:io" -import "core:os" +import os "core:os/os2" -// hash_file will read the file provided by the given handle and return the +// `hash_file` will read the file provided by the given handle and return the // computed digest in a newly allocated slice. -hash_file :: proc( - algorithm: Algorithm, - hd: os.Handle, +hash_file_by_handle :: proc( + algorithm: Algorithm, + handle: ^os.File, load_at_once := false, - allocator := context.allocator, + allocator := context.allocator, ) -> ( []byte, io.Error, ) { if !load_at_once { - return hash_stream(algorithm, os.stream_from_handle(hd), allocator) + return hash_stream(algorithm, handle.stream, allocator) } - buf, ok := os.read_entire_file(hd, allocator) - if !ok { + buf, err := os.read_entire_file(handle, allocator) + if err != nil { return nil, io.Error.Unknown } defer delete(buf, allocator) @@ -28,11 +28,30 @@ hash_file :: proc( return hash_bytes(algorithm, buf, allocator), io.Error.None } +hash_file_by_name :: proc( + algorithm: Algorithm, + filename: string, + load_at_once := false, + allocator := context.allocator, +) -> ( + []byte, + io.Error, +) { + handle, err := os.open(filename) + defer os.close(handle) + + if err != nil { + return {}, io.Error.Unknown + } + return hash_file_by_handle(algorithm, handle, load_at_once, allocator) +} + + hash :: proc { hash_stream, - hash_file, + hash_file_by_handle, hash_bytes, hash_string, hash_bytes_to_buffer, hash_string_to_buffer, -} +} \ No newline at end of file diff --git a/core/crypto/rand_windows.odin b/core/crypto/rand_windows.odin index 83a976e38..c1a307007 100644 --- a/core/crypto/rand_windows.odin +++ b/core/crypto/rand_windows.odin @@ -1,21 +1,22 @@ package crypto import win32 "core:sys/windows" -import "core:os" import "core:fmt" HAS_RAND_BYTES :: true @(private) _rand_bytes :: proc(dst: []byte) { - ret := os.Platform_Error(win32.BCryptGenRandom(nil, raw_data(dst), u32(len(dst)), win32.BCRYPT_USE_SYSTEM_PREFERRED_RNG)) - if ret != nil { - #partial switch ret { - case os.ERROR_INVALID_HANDLE: + // NOTE(Jeroen) We don't actually use anything `core:os`-specific here. + // So let's just evaluate `win32`'s return values without first wrapping them. + ret := win32.DWORD(win32.BCryptGenRandom(nil, raw_data(dst), u32(len(dst)), win32.BCRYPT_USE_SYSTEM_PREFERRED_RNG)) + if ret != win32.ERROR_SUCCESS { + switch ret { + case win32.ERROR_INVALID_HANDLE: // The handle to the first parameter is invalid. // This should not happen here, since we explicitly pass nil to it panic("crypto: BCryptGenRandom Invalid handle for hAlgorithm") - case os.ERROR_INVALID_PARAMETER: + case win32.ERROR_INVALID_PARAMETER: // One of the parameters was invalid panic("crypto: BCryptGenRandom Invalid parameter") case: