/src/S2OPC/src/Common/helpers/sopc_hash.c
Line | Count | Source |
1 | | /* |
2 | | * Licensed to Systerel under one or more contributor license |
3 | | * agreements. See the NOTICE file distributed with this work |
4 | | * for additional information regarding copyright ownership. |
5 | | * Systerel licenses this file to you under the Apache |
6 | | * License, Version 2.0 (the "License"); you may not use this |
7 | | * file except in compliance with the License. You may obtain |
8 | | * a copy of the License at |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, |
13 | | * software distributed under the License is distributed on an |
14 | | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | | * KIND, either express or implied. See the License for the |
16 | | * specific language governing permissions and limitations |
17 | | * under the License. |
18 | | */ |
19 | | |
20 | | #include "sopc_hash.h" |
21 | | |
22 | | uint64_t SOPC_DJBHash(const uint8_t* data, size_t len) |
23 | 0 | { |
24 | 0 | return SOPC_DJBHash_Step(5381, data, len); |
25 | 0 | } |
26 | | |
27 | | uint64_t SOPC_DJBHash_Step(uint64_t current, const uint8_t* data, size_t len) |
28 | 0 | { |
29 | 0 | for (size_t i = 0; i < len; ++i) |
30 | 0 | { |
31 | 0 | current = (current << 5) + current + data[i]; |
32 | 0 | } |
33 | |
|
34 | 0 | return current; |
35 | 0 | } |
36 | | |
37 | | /* Constants of the 64-bit FNV-1a hash, as specified by its authors: offset basis |
38 | | * 0xcbf29ce484222325 and prime 0x100000001b3. */ |
39 | 0 | #define FNV1A_OFFSET_BASIS 14695981039346656037ULL |
40 | 0 | #define FNV1A_PRIME 1099511628211ULL |
41 | | |
42 | | uint64_t SOPC_FNV1aHash(const uint8_t* data, size_t len) |
43 | 0 | { |
44 | 0 | return SOPC_FNV1aHash_Step(FNV1A_OFFSET_BASIS, data, len); |
45 | 0 | } |
46 | | |
47 | | uint64_t SOPC_FNV1aHash_Step(uint64_t current, const uint8_t* data, size_t len) |
48 | 0 | { |
49 | 0 | for (size_t i = 0; i < len; ++i) |
50 | 0 | { |
51 | | // FNV-1a: xor before multiply (FNV-1 does the opposite and diffuses less) |
52 | 0 | current ^= data[i]; |
53 | 0 | current *= FNV1A_PRIME; |
54 | 0 | } |
55 | |
|
56 | 0 | return current; |
57 | 0 | } |