2 * This code is derived from (original license follows): 4 * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. 5 * MD5 Message-Digest Algorithm (RFC 1321). 8 * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 11 * Alexander Peslyak, better known as Solar Designer <solar at openwall.com> 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: 20 * Redistribution and use in source and binary forms, with or without 21 * modification, are permitted. 23 * There's ABSOLUTELY NO WARRANTY, express or implied. 25 * (This is a heavily cut-down "BSD license".) 27 * This differs from Colin Plumb's older public domain implementation in that 28 * no exactly 32-bit integer data type is required (any 32-bit or wider 29 * unsigned integer data type will do), there's no compile-time endianness 30 * configuration, and the function prototypes match OpenSSL's. No code from 31 * Colin Plumb's implementation has been reused; this comment merely compares 32 * the properties of the two independent implementations. 34 * The primary goals of this implementation are portability and ease of use. 35 * It is meant to be fast, but not as fast as possible. Some known 36 * optimizations are not included to reduce source code size and avoid 37 * compile-time configuration. 50// The basic MD5 functions. 52// F and G are optimized compared to their RFC 1321 definitions for 53// architectures that lack an AND-NOT instruction, just like in Colin Plumb's 55#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) 56#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y)))) 57#define H(x, y, z) ((x) ^ (y) ^ (z)) 58#define I(x, y, z) ((y) ^ ((x) | ~(z))) 60// The MD5 transformation for all four rounds. 61#define STEP(f, a, b, c, d, x, t, s) \ 62 (a) += f((b), (c), (d)) + (x) + (t); \ 63 (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ 66// SET reads 4 input bytes in little-endian byte order and stores them 67// in a properly aligned word in host byte order. 69 (InternalState.block[(n)] = (MD5_u32plus)ptr[(n)*4] | \ 70 ((MD5_u32plus)ptr[(n)*4 + 1] << 8) | \ 71 ((MD5_u32plus)ptr[(n)*4 + 2] << 16) | \ 72 ((MD5_u32plus)ptr[(n)*4 + 3] << 24)) 73#define GET(n) (InternalState.block[(n)]) 77/// This processes one or more 64-byte data blocks, but does NOT update 78///the bit counters. There are no alignment requirements. 82 MD5_u32plus saved_a, saved_b, saved_c, saved_d;
188/// Incrementally add the bytes in \p Data to the hash. 191unsignedlong used, free;
195 saved_lo = InternalState.lo;
196if ((InternalState.lo = (saved_lo +
Size) & 0x1fffffff) < saved_lo)
198 InternalState.hi +=
Size >> 29;
200 used = saved_lo & 0x3f;
206 memcpy(&InternalState.buffer[used],
Ptr,
Size);
210 memcpy(&InternalState.buffer[used],
Ptr, free);
213 body(
ArrayRef(InternalState.buffer, 64));
221 memcpy(InternalState.buffer,
Ptr,
Size);
224/// Add the bytes in the StringRef \p Str to the hash. 225// Note that this isn't a string and so this won't include any trailing NULL 232/// Finish the hash and place the resulting hash into \p result. 233/// \param Result is assumed to be a minimum of 16-bytes in size. 235unsignedlong used, free;
237 used = InternalState.lo & 0x3f;
239 InternalState.buffer[used++] = 0x80;
244 memset(&InternalState.buffer[used], 0, free);
245 body(
ArrayRef(InternalState.buffer, 64));
250 memset(&InternalState.buffer[used], 0, free - 8);
252 InternalState.lo <<= 3;
256 body(
ArrayRef(InternalState.buffer, 64));
271auto StateToRestore = InternalState;
276 InternalState = StateToRestore;
283 toHex(*
this,
/*LowerCase*/true, Str);
288 toHex(Result,
/*LowerCase*/true, Str);
#define STEP(f, a, b, c, d, x, t, s)
#define SET(ID, TYPE, VAL)
This file defines the SmallString class.
This file contains some functions that are useful when dealing with strings.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
static void stringifyResult(MD5Result &Result, SmallVectorImpl< char > &Str)
Translates the bytes in Res to a hex string that is deposited into Str.
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
MD5Result final()
Finishes off the hash, and returns the 16-byte hash data.
MD5Result result()
Finishes off the hash, and returns the 16-byte hash data.
static MD5Result hash(ArrayRef< uint8_t > Data)
Computes the hash for a given bytes.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
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.
void write32le(void *P, uint32_t V)
This is an optimization pass for GlobalISel generic memory operations.
SmallString< 32 > digest() const