-
Notifications
You must be signed in to change notification settings - Fork 4
/
hash.c
67 lines (52 loc) · 1.27 KB
/
hash.c
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
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: MIT
#include <xxhash.h>
#include <openssl/sha.h>
#include <blake2.h>
#include "ondisk_format.h"
#include "messages.h"
#include "libs/crc32c.h"
static int hash_crc32c(const u8* buf, size_t length, u8 *out)
{
u32 crc = ~0;
crc = crc32c(~0, buf, length);
put_unaligned_le32(~crc, out);
return 0;
}
static int hash_xxhash(const u8* buf, size_t length, u8 *out)
{
XXH64_hash_t hash;
hash = XXH64(buf, length, 0);
put_unaligned_le64(hash, out);
return 0;
}
static int hash_sha256(const u8* buf, size_t length, u8 *out)
{
SHA256(buf, length, out);
return 0;
}
static int hash_blake2b(const u8* buf, size_t length, u8 *out)
{
blake2b_state S;
blake2b_init(&S, BTRFS_CSUM_SIZE);
blake2b_update(&S, buf, length);
blake2b_final(&S, out, BTRFS_CSUM_SIZE);
return 0;
}
int btrfs_csum_data(u16 csum_type, const u8 *data, u8 *out, size_t len)
{
memset(out, 0, BTRFS_CSUM_SIZE);
switch(csum_type) {
case BTRFS_CSUM_TYPE_CRC32:
return hash_crc32c(data, len, out);
case BTRFS_CSUM_TYPE_XXHASH:
return hash_xxhash(data, len, out);
case BTRFS_CSUM_TYPE_SHA256:
return hash_sha256(data, len, out);
case BTRFS_CSUM_TYPE_BLAKE2:
return hash_blake2b(data, len, out);
default:
error("unknown csum type: %d\n", csum_type);
assert(0);
}
return -1;
}