LLVM  14.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 
28 namespace llvm {
29 
30 template <typename T> class ArrayRef;
31 class StringRef;
32 
33 class SHA256 {
34 public:
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 a reference to 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  StringRef final();
51 
52  /// Return a reference to 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  StringRef 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 
61 private:
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];
72  } Buffer;
73  uint32_t State[HASH_LENGTH / 4];
75  uint8_t BufferOffset;
76  } InternalState;
77 
78  // Internal copy of the hash, populated and accessed on calls to result()
79  uint32_t HashResult[HASH_LENGTH / 4];
80 
81  // Helper
82  void writebyte(uint8_t data);
83  void hashBlock();
84  void addUncounted(uint8_t data);
85  void pad();
86 };
87 
88 } // namespace llvm
89 
90 #endif // LLVM_SUPPORT_SHA256_H
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::SHA256::init
void init()
Reinitialize the internal state.
Definition: SHA256.cpp:55
llvm::SHA256
Definition: SHA256.h:33
llvm::SHA256::Buffer
union llvm::SHA256::@366::@367 Buffer
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::SHA256::BufferOffset
uint8_t BufferOffset
Definition: SHA256.h:75
llvm::SHA256::SHA256
SHA256()
Definition: SHA256.h:35
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::SHA256::result
StringRef result()
Return a reference to the current raw 256-bits SHA256 for the digested data since the last call to in...
Definition: SHA256.cpp:269
llvm::SHA256::L
uint32_t L[BLOCK_LENGTH/4]
Definition: SHA256.h:71
llvm::ArrayRef< uint8_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
llvm::SHA256::update
void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition: SHA256.cpp:192
llvm::SHA256::ByteCount
uint32_t ByteCount
Definition: SHA256.h:74
llvm::SHA256::hash
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:281
llvm::SHA256::State
uint32_t State[HASH_LENGTH/4]
Definition: SHA256.h:73