aboutsummaryrefslogtreecommitdiff
path: root/core/crypto/hash/hash_os.odin
blob: 49c1a0ff8d4a24b11a1526605012745500590e15 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#+build !freestanding
#+build !js
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_by_handle :: proc(
	algorithm:      Algorithm,
	handle:         ^os.File,
	load_at_once := false,
	allocator    := context.allocator,
) -> (
	[]byte,
	io.Error,
) {
	if !load_at_once {
		return hash_stream(algorithm, os.to_stream(handle), allocator)
	}

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

	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_by_handle,
	hash_bytes,
	hash_string,
	hash_bytes_to_buffer,
	hash_string_to_buffer,
}