LLVM 20.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
25#include <array>
26#include <cstdint>
27
28namespace llvm {
29
30template <typename T> class ArrayRef;
31class StringRef;
32
33class SHA256 {
34public:
35 explicit SHA256() { init(); }
36
37 /// Reinitialize the internal state
38 void init();
39
40 /// Digest more data.
42
43 /// Digest more data.
44 void update(StringRef Str);
45
46 /// Return the current raw 256-bits SHA256 for the digested
47 /// data since the last call to init(). This call will add data to the
48 /// internal state and as such is not suited for getting an intermediate
49 /// result (see result()).
50 std::array<uint8_t, 32> final();
51
52 /// Return the current raw 256-bits SHA256 for the digested
53 /// data since the last call to init(). This is suitable for getting the
54 /// SHA256 at any time without invalidating the internal state so that more
55 /// calls can be made into update.
56 std::array<uint8_t, 32> result();
57
58 /// Returns a raw 256-bit SHA256 hash for the given data.
59 static std::array<uint8_t, 32> hash(ArrayRef<uint8_t> Data);
60
61private:
62 /// Define some constants.
63 /// "static constexpr" would be cleaner but MSVC does not support it yet.
64 enum { BLOCK_LENGTH = 64 };
65 enum { HASH_LENGTH = 32 };
66
67 // Internal State
68 struct {
69 union {
70 uint8_t C[BLOCK_LENGTH];
71 uint32_t L[BLOCK_LENGTH / 4];
73 uint32_t State[HASH_LENGTH / 4];
75 uint8_t BufferOffset;
76 } InternalState;
77
78 // Helper
79 void writebyte(uint8_t data);
80 void hashBlock();
81 void addUncounted(uint8_t data);
82 void pad();
83
84 void final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult);
85};
86
87} // namespace llvm
88
89#endif // LLVM_SUPPORT_SHA256_H
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
void init()
Reinitialize the internal state.
Definition: SHA256.cpp:51
uint32_t State[HASH_LENGTH/4]
Definition: SHA256.h:73
uint8_t C[BLOCK_LENGTH]
Definition: SHA256.h:70
static 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
void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition: SHA256.cpp:187
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
uint8_t BufferOffset
Definition: SHA256.h:75
uint32_t ByteCount
Definition: SHA256.h:74
uint32_t L[BLOCK_LENGTH/4]
Definition: SHA256.h:71
union llvm::SHA256::@420::@421 Buffer
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ArrayRef(const T &OneElt) -> ArrayRef< T >