LLVM 22.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"
33#include "llvm/Support/Endian.h"
34#include <array>
35#include <cstdint>
36
37namespace llvm {
38
39template <unsigned N> class SmallString;
40template <typename T> class ArrayRef;
41
42class MD5 {
43public:
44 struct MD5Result : public std::array<uint8_t, 16> {
46
47 uint64_t low() const {
48 // Our MD5 implementation returns the result in little endian, so the low
49 // word is first.
50 using namespace support;
52 }
53
54 uint64_t high() const {
55 using namespace support;
57 }
58 std::pair<uint64_t, uint64_t> words() const {
59 using namespace support;
60 return std::make_pair(high(), low());
61 }
62 };
63
65
66 /// Updates the hash for the byte stream provided.
68
69 /// Updates the hash for the StringRef provided.
70 LLVM_ABI void update(StringRef Str);
71
72 /// Finishes off the hash and puts the result in result.
73 LLVM_ABI void final(MD5Result &Result);
74
75 /// Finishes off the hash, and returns the 16-byte hash data.
76 LLVM_ABI MD5Result final();
77
78 /// Finishes off the hash, and returns the 16-byte hash data.
79 /// This is suitable for getting the MD5 at any time without invalidating the
80 /// internal state, so that more calls can be made into `update`.
82
83 /// Translates the bytes in \p Res to a hex string that is
84 /// deposited into \p Str. The result will be of length 32.
85 LLVM_ABI static void stringifyResult(MD5Result &Result,
87
88 /// Computes the hash for a given bytes.
90
91private:
92 // Any 32-bit or wider unsigned integer data type will do.
93 typedef uint32_t MD5_u32plus;
94
95 // Internal State
96 struct {
97 MD5_u32plus a = 0x67452301;
98 MD5_u32plus b = 0xefcdab89;
99 MD5_u32plus c = 0x98badcfe;
100 MD5_u32plus d = 0x10325476;
101 MD5_u32plus hi = 0;
102 MD5_u32plus lo = 0;
104 MD5_u32plus block[16];
105 } InternalState;
106
108};
109
110/// Helper to compute and return lower 64 bits of the given string's MD5 hash.
112 using namespace support;
113
114 MD5 Hash;
115 Hash.update(Str);
116 MD5::MD5Result Result;
117 Hash.final(Result);
118 // Return the least significant word.
119 return Result.low();
120}
121
122} // end namespace llvm
123
124#endif // LLVM_SUPPORT_MD5_H
#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
MD5_u32plus block[16]
Definition MD5.h:104
MD5_u32plus a
Definition MD5.h:97
LLVM_ABI void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition MD5.cpp:189
static LLVM_ABI void stringifyResult(MD5Result &Result, SmallVectorImpl< char > &Str)
Translates the bytes in Res to a hex string that is deposited into Str.
Definition MD5.cpp:287
LLVM_ABI void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition MD5.cpp:234
MD5_u32plus hi
Definition MD5.h:101
LLVM_ABI MD5Result result()
Finishes off the hash, and returns the 16-byte hash data.
Definition MD5.cpp:270
static LLVM_ABI MD5Result hash(ArrayRef< uint8_t > Data)
Computes the hash for a given bytes.
Definition MD5.cpp:291
uint8_t buffer[64]
Definition MD5.h:103
MD5_u32plus lo
Definition MD5.h:102
MD5_u32plus b
Definition MD5.h:98
MD5_u32plus d
Definition MD5.h:100
MD5_u32plus c
Definition MD5.h:99
LLVM_ABI MD5()
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
uint64_t MD5Hash(const FunctionId &Obj)
Definition FunctionId.h:167
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition Endian.h:58
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
LLVM_ABI SmallString< 32 > digest() const
Definition MD5.cpp:281
uint64_t low() const
Definition MD5.h:47
uint64_t high() const
Definition MD5.h:54
std::pair< uint64_t, uint64_t > words() const
Definition MD5.h:58