commonlib
sha1.h File Reference

Compile time SHA-1 hash calculation. More...

#include <cstdint>
#include <array>
#include "crypto.h"
#include <stdexcept>

Go to the source code of this file.

Classes

struct  Detail::Sha1State
 

Macros

#define F0(x, y, z)   (z ^ (x & (y ^ z)))
 
#define F1(x, y, z)   (x ^ y ^ z)
 
#define F2(x, y, z)   ((x & y) | (z & (x | y)))
 
#define F3(x, y, z)   (x ^ y ^ z)
 
#define ROL(x, y)   ( (((uint32_t)(x)<<(uint32_t)((y)&31)) | (((uint32_t)(x)&0xFFFFFFFFUL)>>(uint32_t)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
 
#define ROR(x, y)   ( ((((uint32_t)(x)&0xFFFFFFFFUL)>>(uint32_t)((y)&31)) | ((uint32_t)(x)<<(uint32_t)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
 
#define ROLc(x, y)   ( (((uint32_t)(x)<<(uint32_t)((y)&31)) | (((uint32_t)(x)&0xFFFFFFFFUL)>>(uint32_t)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
 
#define RORc(x, y)   ( ((((uint32_t)(x)&0xFFFFFFFFUL)>>(uint32_t)((y)&31)) | ((uint32_t)(x)<<(uint32_t)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
 
#define STORE64H(x, y)
 
#define STORE32H(x, y)
 
#define LOAD32H(x, y)
 
#define MIN(x, y)   ( ((x)<(y))?(x):(y) )
 
#define FF0(a, b, c, d, e, i)   e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30);
 
#define FF1(a, b, c, d, e, i)   e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30);
 
#define FF2(a, b, c, d, e, i)   e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30);
 
#define FF3(a, b, c, d, e, i)   e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30);
 

Enumerations

enum  Sha1Status { Ok, InvalidArgument, Overflow }
 

Functions

constexpr Sha1Status Detail::sha1Compress (Sha1State &md, const unsigned char *buffer)
 
template<bool HasTrailingZero = false, std::uint32_t N>
constexpr Sha1Status Detail::sha1Process (Sha1State &md, const unsigned char(&in)[N])
 Process a block of memory though the hash. More...
 
constexpr Sha1Status Detail::sha1Done (Sha1State &md, unsigned char *const out)
 Terminate the hash to get the digest. More...
 
template<bool HasTrailingZero = false, std::size_t N>
constexpr std::array< unsigned char, 20 > ct_sha1 (const unsigned char(&in)[N])
 Calculates the SHA-1 digest in compile-time if the arguments are constexpr. More...
 

Detailed Description

Compile time SHA-1 hash calculation.

Author
Martin Kraner
Date
04/03/2019
Version
1.0

Macro Definition Documentation

◆ LOAD32H

#define LOAD32H (   x,
  y 
)
Value:
do { x = ((uint32_t)((y)[0] & 255)<<24) | \
((uint32_t)((y)[1] & 255)<<16) | \
((uint32_t)((y)[2] & 255)<<8) | \
((uint32_t)((y)[3] & 255)); } while(0)

◆ STORE32H

#define STORE32H (   x,
  y 
)
Value:
do { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
(y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } while(0)

◆ STORE64H

#define STORE64H (   x,
  y 
)
Value:
do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
(y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
(y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
(y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)

Function Documentation

◆ ct_sha1()

template<bool HasTrailingZero = false, std::size_t N>
constexpr std::array<unsigned char, 20> ct_sha1 ( const unsigned char(&)  in[N])

Calculates the SHA-1 digest in compile-time if the arguments are constexpr.

Template Parameters
HasTrailingZeroWhether the input data is a null terminated string
NThe length of the input data array.
Parameters
inThe input data to hash.
Returns
The 20 bytes representing a Sha1 digest of the input data.

◆ sha1Done()

constexpr Sha1Status Detail::sha1Done ( Sha1State &  md,
unsigned char *const  out 
)

Terminate the hash to get the digest.

Parameters
mdThe hash state
out[out] The destination of the hash (20 bytes)
Returns
Sha1Status::Ok if successful

◆ sha1Process()

template<bool HasTrailingZero = false, std::uint32_t N>
constexpr Sha1Status Detail::sha1Process ( Sha1State &  md,
const unsigned char(&)  in[N] 
)

Process a block of memory though the hash.

Template Parameters
HasTrailingZeroWhether the input data is a null terminated string
NThe length of the input data array.
Parameters
mdThe hash state
inThe data to hash
Returns
Sha1Status::Ok if successful