w

API 参考

概述

MD5 哈希计算器提供完整的 RESTful API 接口,支持文本计算、文件处理和批量操作。

基础信息

  • 基础 URLhttps://api.example.com/md5
  • 认证方式:API Key
  • 数据格式:JSON
  • 字符编码:UTF-8

响应格式

所有 API 响应都遵循统一的格式:

{
  "success": true,
  "data": {
    // 具体数据
  },
  "message": "操作成功",
  "timestamp": "2024-01-20T10:30:00Z"
}

文本 MD5 计算

计算文本 MD5

接口地址POST /api/md5/text

请求参数

{
  "text": "要计算的文本内容",
  "encoding": "utf8",
  "outputFormat": "hex"
}

参数说明

参数类型必填说明
textstring要计算 MD5 的文本内容
encodingstring字符编码,默认 utf8
outputFormatstring输出格式,默认 hex

支持的编码格式

  • utf8:UTF-8 编码
  • ascii:ASCII 编码
  • utf16:UTF-16 编码
  • gbk:GBK 编码

支持的输出格式

  • hex:小写十六进制
  • hex-upper:大写十六进制
  • base64:Base64 编码

响应示例

{
  "success": true,
  "data": {
    "text": "Hello, World!",
    "hash": "65a8e27d8879283831b664bd8b7f0ad4",
    "encoding": "utf8",
    "outputFormat": "hex",
    "length": 32
  },
  "message": "计算成功",
  "timestamp": "2024-01-20T10:30:00Z"
}

批量文本计算

接口地址POST /api/md5/text/batch

请求参数

{
  "texts": ["文本1", "文本2", "文本3"],
  "encoding": "utf8",
  "outputFormat": "hex"
}

响应示例

{
  "success": true,
  "data": {
    "results": [
      {
        "text": "文本1",
        "hash": "hash1",
        "index": 0
      },
      {
        "text": "文本2",
        "hash": "hash2",
        "index": 1
      },
      {
        "text": "文本3",
        "hash": "hash3",
        "index": 2
      }
    ],
    "total": 3,
    "encoding": "utf8",
    "outputFormat": "hex"
  },
  "message": "批量计算成功",
  "timestamp": "2024-01-20T10:30:00Z"
}

文件 MD5 计算

上传文件计算

接口地址POST /api/md5/file

请求格式multipart/form-data

请求参数

参数类型必填说明
filefile要计算的文件
outputFormatstring输出格式,默认 hex

响应示例

{
  "success": true,
  "data": {
    "filename": "example.txt",
    "size": 1024,
    "hash": "d41d8cd98f00b204e9800998ecf8427e",
    "outputFormat": "hex",
    "processingTime": 150
  },
  "message": "文件计算成功",
  "timestamp": "2024-01-20T10:30:00Z"
}

批量文件计算

接口地址POST /api/md5/file/batch

请求格式multipart/form-data

请求参数

参数类型必填说明
filesfile要计算的文件数组
outputFormatstring输出格式,默认 hex

响应示例

{
  "success": true,
  "data": {
    "results": [
      {
        "filename": "file1.txt",
        "size": 1024,
        "hash": "hash1",
        "processingTime": 150
      },
      {
        "filename": "file2.txt",
        "size": 2048,
        "hash": "hash2",
        "processingTime": 200
      }
    ],
    "total": 2,
    "totalSize": 3072,
    "totalTime": 350
  },
  "message": "批量文件计算成功",
  "timestamp": "2024-01-20T10:30:00Z"
}

散列值验证

验证散列值

接口地址POST /api/md5/verify

请求参数

{
  "text": "要验证的文本",
  "expectedHash": "期望的散列值",
  "encoding": "utf8"
}

响应示例

{
  "success": true,
  "data": {
    "text": "Hello, World!",
    "expectedHash": "65a8e27d8879283831b664bd8b7f0ad4",
    "calculatedHash": "65a8e27d8879283831b664bd8b7f0ad4",
    "isValid": true,
    "encoding": "utf8"
  },
  "message": "验证成功",
  "timestamp": "2024-01-20T10:30:00Z"
}

文件完整性验证

接口地址POST /api/md5/verify/file

请求格式multipart/form-data

请求参数

参数类型必填说明
filefile要验证的文件
expectedHashstring期望的散列值

响应示例

