LLVM  14.0.0git
MD5.h
Go to the documentation of this file.
1 /* -*- C++ -*-
2  * This code is derived from (original license follows):
3  *
4  * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
5  * MD5 Message-Digest Algorithm (RFC 1321).
6  *
7  * Homepage:
8  * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
9  *
10  * Author:
11  * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
12  *
13  * This software was written by Alexander Peslyak in 2001. No copyright is
14  * claimed, and the software is hereby placed in the public domain.
15  * In case this attempt to disclaim copyright and place the software in the
16  * public domain is deemed null and void, then the software is
17  * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
18  * general public under the following terms:
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted.
22  *
23  * There's ABSOLUTELY NO WARRANTY, express or implied.
24  *
25  * See md5.c for more information.
26  */
27 
28 #ifndef LLVM_SUPPORT_MD5_H
29 #define LLVM_SUPPORT_MD5_H
30 
31 #include "llvm/ADT/StringRef.h"
32 #include "llvm/Support/Endian.h"
33 #include <array>
34 #include <cstdint>
35 
36 namespace llvm {
37 
38 template <unsigned N> class SmallString;
39 template <typename T> class ArrayRef;
40 
41 class MD5 {
42 public:
43  struct MD5Result {
44  std::array<uint8_t, 16> Bytes;
45 
46  operator std::array<uint8_t, 16>() const { return Bytes; }
47 
48  const uint8_t &operator[](size_t I) const { return Bytes[I]; }
49  uint8_t &operator[](size_t I) { return Bytes[I]; }
50 
51  SmallString<32> digest() const;
52 
53  uint64_t low() const {
54  // Our MD5 implementation returns the result in little endian, so the low
55  // word is first.
56  using namespace support;
57  return endian::read<uint64_t, little, unaligned>(Bytes.data());
58  }
59 
60  uint64_t high() const {
61  using namespace support;
62  return endian::read<uint64_t, little, unaligned>(Bytes.data() + 8);
63  }
64  std::pair<uint64_t, uint64_t> words() const {
65  using namespace support;
66  return std::make_pair(high(), low());
67  }
68  };
69 
70  MD5();
71 
72  /// Updates the hash for the byte stream provided.
74 
75  /// Updates the hash for the StringRef provided.
76  void update(StringRef Str);
77 
78  /// Finishes off the hash and puts the result in result.
79  void final(MD5Result &Result);
80 
81  /// Finishes off the hash, and returns a reference to the 16-byte hash data.
82  StringRef final();
83 
84  /// Finishes off the hash, and returns a reference to the 16-byte hash data.
85  /// This is suitable for getting the MD5 at any time without invalidating the
86  /// internal state, so that more calls can be made into `update`.
87  StringRef result();
88 
89  /// Translates the bytes in \p Res to a hex string that is
90  /// deposited into \p Str. The result will be of length 32.
91  static void stringifyResult(MD5Result &Result, SmallString<32> &Str);
92 
93  /// Computes the hash for a given bytes.
94  static std::array<uint8_t, 16> hash(ArrayRef<uint8_t> Data);
95 
96 private:
97  // Any 32-bit or wider unsigned integer data type will do.
98  typedef uint32_t MD5_u32plus;
99 
100  // Internal State
101  struct {
102  MD5_u32plus a = 0x67452301;
103  MD5_u32plus b = 0xefcdab89;
104  MD5_u32plus c = 0x98badcfe;
105  MD5_u32plus d = 0x10325476;
108  uint8_t buffer[64];
110  } InternalState;
111 
112  MD5Result Result;
113 
114  const uint8_t *body(ArrayRef<uint8_t> Data);
115 };
116 
117 inline bool operator==(const MD5::MD5Result &LHS, const MD5::MD5Result &RHS) {
118  return LHS.Bytes == RHS.Bytes;
119 }
120 
121 /// Helper to compute and return lower 64 bits of the given string's MD5 hash.
123  using namespace support;
124 
125  MD5 Hash;
126  Hash.update(Str);
127  MD5::MD5Result Result;
128  Hash.final(Result);
129  // Return the least significant word.
130  return Result.low();
131 }
132 
133 } // end namespace llvm
134 
135 #endif // LLVM_SUPPORT_MD5_H
llvm::MD5::b
MD5_u32plus b
Definition: MD5.h:103
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::MD5::update
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition: MD5.cpp:190
StringRef.h
llvm::MD5::stringifyResult
static void stringifyResult(MD5Result &Result, SmallString< 32 > &Str)
Translates the bytes in Res to a hex string that is deposited into Str.
Definition: MD5.cpp:290
llvm::MD5::MD5Result::low
uint64_t low() const
Definition: MD5.h:53
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::MD5::block
MD5_u32plus block[16]
Definition: MD5.h:109
llvm::MD5::MD5
MD5()
llvm::MD5::d
MD5_u32plus d
Definition: MD5.h:105
a
=0.0 ? 0.0 :(a > 0.0 ? 1.0 :-1.0) a
Definition: README.txt:489
llvm::MD5::hi
MD5_u32plus hi
Definition: MD5.h:106
llvm::MD5::final
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition: MD5.cpp:235
llvm::MD5::MD5Result::Bytes
std::array< uint8_t, 16 > Bytes
Definition: MD5.h:44
llvm::MD5::lo
MD5_u32plus lo
Definition: MD5.h:107
llvm::SmallString< 32 >
llvm::MD5::result
StringRef result()
Finishes off the hash, and returns a reference to the 16-byte hash data.
Definition: MD5.cpp:271
uint64_t
llvm::MD5
Definition: MD5.h:41
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::operator==
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1981
llvm::MD5::MD5Result::high
uint64_t high() const
Definition: MD5.h:60
llvm::ArrayRef< uint8_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
uint32_t
llvm::MD5::MD5Result
Definition: MD5.h:43
llvm::MD5::buffer
uint8_t buffer[64]
Definition: MD5.h:108
llvm::MD5::MD5Result::words
std::pair< uint64_t, uint64_t > words() const
Definition: MD5.h:64
llvm::MD5::hash
static std::array< uint8_t, 16 > hash(ArrayRef< uint8_t > Data)
Computes the hash for a given bytes.
Definition: MD5.cpp:294
support
Reimplement select in terms of SEL *We would really like to support but we need to prove that the add doesn t need to overflow between the two bit chunks *Implement pre post increment support(e.g. PR935) *Implement smarter const ant generation for binops with large immediates. A few ARMv6T2 ops should be pattern matched
Definition: README.txt:10
llvm::MD5::c
MD5_u32plus c
Definition: MD5.h:104
llvm::MD5::MD5Result::operator[]
uint8_t & operator[](size_t I)
Definition: MD5.h:49
Endian.h
llvm::MD5::MD5Result::operator[]
const uint8_t & operator[](size_t I) const
Definition: MD5.h:48
llvm::MD5Hash
uint64_t MD5Hash(StringRef Str)
Helper to compute and return lower 64 bits of the given string's MD5 hash.
Definition: MD5.h:122
llvm::MD5::MD5Result::digest
SmallString< 32 > digest() const
Definition: MD5.cpp:282