forked from hanfei1991/encryption_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsha1.h
58 lines (54 loc) · 1.69 KB
/
sha1.h
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
//
// Created by Gin Liu on 2018/11/2.
//
//
// Created by Gin Liu on 2018/9/9.
//
#include <array>
#include <fstream>
#include <string>
#include <vector>
/**
* Bit rotation left by a compile-time constant amount
* @param input the input word
* @return input rotated left by ROT bits
*/
template <size_t ROT, typename T>
inline T rotl(T input) {
static_assert(ROT > 0 && ROT < 8 * sizeof(T), "Invalid rotation constant");
return static_cast<T>((input << ROT) | (input >> (8 * sizeof(T) - ROT)));
}
/**
* Bit rotation right by a compile-time constant amount
* @param input the input word
* @return input rotated right by ROT bits
*/
template <size_t ROT, typename T>
inline T rotr(T input) {
static_assert(ROT > 0 && ROT < 8 * sizeof(T), "Invalid rotation constant");
return static_cast<T>((input >> ROT) | (input << (8 * sizeof(T) - ROT)));
}
/**
* Bit rotation left, variable rotation amount
* @param input the input word
* @param rot the number of bits to rotate, must be between 0 and sizeof(T)*8-1
* @return input rotated left by rot bits
*/
template <typename T>
inline T rotl_var(T input, size_t rot) {
return rot ? static_cast<T>((input << rot) | (input >> (sizeof(T) * 8 - rot)))
: input;
}
/**
* Bit rotation right, variable rotation amount
* @param input the input word
* @param rot the number of bits to rotate, must be between 0 and sizeof(T)*8-1
* @return input rotated right by rot bits
*/
template <typename T>
inline T rotr_var(T input, size_t rot) {
return rot ? static_cast<T>((input >> rot) | (input << (sizeof(T) * 8 - rot)))
: input;
}
void sse2_compress_n(std::vector<uint32_t>& digest, const uint8_t *input,
size_t blocks);