Created
August 21, 2012 15:43
-
-
Save branan/3416682 to your computer and use it in GitHub Desktop.
Metaprogrammed SHA-1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <uint32_t val> | |
struct BigEndian { | |
enum { | |
value = (val & 0xFF << 24) | (val & 0xFF00 << 8) | (val & 0xFF0000 >> 8) | (val & 0xFF000000 >> 24) | |
}; | |
}; | |
struct ShaInitVector { | |
enum { | |
H0 = 0x67452301, | |
H1 = 0xEFCDAB89, | |
H2 = 0x98BADCFE, | |
H3 = 0x10325476, | |
H4 = 0xC3D2E1F0 | |
}; | |
}; | |
template <uint32_t val, uint32_t r> | |
struct RotateLeft{ | |
enum { value = (val << r) | (val >> (32 - r)) }; | |
}; | |
template <typename InitVector, typename RoundInfo, uint32_t w> | |
struct ShaRound { | |
enum { | |
H0 = RotateLeft<InitVector::H0, 5>::value + RoundInfo::f + InitVector::H4 + RoundInfo::k + w, | |
H1 = InitVector::H0, | |
H2 = RotateLeft<InitVector::H1, 30>, | |
H3 = InitVector::H2, | |
H4 = InitVector::H3 | |
}; | |
} | |
template <uint32_t b, uint32_t c, uint32_t d> | |
struct ShaF0 { | |
enum { | |
f = (b & c) | ((~b) & d), | |
k = 0x5A827999 | |
}; | |
}; | |
template <uint32_t b, uint32_t c, uint32_t d> | |
struct ShaF1 { | |
enum { | |
f = b ^ c ^ d, | |
k = 0x6ED9EBA1 | |
}; | |
}; | |
template <uint32_t b, uint32_t c, uint32_t d> | |
struct ShaF2 { | |
enum { | |
f = (b & c) | (b & d) | (c & d), | |
k = 0x8F1BBCDC | |
} | |
}; | |
template <uint32_t b, uint32_t c, uint32_t d> | |
struct ShaF3 { | |
enum { | |
f = b ^ c ^ d, | |
k = 0xCA62C1D6 | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tabs :(