[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / include / llvm / Support / SHA1.h
blob87fe94bbd5cd36d0e183a0d4a93cdb8f2d1ed178
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_SHA1_H
16 #define LLVM_SUPPORT_SHA1_H
18 #include "llvm/ADT/ArrayRef.h"
20 #include <array>
21 #include <cstdint>
23 namespace llvm {
24 template <typename T> class ArrayRef;
25 class StringRef;
27 /// A class that wrap the SHA1 algorithm.
28 class SHA1 {
29 public:
30 SHA1() { init(); }
32 /// Reinitialize the internal state
33 void init();
35 /// Digest more data.
36 void update(ArrayRef<uint8_t> Data);
38 /// Digest more data.
39 void update(StringRef Str) {
40 update(ArrayRef<uint8_t>((uint8_t *)const_cast<char *>(Str.data()),
41 Str.size()));
44 /// Return a reference to the current raw 160-bits SHA1 for the digested data
45 /// since the last call to init(). This call will add data to the internal
46 /// state and as such is not suited for getting an intermediate result
47 /// (see result()).
48 StringRef final();
50 /// Return a reference to the current raw 160-bits SHA1 for the digested data
51 /// since the last call to init(). This is suitable for getting the SHA1 at
52 /// any time without invalidating the internal state so that more calls can be
53 /// made into update.
54 StringRef result();
56 /// Returns a raw 160-bit SHA1 hash for the given data.
57 static std::array<uint8_t, 20> hash(ArrayRef<uint8_t> Data);
59 private:
60 /// Define some constants.
61 /// "static constexpr" would be cleaner but MSVC does not support it yet.
62 enum { BLOCK_LENGTH = 64 };
63 enum { HASH_LENGTH = 20 };
65 // Internal State
66 struct {
67 union {
68 uint8_t C[BLOCK_LENGTH];
69 uint32_t L[BLOCK_LENGTH / 4];
70 } Buffer;
71 uint32_t State[HASH_LENGTH / 4];
72 uint32_t ByteCount;
73 uint8_t BufferOffset;
74 } InternalState;
76 // Internal copy of the hash, populated and accessed on calls to result()
77 uint32_t HashResult[HASH_LENGTH / 4];
79 // Helper
80 void writebyte(uint8_t data);
81 void hashBlock();
82 void addUncounted(uint8_t data);
83 void pad();
86 } // end llvm namespace
88 #endif