結果

問題 No.8005 天使のハッシュ関数
ユーザー a01sa01to
提出日時 2025-01-31 00:03:38
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,883 bytes
コンパイル時間 3,481 ms
コンパイル使用メモリ 282,116 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2025-01-31 00:03:42
合計ジャッジ時間 4,535 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
  #include "settings/debug.cpp"
#else
  #define Debug(...) void(0)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

// https://en.wikipedia.org/wiki/MD5#Pseudocode
string md5(string str) {
  static_assert(endian::native == endian::little, "only little endian");

  vector<uint8_t> bytes;
  for (char c : str) bytes.push_back(c);
  // s: ラウンドごとのシフト数
  array<uint32_t, 64> s;
  {
    constexpr array<uint32_t, 4> r1 = { 7, 12, 17, 22 };
    constexpr array<uint32_t, 4> r2 = { 5, 9, 14, 20 };
    constexpr array<uint32_t, 4> r3 = { 4, 11, 16, 23 };
    constexpr array<uint32_t, 4> r4 = { 6, 10, 15, 21 };
    for (int i = 0; i < 16; ++i) s[i] = r1[i % 4];
    for (int i = 16; i < 32; ++i) s[i] = r2[i % 4];
    for (int i = 32; i < 48; ++i) s[i] = r3[i % 4];
    for (int i = 48; i < 64; ++i) s[i] = r4[i % 4];
  }
  // k: sin(i) * 2^32
  // wikipedia に載ってる前計算の値を使う
  // clang-format off
  array<uint32_t, 64> k = { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 };
  // clang-format on
  // 初期値
  uint32_t a0 = 0x67452301, b0 = 0xefcdab89, c0 = 0x98badcfe, d0 = 0x10325476;
  // padding
  bytes.push_back(0x80);
  while (bytes.size() % 64 != 56) bytes.push_back(0x00);
  uint64_t bitlen = str.size() * 8;
  // little endian で追加
  for (int i = 0; i < 8; ++i) {
    bytes.push_back(bitlen & 0xff);
    bitlen >>= 8;
  }
  // process
  assert(bytes.size() % 64 == 0);
  for (int chunk = 0; chunk < bytes.size(); chunk += 64) {
    // 512bit のブロックを 32bit ずつに分割
    array<uint32_t, 16> m;
    for (int i = 0; i < 16; ++i) {
      m[i] = 0;
      for (int j = 0; j < 4; ++j) m[i] |= bytes[chunk + i * 4 + j] << (j * 8);
    }
    uint32_t a = a0, b = b0, c = c0, d = d0;
    for (int i = 0; i < 64; ++i) {
      uint32_t f, g;
      if (i < 16) {
        f = (b & c) | ((~b) & d);
        g = i;
      }
      else if (i < 32) {
        f = (d & b) | ((~d) & c);
        g = (5 * i + 1) % 16;
      }
      else if (i < 48) {
        f = b ^ c ^ d;
        g = (3 * i + 5) % 16;
      }
      else {
        f = c ^ (b | (~d));
        g = (7 * i) % 16;
      }
      f += a + k[i] + m[g];
      a = d, d = c, c = b;
      // left rotate x by s[i] bits
      b += rotl(f, s[i]);
    }
    a0 += a, b0 += b, c0 += c, d0 += d;
  }
  vector<uint8_t> digest;
  for (uint32_t x : { a0, b0, c0, d0 }) {
    for (int i = 0; i < 4; ++i) {
      digest.push_back(x & 0xff);
      x >>= 8;
    }
  }
  // hex 表記にする
  const string hex = "0123456789abcdef";
  string res = "";
  for (uint8_t x : digest) {
    res += hex[x >> 4];
    res += hex[x & 0xf];
  }
  return res;
}

int main() {
  // https://en.wikipedia.org/wiki/MD5#MD5_hashes
  assert(md5("The quick brown fox jumps over the lazy dog") == "9e107d9d372bb6826bd81d3542a419d6");
  assert(md5("The quick brown fox jumps over the lazy dog.") == "e4d909c290d0fb1ca068ffaddf22cbd0");
  assert(md5("") == "d41d8cd98f00b204e9800998ecf8427e");

  string s;
  cin >> s;
  cout << md5(md5(s)) << endl;
  return 0;
}
0