diff options
Diffstat (limited to 'llvm/include')
| -rw-r--r-- | llvm/include/llvm/Support/SHA1.h | 75 | ||||
| -rw-r--r-- | llvm/include/llvm/Support/raw_sha1_ostream.h | 46 |
2 files changed, 121 insertions, 0 deletions
diff --git a/llvm/include/llvm/Support/SHA1.h b/llvm/include/llvm/Support/SHA1.h new file mode 100644 index 00000000000..57af39ab844 --- /dev/null +++ b/llvm/include/llvm/Support/SHA1.h @@ -0,0 +1,75 @@ +//==- SHA1.h - SHA1 implementation for LLVM --*- C++ -*-==// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// This code is taken from public domain +// (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c) +// and modified by wrapping it in a C++ interface for LLVM, +// and removing unnecessary code. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_SHA1_H +#define LLVM_SUPPORT_SHA1_H + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/StringRef.h" + +#include <cstdint> + +namespace llvm { + +/// A class that wrap the SHA1 algorithm. +class SHA1 { +public: + SHA1() { init(); } + + /// Reinitialize the internal state + void init(); + + /// Digest more data. + void update(ArrayRef<uint8_t> Data); + + /// Return a reference to the current raw 160-bits SHA1 for the digested data + /// since the last call to init(). This call will add data to the internal + /// state and as such is not suited for getting an intermediate result + /// (see result()). + StringRef final(); + + /// Return a reference to the current raw 160-bits SHA1 for the digested data + /// since the last call to init(). This is suitable for getting the SHA1 at + /// any time without invalidating the internal state so that more calls can be + /// made into update. + StringRef result(); + +private: + /// Define some constants. + /// "static constexpr" would be cleaner but MSVC does not support it yet. + enum { BLOCK_LENGTH = 64 }; + enum { HASH_LENGTH = 20 }; + + // Internal State + struct { + uint32_t Buffer[BLOCK_LENGTH / 4]; + uint32_t State[HASH_LENGTH / 4]; + uint32_t ByteCount; + uint8_t BufferOffset; + } InternalState; + + // Internal copy of the hash, populated and accessed on calls to result() + uint32_t HashResult[HASH_LENGTH / 4]; + + // Helper + void writebyte(uint8_t data); + void hashBlock(); + void addUncounted(uint8_t data); + void pad(); +}; + +} // end llvm namespace + +#endif diff --git a/llvm/include/llvm/Support/raw_sha1_ostream.h b/llvm/include/llvm/Support/raw_sha1_ostream.h new file mode 100644 index 00000000000..9f136dfa7e1 --- /dev/null +++ b/llvm/include/llvm/Support/raw_sha1_ostream.h @@ -0,0 +1,46 @@ +//==- raw_sha1_ostream.h - raw_ostream that compute SHA1 --*- C++ -*-==// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the raw_sha1_ostream class. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_RAW_SHA1_OSTREAM_H +#define LLVM_SUPPORT_RAW_SHA1_OSTREAM_H + +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/SHA1.h" + +namespace llvm { + +/// A raw_ostream that hash the content using the sha1 algorithm. +class raw_sha1_ostream : public raw_ostream { + SHA1 State; + + /// See raw_ostream::write_impl. + void write_impl(const char *Ptr, size_t Size) override { + State.update(ArrayRef<uint8_t>((uint8_t *)Ptr, Size)); + } + +public: + /// Return the current SHA1 hash for the content of the stream + StringRef sha1() { + flush(); + return State.result(); + } + + /// Reset the internal state to start over from scratch. + void resetHash() { State.init(); } + + uint64_t current_pos() const override { return 0; } +}; + +} // end llvm namespace + +#endif |

