LLVM 22.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
19#include <array>
20#include <cstdint>
21
22namespace llvm {
23template <typename T> class ArrayRef;
24class StringRef;
25
26/// A class that wrap the SHA1 algorithm.
27class SHA1 {
28public:
29 SHA1() { init(); }
30
31 /// Reinitialize the internal state
32 LLVM_ABI void init();
33
34 /// Digest more data.
36
37 /// Digest more data.
38 LLVM_ABI void update(StringRef Str);
39
40 /// Return the current raw 160-bits SHA1 for the digested data
41 /// since the last call to init(). This call will add data to the internal
42 /// state and as such is not suited for getting an intermediate result
43 /// (see result()).
44 LLVM_ABI std::array<uint8_t, 20> final();
45
46 /// Return the current raw 160-bits SHA1 for the digested data
47 /// since the last call to init(). This is suitable for getting the SHA1 at
48 /// any time without invalidating the internal state so that more calls can be
49 /// made into update.
50 LLVM_ABI std::array<uint8_t, 20> result();
51
52 /// Returns a raw 160-bit SHA1 hash for the given data.
53 LLVM_ABI static std::array<uint8_t, 20> hash(ArrayRef<uint8_t> Data);
54
55private:
56 /// Define some constants.
57 /// "static constexpr" would be cleaner but MSVC does not support it yet.
58 enum { BLOCK_LENGTH = 64 };
59 enum { HASH_LENGTH = 20 };
60
61 // Internal State
62 struct {
63 union {
64 uint8_t C[BLOCK_LENGTH];
65 uint32_t L[BLOCK_LENGTH / 4];
67 uint32_t State[HASH_LENGTH / 4];
70 } InternalState;
71
72 // Helper
73 void writebyte(uint8_t data);
74 void hashBlock();
75 void addUncounted(uint8_t data);
76 void pad();
77
78 void final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult);
79};
80
81} // end llvm namespace
82
83#endif
#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
A class that wrap the SHA1 algorithm.
Definition: SHA1.h:27
SHA1()
Definition: SHA1.h:29
LLVM_ABI void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition: SHA1.cpp:208
uint8_t C[BLOCK_LENGTH]
Definition: SHA1.h:64
static LLVM_ABI std::array< uint8_t, 20 > hash(ArrayRef< uint8_t > Data)
Returns a raw 160-bit SHA1 hash for the given data.
Definition: SHA1.cpp:300
LLVM_ABI std::array< uint8_t, 20 > result()
Return the current raw 160-bits SHA1 for the digested data since the last call to init().
Definition: SHA1.cpp:288
uint32_t ByteCount
Definition: SHA1.h:68
LLVM_ABI void init()
Reinitialize the internal state.
Definition: SHA1.cpp:81
uint32_t State[HASH_LENGTH/4]
Definition: SHA1.h:67
uint32_t L[BLOCK_LENGTH/4]
Definition: SHA1.h:65
uint8_t BufferOffset
Definition: SHA1.h:69
union llvm::SHA1::@426::@427 Buffer
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 >