// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // The original source code is from: // https://chromium.googlesource.com/chromium/src/base/+/ec2c345/md5.cc // The original file was copied from sqlite, and was in the public domain. /* * This code implements the MD5 message-digest algorithm. * The algorithm is due to Ron Rivest. This code was * written by Colin Plumb in 1993, no copyright is claimed. * This code is in the public domain; do with it what you wish. * * Equivalent code is available from RSA Data Security, Inc. * This code has been tested against that, and is equivalent, * except that you don't need to include two pages of legalese * with every copy. * * To compute the message digest of a chunk of bytes, declare an * MD5Context structure, pass it to MD5Init, call MD5Update as * needed on buffers full of bytes, and then call MD5Final, which * will fill a supplied 16-byte array with the digest. */ #include "md5.h" #include <cstddef> #include <string> #include <string.h> namespace { struct Context { … }; /* * Note: this code is harmless on little-endian machines. */ void byteReverse(uint8_t* buf, unsigned longs) { … } /* The four core functions - F1 is optimized somewhat */ /* #define F1(x, y, z) (x & y | ~x & z) */ #define F1(x, y, z) … #define F2(x, y, z) … #define F3(x, y, z) … #define F4(x, y, z) … /* This is the central step in the MD5 algorithm. */ #define MD5STEP(f, w, x, y, z, data, s) … /* * The core of the MD5 algorithm, this alters an existing MD5 hash to * reflect the addition of 16 longwords of new data. MD5Update blocks * the data and converts bytes into longwords for this routine. */ void MD5Transform(uint32_t buf[4], const uint32_t in[16]) { … } } // namespace namespace i18n { namespace addressinput { /* * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious * initialization constants. */ void MD5Init(MD5Context* context) { … } /* * Update context to reflect the concatenation of another buffer full * of bytes. */ void MD5Update(MD5Context* context, const std::string& data) { … } /* * Final wrapup - pad to 64-byte boundary with the bit pattern * 1 0* (64-bit count of bits processed, MSB-first) */ void MD5Final(MD5Digest* digest, MD5Context* context) { … } void MD5IntermediateFinal(MD5Digest* digest, const MD5Context* context) { … } std::string MD5DigestToBase16(const MD5Digest& digest) { … } void MD5Sum(const void* data, size_t length, MD5Digest* digest) { … } std::string MD5String(const std::string& str) { … } } // namespace addressinput } // namespace i18n