Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:32

0001 /* -*- C++ -*-
0002  * This code is derived from (original license follows):
0003  *
0004  * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
0005  * MD5 Message-Digest Algorithm (RFC 1321).
0006  *
0007  * Homepage:
0008  * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
0009  *
0010  * Author:
0011  * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
0012  *
0013  * This software was written by Alexander Peslyak in 2001.  No copyright is
0014  * claimed, and the software is hereby placed in the public domain.
0015  * In case this attempt to disclaim copyright and place the software in the
0016  * public domain is deemed null and void, then the software is
0017  * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
0018  * general public under the following terms:
0019  *
0020  * Redistribution and use in source and binary forms, with or without
0021  * modification, are permitted.
0022  *
0023  * There's ABSOLUTELY NO WARRANTY, express or implied.
0024  *
0025  * See md5.c for more information.
0026  */
0027 
0028 #ifndef LLVM_SUPPORT_MD5_H
0029 #define LLVM_SUPPORT_MD5_H
0030 
0031 #include "llvm/ADT/StringRef.h"
0032 #include "llvm/Support/Endian.h"
0033 #include <array>
0034 #include <cstdint>
0035 
0036 namespace llvm {
0037 
0038 template <unsigned N> class SmallString;
0039 template <typename T> class ArrayRef;
0040 
0041 class MD5 {
0042 public:
0043   struct MD5Result : public std::array<uint8_t, 16> {
0044     SmallString<32> digest() const;
0045 
0046     uint64_t low() const {
0047       // Our MD5 implementation returns the result in little endian, so the low
0048       // word is first.
0049       using namespace support;
0050       return endian::read<uint64_t, llvm::endianness::little>(data());
0051     }
0052 
0053     uint64_t high() const {
0054       using namespace support;
0055       return endian::read<uint64_t, llvm::endianness::little>(data() + 8);
0056     }
0057     std::pair<uint64_t, uint64_t> words() const {
0058       using namespace support;
0059       return std::make_pair(high(), low());
0060     }
0061   };
0062 
0063   MD5();
0064 
0065   /// Updates the hash for the byte stream provided.
0066   void update(ArrayRef<uint8_t> Data);
0067 
0068   /// Updates the hash for the StringRef provided.
0069   void update(StringRef Str);
0070 
0071   /// Finishes off the hash and puts the result in result.
0072   void final(MD5Result &Result);
0073 
0074   /// Finishes off the hash, and returns the 16-byte hash data.
0075   MD5Result final();
0076 
0077   /// Finishes off the hash, and returns the 16-byte hash data.
0078   /// This is suitable for getting the MD5 at any time without invalidating the
0079   /// internal state, so that more calls can be made into `update`.
0080   MD5Result result();
0081 
0082   /// Translates the bytes in \p Res to a hex string that is
0083   /// deposited into \p Str. The result will be of length 32.
0084   static void stringifyResult(MD5Result &Result, SmallVectorImpl<char> &Str);
0085 
0086   /// Computes the hash for a given bytes.
0087   static MD5Result hash(ArrayRef<uint8_t> Data);
0088 
0089 private:
0090   // Any 32-bit or wider unsigned integer data type will do.
0091   typedef uint32_t MD5_u32plus;
0092 
0093   // Internal State
0094   struct {
0095     MD5_u32plus a = 0x67452301;
0096     MD5_u32plus b = 0xefcdab89;
0097     MD5_u32plus c = 0x98badcfe;
0098     MD5_u32plus d = 0x10325476;
0099     MD5_u32plus hi = 0;
0100     MD5_u32plus lo = 0;
0101     uint8_t buffer[64];
0102     MD5_u32plus block[16];
0103   } InternalState;
0104 
0105   const uint8_t *body(ArrayRef<uint8_t> Data);
0106 };
0107 
0108 /// Helper to compute and return lower 64 bits of the given string's MD5 hash.
0109 inline uint64_t MD5Hash(StringRef Str) {
0110   using namespace support;
0111 
0112   MD5 Hash;
0113   Hash.update(Str);
0114   MD5::MD5Result Result;
0115   Hash.final(Result);
0116   // Return the least significant word.
0117   return Result.low();
0118 }
0119 
0120 } // end namespace llvm
0121 
0122 #endif // LLVM_SUPPORT_MD5_H