md5-test.html 3.76 KB
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>MD5加密测试</title>
  <script src="md5.js"></script>
  <style>
    body {
      font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
      margin: 20px;
      background: #f5f5f5;
    }
    .container {
      max-width: 600px;
      margin: 0 auto;
      background: white;
      padding: 20px;
      border-radius: 10px;
      box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    }
    .test-item {
      margin: 15px 0;
      padding: 15px;
      border: 1px solid #ddd;
      border-radius: 8px;
    }
    .result {
      font-family: monospace;
      background: #f5f5f5;
      padding: 10px;
      border-radius: 5px;
      margin-top: 10px;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>🔐 MD5加密测试</h1>
    
    <div class="test-item">
      <h3>测试用例</h3>
      <p><strong>原始密码:</strong> 123456</p>
      <p><strong>MD5加密后:</strong> <span id="md5Result">-</span></p>
      <p><strong>预期结果:</strong> e10adc3949ba59abbe56e057f20f883e</p>
      <p><strong>测试结果:</strong> <span id="testResult">-</span></p>
    </div>
    
    <div class="test-item">
      <h3>自定义测试</h3>
      <input type="text" id="customInput" placeholder="输入要加密的文本" value="test">
      <button onclick="testCustom()">测试加密</button>
      <div class="result" id="customResult">-</div>
    </div>
    
    <div class="test-item">
      <h3>常见密码测试</h3>
      <div id="commonPasswords"></div>
    </div>
  </div>

  <script>
    // 页面加载完成后测试
    document.addEventListener('DOMContentLoaded', function() {
      // 测试基本MD5功能
      const testPassword = '123456';
      const expectedMd5 = 'e10adc3949ba59abbe56e057f20f883e';
      const actualMd5 = md5(testPassword);
      
      document.getElementById('md5Result').textContent = actualMd5;
      
      if (actualMd5 === expectedMd5) {
        document.getElementById('testResult').textContent = '✅ 测试通过';
        document.getElementById('testResult').style.color = 'green';
      } else {
        document.getElementById('testResult').textContent = '❌ 测试失败';
        document.getElementById('testResult').style.color = 'red';
      }
      
      // 测试常见密码
      testCommonPasswords();
    });
    
    // 测试自定义输入
    function testCustom() {
      const input = document.getElementById('customInput').value;
      const result = md5(input);
      document.getElementById('customResult').textContent = result;
    }
    
    // 测试常见密码
    function testCommonPasswords() {
      const passwords = [
        { text: 'admin', expected: '21232f297a57a5a743894a0e4a801fc3' },
        { text: 'password', expected: '5f4dcc3b5aa765d61d8327deb882cf99' },
        { text: '123456', expected: 'e10adc3949ba59abbe56e057f20f883e' },
        { text: 'qwerty', expected: 'd8578edf8458ce06fbc5bb76a58c5ca4' }
      ];
      
      const container = document.getElementById('commonPasswords');
      
      passwords.forEach(pwd => {
        const actual = md5(pwd.text);
        const isCorrect = actual === pwd.expected;
        
        const div = document.createElement('div');
        div.style.margin = '10px 0';
        div.style.padding = '10px';
        div.style.border = '1px solid #ddd';
        div.style.borderRadius = '5px';
        
        div.innerHTML = `
          <strong>${pwd.text}</strong><br>
          MD5: ${actual}<br>
          预期: ${pwd.expected}<br>
          结果: <span style="color: ${isCorrect ? 'green' : 'red'}">${isCorrect ? '✅' : '❌'}</span>
        `;
        
        container.appendChild(div);
      });
    }
  </script>
</body>
</html>