|
|
|||
File indexing completed on 2026-05-10 08:44:34
0001 //==- SHA1.h - SHA1 implementation for LLVM --*- C++ -*-==// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // This code is taken from public domain 0009 // (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c) 0010 // and modified by wrapping it in a C++ interface for LLVM, 0011 // and removing unnecessary code. 0012 // 0013 //===----------------------------------------------------------------------===// 0014 0015 #ifndef LLVM_SUPPORT_SHA1_H 0016 #define LLVM_SUPPORT_SHA1_H 0017 0018 #include <array> 0019 #include <cstdint> 0020 0021 namespace llvm { 0022 template <typename T> class ArrayRef; 0023 class StringRef; 0024 0025 /// A class that wrap the SHA1 algorithm. 0026 class SHA1 { 0027 public: 0028 SHA1() { init(); } 0029 0030 /// Reinitialize the internal state 0031 void init(); 0032 0033 /// Digest more data. 0034 void update(ArrayRef<uint8_t> Data); 0035 0036 /// Digest more data. 0037 void update(StringRef Str); 0038 0039 /// Return the current raw 160-bits SHA1 for the digested data 0040 /// since the last call to init(). This call will add data to the internal 0041 /// state and as such is not suited for getting an intermediate result 0042 /// (see result()). 0043 std::array<uint8_t, 20> final(); 0044 0045 /// Return the current raw 160-bits SHA1 for the digested data 0046 /// since the last call to init(). This is suitable for getting the SHA1 at 0047 /// any time without invalidating the internal state so that more calls can be 0048 /// made into update. 0049 std::array<uint8_t, 20> result(); 0050 0051 /// Returns a raw 160-bit SHA1 hash for the given data. 0052 static std::array<uint8_t, 20> hash(ArrayRef<uint8_t> Data); 0053 0054 private: 0055 /// Define some constants. 0056 /// "static constexpr" would be cleaner but MSVC does not support it yet. 0057 enum { BLOCK_LENGTH = 64 }; 0058 enum { HASH_LENGTH = 20 }; 0059 0060 // Internal State 0061 struct { 0062 union { 0063 uint8_t C[BLOCK_LENGTH]; 0064 uint32_t L[BLOCK_LENGTH / 4]; 0065 } Buffer; 0066 uint32_t State[HASH_LENGTH / 4]; 0067 uint32_t ByteCount; 0068 uint8_t BufferOffset; 0069 } InternalState; 0070 0071 // Helper 0072 void writebyte(uint8_t data); 0073 void hashBlock(); 0074 void addUncounted(uint8_t data); 0075 void pad(); 0076 0077 void final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult); 0078 }; 0079 0080 } // end llvm namespace 0081 0082 #endif
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|