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