LLVM  13.0.0git
SHA1.h
Go to the documentation of this file.
1 //==- SHA1.h - SHA1 implementation for LLVM --*- 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 // This code is taken from public domain
9 // (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c)
10 // and modified by wrapping it in a C++ interface for LLVM,
11 // and removing unnecessary code.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_SUPPORT_SHA1_H
16 #define LLVM_SUPPORT_SHA1_H
17 
18 #include <array>
19 #include <cstdint>
20 
21 namespace llvm {
22 template <typename T> class ArrayRef;
23 class StringRef;
24 
25 /// A class that wrap the SHA1 algorithm.
26 class SHA1 {
27 public:
28  SHA1() { init(); }
29 
30  /// Reinitialize the internal state
31  void init();
32 
33  /// Digest more data.
35 
36  /// Digest more data.
37  void update(StringRef Str);
38 
39  /// Return a reference to the current raw 160-bits SHA1 for the digested data
40  /// since the last call to init(). This call will add data to the internal
41  /// state and as such is not suited for getting an intermediate result
42  /// (see result()).
43  StringRef final();
44 
45  /// Return a reference to the current raw 160-bits SHA1 for the digested data
46  /// since the last call to init(). This is suitable for getting the SHA1 at
47  /// any time without invalidating the internal state so that more calls can be
48  /// made into update.
49  StringRef result();
50 
51  /// Returns a raw 160-bit SHA1 hash for the given data.
52  static std::array<uint8_t, 20> hash(ArrayRef<uint8_t> Data);
53 
54 private:
55  /// Define some constants.
56  /// "static constexpr" would be cleaner but MSVC does not support it yet.
57  enum { BLOCK_LENGTH = 64 };
58  enum { HASH_LENGTH = 20 };
59 
60  // Internal State
61  struct {
62  union {
63  uint8_t C[BLOCK_LENGTH];
64  uint32_t L[BLOCK_LENGTH / 4];
65  } Buffer;
66  uint32_t State[HASH_LENGTH / 4];
68  uint8_t BufferOffset;
69  } InternalState;
70 
71  // Internal copy of the hash, populated and accessed on calls to result()
72  uint32_t HashResult[HASH_LENGTH / 4];
73 
74  // Helper
75  void writebyte(uint8_t data);
76  void hashBlock();
77  void addUncounted(uint8_t data);
78  void pad();
79 };
80 
81 } // end llvm namespace
82 
83 #endif
llvm
Definition: AllocatorList.h:23
llvm::SHA1::ByteCount
uint32_t ByteCount
Definition: SHA1.h:67
llvm::SHA1::SHA1
SHA1()
Definition: SHA1.h:28
llvm::SHA1::Buffer
union llvm::SHA1::@354::@355 Buffer
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::SHA1
A class that wrap the SHA1 algorithm.
Definition: SHA1.h:26
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::SHA1::hash
static std::array< uint8_t, 20 > hash(ArrayRef< uint8_t > Data)
Returns a raw 160-bit SHA1 hash for the given data.
Definition: SHA1.cpp:301
llvm::SHA1::State
uint32_t State[HASH_LENGTH/4]
Definition: SHA1.h:66
llvm::SHA1::init
void init()
Reinitialize the internal state.
Definition: SHA1.cpp:85
llvm::SHA1::update
void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition: SHA1.cpp:213
llvm::ArrayRef< uint8_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
llvm::SHA1::L
uint32_t L[BLOCK_LENGTH/4]
Definition: SHA1.h:64
llvm::SHA1::result
StringRef result()
Return a reference to the current raw 160-bits SHA1 for the digested data since the last call to init...
Definition: SHA1.cpp:289
llvm::SHA1::BufferOffset
uint8_t BufferOffset
Definition: SHA1.h:68