StatusWith<SHA1Block> SHA1Block::fromBuffer(const uint8_t* input, size_t inputLen) { if (inputLen != kHashLength) { return {ErrorCodes::InvalidLength, str::stream() << "Unsupported SHA1Hash hash length: " << inputLen}; } HashType newHash; memcpy(newHash.data(), input, inputLen); return SHA1Block(newHash); }
// Calculates the MAC, hmacdigest will contain the result // Assumes that the last call to HMACBlock was done with len<64 void HMACDone(void) { uint8_t i; unsigned char temp[SHA1_DIGESTSIZE]; // terminate inner digest and store it SHA1Done(); memcpy(temp, shadigest, SHA1_DIGESTSIZE); // prepare key for outer digest // buffer will contain the original key xor 0x5c for (i=0;i<SHA1_BLOCKSIZE;i++) { hmackey[i] ^= 0x6a; } // initialize SHA1 and hash key SHA1Init(); SHA1Block(hmackey, SHA1_BLOCKSIZE); // hash inner digest and terminate hash SHA1Block(temp, SHA1_DIGESTSIZE); SHA1Done(); }
StatusWith<SHA1Block> SHA1Block::fromBinData(const BSONBinData& binData) { if (binData.type != BinDataGeneral) { return {ErrorCodes::UnsupportedFormat, "SHA1Block only accepts BinDataGeneral type"}; } if (binData.length != kHashLength) { return {ErrorCodes::UnsupportedFormat, str::stream() << "Unsupported SHA1Block hash length: " << binData.length}; } HashType newHash; memcpy(newHash.data(), binData.data, binData.length); return SHA1Block(newHash); }
// Initializes HMAC algorithm with given key // key must be smaller than 64 bytes void HMACInit(const unsigned char* key, const uint8_t len) { uint8_t i; // copy key, XOR it for the inner digest, pad it to block size for (i=0;i<len;i++) { hmackey[i] = key[i] ^ 0x36; } for (i=len;i<SHA1_BLOCKSIZE;i++) { hmackey[i] = 0x36; } // initialize SHA1 and hash key SHA1Init(); SHA1Block(hmackey, SHA1_BLOCKSIZE); }
// Authenticates blocks of 64 bytes of data. // Only the last block *must* be smaller than 64 bytes. void HMACBlock(const unsigned char* data, const uint8_t len) { SHA1Block(data, len); }
SHA1Block SHA1Block::fromBinData(std::vector<unsigned char> bytes) { HashType newHash; invariant(bytes.size() == kHashLength); memcpy(newHash.data(), bytes.data(), bytes.size()); return SHA1Block(newHash); }