30 namespace digest_detail {
32 static inline uint32_t
min(uint32_t x, uint32_t y) {
36 static inline void store64h(uint64_t x,
unsigned char* y) {
37 for (
int i = 0; i != 8; ++i)
38 y[i] = (x >> ((7 - i) * 8)) & 255;
40 static inline uint32_t
load32h(
const uint8_t* y) {
41 return (uint32_t(y[0]) << 24) | (uint32_t(y[1]) << 16) |
42 (uint32_t(y[2]) << 8) | (uint32_t(y[3]) << 0);
44 static inline void store32h(uint32_t x, uint8_t* y) {
45 for (
int i = 0; i != 4; ++i)
46 y[i] = (x >> ((3 - i) * 8)) & 255;
50 uint32_t
F0(
const uint32_t& x,
const uint32_t& y,
const uint32_t& z) {
51 return (z ^ (x & (y ^ z)));
54 uint32_t
F1(
const uint32_t& x,
const uint32_t& y,
const uint32_t& z) {
58 uint32_t
F2(
const uint32_t& x,
const uint32_t& y,
const uint32_t& z) {
59 return ((x & y) | (z & (x | y)));
62 uint32_t
F3(
const uint32_t& x,
const uint32_t& y,
const uint32_t& z) {
67 uint32_t a, b, c, d, e, W[80], i, t;
70 for (i = 0; i < 16; i++) {
82 for (i = 16; i < 80; i++) {
83 W[i] =
rol32(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
87 for (i = 0; i < 20; ++i) {
88 e = (
rol32(a, 5) +
F0(b, c, d) + e + W[i] + 0x5a827999UL);
90 t = e, e = d, d = c, c = b, b = a, a = t;
92 for ( ; i < 40; ++i) {
93 e = (
rol32(a, 5) +
F1(b, c, d) + e + W[i] + 0x6ed9eba1UL);
95 t = e, e = d, d = c, c = b, b = a, a = t;
97 for ( ; i < 60; ++i) {
98 e = (
rol32(a, 5) +
F2(b, c, d) + e + W[i] + 0x8f1bbcdcUL);
100 t = e, e = d, d = c, c = b, b = a, a = t;
102 for ( ; i < 80; ++i) {
103 e = (
rol32(a, 5) +
F3(b, c, d) + e + W[i] + 0xca62c1d6UL);
105 t = e, e = d, d = c, c = b, b = a, a = t;
109 state[0] = state[0] + a;
110 state[1] = state[1] + b;
111 state[2] = state[2] + c;
112 state[3] = state[3] + d;
113 state[4] = state[4] + e;
121 state_[0] = 0x67452301UL;
122 state_[1] = 0xefcdab89UL;
123 state_[2] = 0x98badcfeUL;
124 state_[3] = 0x10325476UL;
125 state_[4] = 0xc3d2e1f0UL;
137 const uint32_t block_size =
sizeof(
SHA1::buf_);
138 auto in =
static_cast<const uint8_t*
>(data);
142 if (
curlen_ == 0 && size >= block_size)
153 for (
const uint8_t* a = in; a != in + n; ++a, ++b) {
160 if (curlen_ == block_size)
171 return process(str.data(), str.size());
200 for (
size_t i = 0; i < 5; i++)
206 finalize(const_cast<char*>(out.data()));
222 std::string
sha1_hex(
const void* data, uint32_t size) {
223 return SHA1(data, size).digest_hex();
227 return SHA1(str).digest_hex();
231 return SHA1(data, size).digest_hex_uc();
235 return SHA1(str).digest_hex_uc();
static uint32_t F3(const uint32_t &x, const uint32_t &y, const uint32_t &z)
void process(const void *data, uint32_t size)
process more data
static void store64h(uint64_t x, unsigned char *y)
SHA-1 processor without external dependencies.
static void store32h(uint32_t x, uint8_t *y)
static uint32_t load32h(const uint8_t *y)
std::string digest()
finalize computation and return 20 byte (160 bit) digest
std::string hexdump_lc(const void *const data, size_t size)
Dump a (binary) string as a sequence of lowercase hexadecimal pairs.
void finalize(void *digest)
finalize computation and output 20 byte (160 bit) digest
static uint32_t min(uint32_t x, uint32_t y)
static uint32_t F0(const uint32_t &x, const uint32_t &y, const uint32_t &z)
SHA1()
construct empty object.
static uint32_t F2(const uint32_t &x, const uint32_t &y, const uint32_t &z)
static uint32_t rol32(const uint32_t &x, int i)
rol32 - generic
static void sha1_compress(uint32_t state[4], const uint8_t *buf)
std::string digest_hex_uc()
finalize computation and return 20 byte (160 bit) digest upper-case hex
std::string sha1_hex(const void *data, uint32_t size)
process data and return 20 byte (160 bit) digest hex encoded
std::string hexdump(const void *const data, size_t size)
Dump a (binary) string as a sequence of uppercase hexadecimal pairs.
static uint32_t F1(const uint32_t &x, const uint32_t &y, const uint32_t &z)
static constexpr size_t kDigestLength
digest length in bytes
std::string sha1_hex_uc(const void *data, uint32_t size)
process data and return 20 byte (160 bit) digest upper-case hex encoded
std::string digest_hex()
finalize computation and return 20 byte (160 bit) digest hex encoded