aboutsummaryrefslogtreecommitdiff
path: root/core/crypto/hash/hash_os.odin
blob: d54e657ad2dee52c1f0da3e24517b562d1f00896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#+build !freestanding
package crypto_hash

import "core:io"
import "core:os"

// 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,
	load_at_once := false,
	allocator := context.allocator,
) -> (
	[]byte,
	io.Error,
) {
	if !load_at_once {
		return hash_stream(algorithm, os.stream_from_handle(hd), allocator)
	}

	buf, ok := os.read_entire_file(hd, allocator)
	if !ok {
		return nil, io.Error.Unknown
	}
	defer delete(buf, allocator)

	return hash_bytes(algorithm, buf, allocator), io.Error.None
}

hash :: proc {
	hash_stream,
	hash_file,
	hash_bytes,
	hash_string,
	hash_bytes_to_buffer,
	hash_string_to_buffer,
}