Replace core:crypto usage of core:os with core:os/os2

This commit is contained in:
Jeroen van Rijn
2025-10-27 20:12:13 +01:00
parent fca2232028
commit 60c25ab4cb
2 changed files with 37 additions and 17 deletions

View File

@@ -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,
}
}

View File

@@ -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: