1fe6060f1SDimitry Andric //====- SHA256.cpp - SHA256 implementation ---*- C++ -* ======// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric /* 9fe6060f1SDimitry Andric * The SHA-256 Secure Hash Standard was published by NIST in 2002. 10fe6060f1SDimitry Andric * 11fe6060f1SDimitry Andric * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf 12fe6060f1SDimitry Andric * 13fe6060f1SDimitry Andric * The implementation is based on nacl's sha256 implementation [0] and LLVM's 14fe6060f1SDimitry Andric * pre-exsiting SHA1 code [1]. 15fe6060f1SDimitry Andric * 16fe6060f1SDimitry Andric * [0] https://hyperelliptic.org/nacl/nacl-20110221.tar.bz2 (public domain 17fe6060f1SDimitry Andric * code) 18fe6060f1SDimitry Andric * [1] llvm/lib/Support/SHA1.{h,cpp} 19fe6060f1SDimitry Andric */ 20fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 21fe6060f1SDimitry Andric 22fe6060f1SDimitry Andric #include "llvm/Support/SHA256.h" 23fe6060f1SDimitry Andric #include "llvm/ADT/ArrayRef.h" 24fe6060f1SDimitry Andric #include "llvm/ADT/StringRef.h" 25fe6060f1SDimitry Andric #include "llvm/Support/Endian.h" 26fe6060f1SDimitry Andric #include "llvm/Support/Host.h" 27fe6060f1SDimitry Andric #include <string.h> 28fe6060f1SDimitry Andric 29fe6060f1SDimitry Andric namespace llvm { 30fe6060f1SDimitry Andric 31fe6060f1SDimitry Andric #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN 32fe6060f1SDimitry Andric #define SHA_BIG_ENDIAN 33fe6060f1SDimitry Andric #endif 34fe6060f1SDimitry Andric 35fe6060f1SDimitry Andric #define SHR(x, c) ((x) >> (c)) 36fe6060f1SDimitry Andric #define ROTR(x, n) (((x) >> n) | ((x) << (32 - (n)))) 37fe6060f1SDimitry Andric 38fe6060f1SDimitry Andric #define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z))) 39fe6060f1SDimitry Andric #define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) 40fe6060f1SDimitry Andric 41fe6060f1SDimitry Andric #define SIGMA_0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) 42fe6060f1SDimitry Andric #define SIGMA_1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) 43fe6060f1SDimitry Andric 44fe6060f1SDimitry Andric #define SIGMA_2(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10)) 45fe6060f1SDimitry Andric #define SIGMA_3(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3)) 46fe6060f1SDimitry Andric 47fe6060f1SDimitry Andric #define F_EXPAND(A, B, C, D, E, F, G, H, M1, M2, M3, M4, k) \ 48fe6060f1SDimitry Andric do { \ 49fe6060f1SDimitry Andric H += SIGMA_1(E) + CH(E, F, G) + M1 + k; \ 50fe6060f1SDimitry Andric D += H; \ 51fe6060f1SDimitry Andric H += SIGMA_0(A) + MAJ(A, B, C); \ 52fe6060f1SDimitry Andric M1 += SIGMA_2(M2) + M3 + SIGMA_3(M4); \ 53fe6060f1SDimitry Andric } while (0); 54fe6060f1SDimitry Andric 55fe6060f1SDimitry Andric void SHA256::init() { 56fe6060f1SDimitry Andric InternalState.State[0] = 0x6A09E667; 57fe6060f1SDimitry Andric InternalState.State[1] = 0xBB67AE85; 58fe6060f1SDimitry Andric InternalState.State[2] = 0x3C6EF372; 59fe6060f1SDimitry Andric InternalState.State[3] = 0xA54FF53A; 60fe6060f1SDimitry Andric InternalState.State[4] = 0x510E527F; 61fe6060f1SDimitry Andric InternalState.State[5] = 0x9B05688C; 62fe6060f1SDimitry Andric InternalState.State[6] = 0x1F83D9AB; 63fe6060f1SDimitry Andric InternalState.State[7] = 0x5BE0CD19; 64fe6060f1SDimitry Andric InternalState.ByteCount = 0; 65fe6060f1SDimitry Andric InternalState.BufferOffset = 0; 66fe6060f1SDimitry Andric } 67fe6060f1SDimitry Andric 68fe6060f1SDimitry Andric void SHA256::hashBlock() { 69fe6060f1SDimitry Andric uint32_t A = InternalState.State[0]; 70fe6060f1SDimitry Andric uint32_t B = InternalState.State[1]; 71fe6060f1SDimitry Andric uint32_t C = InternalState.State[2]; 72fe6060f1SDimitry Andric uint32_t D = InternalState.State[3]; 73fe6060f1SDimitry Andric uint32_t E = InternalState.State[4]; 74fe6060f1SDimitry Andric uint32_t F = InternalState.State[5]; 75fe6060f1SDimitry Andric uint32_t G = InternalState.State[6]; 76fe6060f1SDimitry Andric uint32_t H = InternalState.State[7]; 77fe6060f1SDimitry Andric 78fe6060f1SDimitry Andric uint32_t W00 = InternalState.Buffer.L[0]; 79fe6060f1SDimitry Andric uint32_t W01 = InternalState.Buffer.L[1]; 80fe6060f1SDimitry Andric uint32_t W02 = InternalState.Buffer.L[2]; 81fe6060f1SDimitry Andric uint32_t W03 = InternalState.Buffer.L[3]; 82fe6060f1SDimitry Andric uint32_t W04 = InternalState.Buffer.L[4]; 83fe6060f1SDimitry Andric uint32_t W05 = InternalState.Buffer.L[5]; 84fe6060f1SDimitry Andric uint32_t W06 = InternalState.Buffer.L[6]; 85fe6060f1SDimitry Andric uint32_t W07 = InternalState.Buffer.L[7]; 86fe6060f1SDimitry Andric uint32_t W08 = InternalState.Buffer.L[8]; 87fe6060f1SDimitry Andric uint32_t W09 = InternalState.Buffer.L[9]; 88fe6060f1SDimitry Andric uint32_t W10 = InternalState.Buffer.L[10]; 89fe6060f1SDimitry Andric uint32_t W11 = InternalState.Buffer.L[11]; 90fe6060f1SDimitry Andric uint32_t W12 = InternalState.Buffer.L[12]; 91fe6060f1SDimitry Andric uint32_t W13 = InternalState.Buffer.L[13]; 92fe6060f1SDimitry Andric uint32_t W14 = InternalState.Buffer.L[14]; 93fe6060f1SDimitry Andric uint32_t W15 = InternalState.Buffer.L[15]; 94fe6060f1SDimitry Andric 95fe6060f1SDimitry Andric F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x428A2F98); 96fe6060f1SDimitry Andric F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x71374491); 97fe6060f1SDimitry Andric F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xB5C0FBCF); 98fe6060f1SDimitry Andric F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xE9B5DBA5); 99fe6060f1SDimitry Andric F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x3956C25B); 100fe6060f1SDimitry Andric F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x59F111F1); 101fe6060f1SDimitry Andric F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x923F82A4); 102fe6060f1SDimitry Andric F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0xAB1C5ED5); 103fe6060f1SDimitry Andric F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xD807AA98); 104fe6060f1SDimitry Andric F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x12835B01); 105fe6060f1SDimitry Andric F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x243185BE); 106fe6060f1SDimitry Andric F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x550C7DC3); 107fe6060f1SDimitry Andric F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x72BE5D74); 108fe6060f1SDimitry Andric F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x80DEB1FE); 109fe6060f1SDimitry Andric F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x9BDC06A7); 110fe6060f1SDimitry Andric F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC19BF174); 111fe6060f1SDimitry Andric 112fe6060f1SDimitry Andric F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xE49B69C1); 113fe6060f1SDimitry Andric F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xEFBE4786); 114fe6060f1SDimitry Andric F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x0FC19DC6); 115fe6060f1SDimitry Andric F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x240CA1CC); 116fe6060f1SDimitry Andric F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x2DE92C6F); 117fe6060f1SDimitry Andric F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4A7484AA); 118fe6060f1SDimitry Andric F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5CB0A9DC); 119fe6060f1SDimitry Andric F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x76F988DA); 120fe6060f1SDimitry Andric F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x983E5152); 121fe6060f1SDimitry Andric F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA831C66D); 122fe6060f1SDimitry Andric F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xB00327C8); 123fe6060f1SDimitry Andric F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xBF597FC7); 124fe6060f1SDimitry Andric F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xC6E00BF3); 125fe6060f1SDimitry Andric F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD5A79147); 126fe6060f1SDimitry Andric F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x06CA6351); 127fe6060f1SDimitry Andric F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x14292967); 128fe6060f1SDimitry Andric 129fe6060f1SDimitry Andric F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x27B70A85); 130fe6060f1SDimitry Andric F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x2E1B2138); 131fe6060f1SDimitry Andric F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x4D2C6DFC); 132fe6060f1SDimitry Andric F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x53380D13); 133fe6060f1SDimitry Andric F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x650A7354); 134fe6060f1SDimitry Andric F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x766A0ABB); 135fe6060f1SDimitry Andric F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x81C2C92E); 136fe6060f1SDimitry Andric F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x92722C85); 137fe6060f1SDimitry Andric F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xA2BFE8A1); 138fe6060f1SDimitry Andric F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA81A664B); 139fe6060f1SDimitry Andric F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xC24B8B70); 140fe6060f1SDimitry Andric F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xC76C51A3); 141fe6060f1SDimitry Andric F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xD192E819); 142fe6060f1SDimitry Andric F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD6990624); 143fe6060f1SDimitry Andric F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xF40E3585); 144fe6060f1SDimitry Andric F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x106AA070); 145fe6060f1SDimitry Andric 146fe6060f1SDimitry Andric F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x19A4C116); 147fe6060f1SDimitry Andric F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x1E376C08); 148fe6060f1SDimitry Andric F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x2748774C); 149fe6060f1SDimitry Andric F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x34B0BCB5); 150fe6060f1SDimitry Andric F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x391C0CB3); 151fe6060f1SDimitry Andric F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4ED8AA4A); 152fe6060f1SDimitry Andric F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5B9CCA4F); 153fe6060f1SDimitry Andric F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x682E6FF3); 154fe6060f1SDimitry Andric F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x748F82EE); 155fe6060f1SDimitry Andric F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x78A5636F); 156fe6060f1SDimitry Andric F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x84C87814); 157fe6060f1SDimitry Andric F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x8CC70208); 158fe6060f1SDimitry Andric F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x90BEFFFA); 159fe6060f1SDimitry Andric F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xA4506CEB); 160fe6060f1SDimitry Andric F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xBEF9A3F7); 161fe6060f1SDimitry Andric F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC67178F2); 162fe6060f1SDimitry Andric 163fe6060f1SDimitry Andric InternalState.State[0] += A; 164fe6060f1SDimitry Andric InternalState.State[1] += B; 165fe6060f1SDimitry Andric InternalState.State[2] += C; 166fe6060f1SDimitry Andric InternalState.State[3] += D; 167fe6060f1SDimitry Andric InternalState.State[4] += E; 168fe6060f1SDimitry Andric InternalState.State[5] += F; 169fe6060f1SDimitry Andric InternalState.State[6] += G; 170fe6060f1SDimitry Andric InternalState.State[7] += H; 171fe6060f1SDimitry Andric } 172fe6060f1SDimitry Andric 173fe6060f1SDimitry Andric void SHA256::addUncounted(uint8_t Data) { 174fe6060f1SDimitry Andric #ifdef SHA_BIG_ENDIAN 175fe6060f1SDimitry Andric InternalState.Buffer.C[InternalState.BufferOffset] = Data; 176fe6060f1SDimitry Andric #else 177fe6060f1SDimitry Andric InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data; 178fe6060f1SDimitry Andric #endif 179fe6060f1SDimitry Andric 180fe6060f1SDimitry Andric InternalState.BufferOffset++; 181fe6060f1SDimitry Andric if (InternalState.BufferOffset == BLOCK_LENGTH) { 182fe6060f1SDimitry Andric hashBlock(); 183fe6060f1SDimitry Andric InternalState.BufferOffset = 0; 184fe6060f1SDimitry Andric } 185fe6060f1SDimitry Andric } 186fe6060f1SDimitry Andric 187fe6060f1SDimitry Andric void SHA256::writebyte(uint8_t Data) { 188fe6060f1SDimitry Andric ++InternalState.ByteCount; 189fe6060f1SDimitry Andric addUncounted(Data); 190fe6060f1SDimitry Andric } 191fe6060f1SDimitry Andric 192fe6060f1SDimitry Andric void SHA256::update(ArrayRef<uint8_t> Data) { 193fe6060f1SDimitry Andric InternalState.ByteCount += Data.size(); 194fe6060f1SDimitry Andric 195fe6060f1SDimitry Andric // Finish the current block. 196fe6060f1SDimitry Andric if (InternalState.BufferOffset > 0) { 197fe6060f1SDimitry Andric const size_t Remainder = std::min<size_t>( 198fe6060f1SDimitry Andric Data.size(), BLOCK_LENGTH - InternalState.BufferOffset); 199fe6060f1SDimitry Andric for (size_t I = 0; I < Remainder; ++I) 200fe6060f1SDimitry Andric addUncounted(Data[I]); 201fe6060f1SDimitry Andric Data = Data.drop_front(Remainder); 202fe6060f1SDimitry Andric } 203fe6060f1SDimitry Andric 204fe6060f1SDimitry Andric // Fast buffer filling for large inputs. 205fe6060f1SDimitry Andric while (Data.size() >= BLOCK_LENGTH) { 206fe6060f1SDimitry Andric assert(InternalState.BufferOffset == 0); 207fe6060f1SDimitry Andric static_assert(BLOCK_LENGTH % 4 == 0, ""); 208fe6060f1SDimitry Andric constexpr size_t BLOCK_LENGTH_32 = BLOCK_LENGTH / 4; 209fe6060f1SDimitry Andric for (size_t I = 0; I < BLOCK_LENGTH_32; ++I) 210fe6060f1SDimitry Andric InternalState.Buffer.L[I] = support::endian::read32be(&Data[I * 4]); 211fe6060f1SDimitry Andric hashBlock(); 212fe6060f1SDimitry Andric Data = Data.drop_front(BLOCK_LENGTH); 213fe6060f1SDimitry Andric } 214fe6060f1SDimitry Andric 215fe6060f1SDimitry Andric // Finish the remainder. 216fe6060f1SDimitry Andric for (uint8_t C : Data) 217fe6060f1SDimitry Andric addUncounted(C); 218fe6060f1SDimitry Andric } 219fe6060f1SDimitry Andric 220fe6060f1SDimitry Andric void SHA256::update(StringRef Str) { 221fe6060f1SDimitry Andric update( 222fe6060f1SDimitry Andric ArrayRef<uint8_t>((uint8_t *)const_cast<char *>(Str.data()), Str.size())); 223fe6060f1SDimitry Andric } 224fe6060f1SDimitry Andric 225fe6060f1SDimitry Andric void SHA256::pad() { 226fe6060f1SDimitry Andric // Implement SHA-2 padding (fips180-2 5.1.1) 227fe6060f1SDimitry Andric 228fe6060f1SDimitry Andric // Pad with 0x80 followed by 0x00 until the end of the block 229fe6060f1SDimitry Andric addUncounted(0x80); 230fe6060f1SDimitry Andric while (InternalState.BufferOffset != 56) 231fe6060f1SDimitry Andric addUncounted(0x00); 232fe6060f1SDimitry Andric 233fe6060f1SDimitry Andric uint64_t len = InternalState.ByteCount << 3; // bit size 234fe6060f1SDimitry Andric 235fe6060f1SDimitry Andric // Append length in the last 8 bytes big edian encoded 236fe6060f1SDimitry Andric addUncounted(len >> 56); 237fe6060f1SDimitry Andric addUncounted(len >> 48); 238fe6060f1SDimitry Andric addUncounted(len >> 40); 239fe6060f1SDimitry Andric addUncounted(len >> 32); 240fe6060f1SDimitry Andric addUncounted(len >> 24); 241fe6060f1SDimitry Andric addUncounted(len >> 16); 242fe6060f1SDimitry Andric addUncounted(len >> 8); 243fe6060f1SDimitry Andric addUncounted(len); 244fe6060f1SDimitry Andric } 245fe6060f1SDimitry Andric 246*81ad6265SDimitry Andric void SHA256::final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult) { 247fe6060f1SDimitry Andric // Pad to complete the last block 248fe6060f1SDimitry Andric pad(); 249fe6060f1SDimitry Andric 250fe6060f1SDimitry Andric #ifdef SHA_BIG_ENDIAN 251fe6060f1SDimitry Andric // Just copy the current state 252fe6060f1SDimitry Andric for (int i = 0; i < 8; i++) { 253fe6060f1SDimitry Andric HashResult[i] = InternalState.State[i]; 254fe6060f1SDimitry Andric } 255fe6060f1SDimitry Andric #else 256fe6060f1SDimitry Andric // Swap byte order back 257fe6060f1SDimitry Andric for (int i = 0; i < 8; i++) { 258fe6060f1SDimitry Andric HashResult[i] = (((InternalState.State[i]) << 24) & 0xff000000) | 259fe6060f1SDimitry Andric (((InternalState.State[i]) << 8) & 0x00ff0000) | 260fe6060f1SDimitry Andric (((InternalState.State[i]) >> 8) & 0x0000ff00) | 261fe6060f1SDimitry Andric (((InternalState.State[i]) >> 24) & 0x000000ff); 262fe6060f1SDimitry Andric } 263fe6060f1SDimitry Andric #endif 264fe6060f1SDimitry Andric } 265fe6060f1SDimitry Andric 266*81ad6265SDimitry Andric std::array<uint8_t, 32> SHA256::final() { 267*81ad6265SDimitry Andric union { 268*81ad6265SDimitry Andric std::array<uint32_t, HASH_LENGTH / 4> HashResult; 269*81ad6265SDimitry Andric std::array<uint8_t, HASH_LENGTH> ReturnResult; 270*81ad6265SDimitry Andric }; 271*81ad6265SDimitry Andric static_assert(sizeof(HashResult) == sizeof(ReturnResult), ""); 272*81ad6265SDimitry Andric final(HashResult); 273*81ad6265SDimitry Andric return ReturnResult; 274*81ad6265SDimitry Andric } 275*81ad6265SDimitry Andric 276*81ad6265SDimitry Andric std::array<uint8_t, 32> SHA256::result() { 277fe6060f1SDimitry Andric auto StateToRestore = InternalState; 278fe6060f1SDimitry Andric 279fe6060f1SDimitry Andric auto Hash = final(); 280fe6060f1SDimitry Andric 281fe6060f1SDimitry Andric // Restore the state 282fe6060f1SDimitry Andric InternalState = StateToRestore; 283fe6060f1SDimitry Andric 284fe6060f1SDimitry Andric // Return pointer to hash (32 characters) 285fe6060f1SDimitry Andric return Hash; 286fe6060f1SDimitry Andric } 287fe6060f1SDimitry Andric 288fe6060f1SDimitry Andric std::array<uint8_t, 32> SHA256::hash(ArrayRef<uint8_t> Data) { 289fe6060f1SDimitry Andric SHA256 Hash; 290fe6060f1SDimitry Andric Hash.update(Data); 291*81ad6265SDimitry Andric return Hash.final(); 292fe6060f1SDimitry Andric } 293fe6060f1SDimitry Andric 294fe6060f1SDimitry Andric } // namespace llvm 295