LLVM 22.0.0git
SHA256.h
Go to the documentation of this file.
1//====- SHA256.cpp - SHA256 implementation ---*- C++ -* ======//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/*
9 * The SHA-256 Secure Hash Standard was published by NIST in 2002.
10 *
11 * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
12 *
13 * The implementation is based on nacl's sha256 implementation [0] and LLVM's
14 * pre-exsiting SHA1 code [1].
15 *
16 * [0] https://hyperelliptic.org/nacl/nacl-20110221.tar.bz2 (public domain
17 * code)
18 * [1] llvm/lib/Support/SHA1.{h,cpp}
19 */
20//===----------------------------------------------------------------------===//
21
22#ifndef LLVM_SUPPORT_SHA256_H
23#define LLVM_SUPPORT_SHA256_H
24
26#include <array>
27#include <cstdint>
28
29namespace llvm {
30
31template <typename T> class ArrayRef;
32class StringRef;
33
34class SHA256 {
35public:
36 explicit SHA256() { init(); }
37
38 /// Reinitialize the internal state
39 LLVM_ABI void init();
40
41 /// Digest more data.
43
44 /// Digest more data.
45 LLVM_ABI void update(StringRef Str);
46
47 /// Return the current raw 256-bits SHA256 for the digested
48 /// data since the last call to init(). This call will add data to the
49 /// internal state and as such is not suited for getting an intermediate
50 /// result (see result()).
51 LLVM_ABI std::array<uint8_t, 32> final();
52
53 /// Return the current raw 256-bits SHA256 for the digested
54 /// data since the last call to init(). This is suitable for getting the
55 /// SHA256 at any time without invalidating the internal state so that more
56 /// calls can be made into update.
57 LLVM_ABI std::array<uint8_t, 32> result();
58
59 /// Returns a raw 256-bit SHA256 hash for the given data.
60 LLVM_ABI static std::array<uint8_t, 32> hash(ArrayRef<uint8_t> Data);
61
62private:
63 /// Define some constants.
64 /// "static constexpr" would be cleaner but MSVC does not support it yet.
65 enum { BLOCK_LENGTH = 64 };
66 enum { HASH_LENGTH = 32 };
67
68 // Internal State
69 struct {
70 union {
71 uint8_t C[BLOCK_LENGTH];
72 uint32_t L[BLOCK_LENGTH / 4];
74 uint32_t State[HASH_LENGTH / 4];
77 } InternalState;
78
79 // Helper
80 void writebyte(uint8_t data);
81 void hashBlock();
82 void addUncounted(uint8_t data);
83 void pad();
84
85 void final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult);
86};
87
88} // namespace llvm
89
90#endif // LLVM_SUPPORT_SHA256_H
#define LLVM_ABI
Definition: Compiler.h:213
static Split data
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM_ABI void init()
Reinitialize the internal state.
Definition: SHA256.cpp:51
uint32_t State[HASH_LENGTH/4]
Definition: SHA256.h:74
uint8_t C[BLOCK_LENGTH]
Definition: SHA256.h:71
static LLVM_ABI std::array< uint8_t, 32 > hash(ArrayRef< uint8_t > Data)
Returns a raw 256-bit SHA256 hash for the given data.
Definition: SHA256.cpp:280
LLVM_ABI void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition: SHA256.cpp:187
LLVM_ABI std::array< uint8_t, 32 > result()
Return the current raw 256-bits SHA256 for the digested data since the last call to init().
Definition: SHA256.cpp:268
union llvm::SHA256::@430::@431 Buffer
uint8_t BufferOffset
Definition: SHA256.h:76
uint32_t ByteCount
Definition: SHA256.h:75
uint32_t L[BLOCK_LENGTH/4]
Definition: SHA256.h:72
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ArrayRef(const T &OneElt) -> ArrayRef< T >