{
  "success": true,
  "data": {
    "filename": "example.txt",
    "expectedHash": "d41d8cd98f00b204e9800998ecf8427e",
    "calculatedHash": "d41d8cd98f00b204e9800998ecf8427e",
    "isValid": true,
    "size": 1024
  },
  "message": "文件完整性验证成功",
  "timestamp": "2024-01-20T10:30:00Z"
}

错误处理

错误响应格式

{
  "success": false,
  "error": {
    "code": "ERROR_CODE",
    "message": "错误描述",
    "details": "详细错误信息"
  },
  "timestamp": "2024-01-20T10:30:00Z"
}

常见错误码

错误码说明HTTP 状态码
INVALID_INPUT输入参数无效400
FILE_TOO_LARGE文件过大413
UNSUPPORTED_ENCODING不支持的编码格式400
INVALID_HASH_FORMAT散列值格式无效400
RATE_LIMIT_EXCEEDED请求频率超限429
INTERNAL_ERROR服务器内部错误500

使用示例

JavaScript 示例

// 计算文本 MD5
const calculateTextMD5 = async (text) => {
  const response = await fetch('/api/md5/text', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'Bearer YOUR_API_KEY',
    },
    body: JSON.stringify({
      text,
      encoding: 'utf8',
      outputFormat: 'hex',
    }),
  });

  const result = await response.json();
  return result.data.hash;
};

// 计算文件 MD5
const calculateFileMD5 = async (file) => {
  const formData = new FormData();
  formData.append('file', file);

  const response = await fetch('/api/md5/file', {
    method: 'POST',
    headers: {
      Authorization: 'Bearer YOUR_API_KEY',
    },
    body: formData,
  });

  const result = await response.json();
  return result.data.hash;
};

// 验证散列值
const verifyHash = async (text, expectedHash) => {
  const response = await fetch('/api/md5/verify', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'Bearer YOUR_API_KEY',
    },
    body: JSON.stringify({
      text,
      expectedHash,
      encoding: 'utf8',
    }),
  });

  const result = await response.json();
  return result.data.isValid;
};

Python 示例

import requests
import json

# 计算文本 MD5
def calculate_text_md5(text, api_key):
    url = "https://api.example.com/md5/text"
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }
    data = {
        "text": text,
        "encoding": "utf8",
        "outputFormat": "hex"
    }

    response = requests.post(url, headers=headers, json=data)
    result = response.json()

    if result["success"]:
        return result["data"]["hash"]
    else:
        raise Exception(result["error"]["message"])

# 计算文件 MD5
def calculate_file_md5(file_path, api_key):
    url = "https://api.example.com/md5/file"
    headers = {
        "Authorization": f"Bearer {api_key}"
    }

    with open(file_path, 'rb') as file:
        files = {'file': file}
        response = requests.post(url, headers=headers, files=files)

    result = response.json()

    if result["success"]:
        return result["data"]["hash"]
    else:
        raise Exception(result["error"]["message"])

# 验证散列值
def verify_hash(text, expected_hash, api_key):
    url = "https://api.example.com/md5/verify"
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }
    data = {
        "text": text,
        "expectedHash": expected_hash,
        "encoding": "utf8"
    }

    response = requests.post(url, headers=headers, json=data)
    result = response.json()

    if result["success"]:
        return result["data"]["isValid"]
    else:
        raise Exception(result["error"]["message"])

cURL 示例

# 计算文本 MD5
curl -X POST "https://api.example.com/md5/text" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "text": "Hello, World!",
    "encoding": "utf8",
    "outputFormat": "hex"
  }'

# 计算文件 MD5
curl -X POST "https://api.example.com/md5/file" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -F "file=@example.txt"

# 验证散列值
curl -X POST "https://api.example.com/md5/verify" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "text": "Hello, World!",
    "expectedHash": "65a8e27d8879283831b664bd8b7f0ad4",
    "encoding": "utf8"
  }'

限制说明

请求限制

  • 文本长度:最大 10MB
  • 文件大小:最大 100MB
  • 批量数量:最多 100 个项目
  • 请求频率:每分钟最多 1000 次请求

支持的格式

  • 文本编码:UTF-8, ASCII, UTF-16, GBK
  • 输出格式:十六进制(大小写), Base64
  • 文件类型:所有文件类型

最后更新时间:2024年1月20日

Was this page helpful?