API Reference
Overview
The MD5 hash calculator provides a complete RESTful API interface supporting text calculation, file processing, and batch operations.
Basic Information
- Base URL:
https://api.example.com/md5
- Authentication: API Key
- Data Format: JSON
- Character Encoding: UTF-8
Response Format
All API responses follow a unified format:
{
"success": true,
"data": {
// Specific data
},
"message": "Operation successful",
"timestamp": "2024-01-20T10:30:00Z"
}
Text MD5 Calculation
Calculate Text MD5
Endpoint: POST /api/md5/text
Request Parameters:
{
"text": "Text content to calculate",
"encoding": "utf8",
"outputFormat": "hex"
}
Parameter Description:
Parameter | Type | Required | Description |
---|---|---|---|
text | string | Yes | Text content to calculate MD5 |
encoding | string | No | Character encoding, default utf8 |
outputFormat | string | No | Output format, default hex |
Supported Encoding Formats:
utf8
: UTF-8 encodingascii
: ASCII encodingutf16
: UTF-16 encodinggbk
: GBK encoding
Supported Output Formats:
hex
: Lowercase hexadecimalhex-upper
: Uppercase hexadecimalbase64
: Base64 encoding
Response Example:
{
"success": true,
"data": {
"text": "Hello, World!",
"hash": "65a8e27d8879283831b664bd8b7f0ad4",
"encoding": "utf8",
"outputFormat": "hex",
"length": 32
},
"message": "Calculation successful",
"timestamp": "2024-01-20T10:30:00Z"
}
Batch Text Calculation
Endpoint: POST /api/md5/text/batch
Request Parameters:
{
"texts": ["Text1", "Text2", "Text3"],
"encoding": "utf8",
"outputFormat": "hex"
}
Response Example:
{
"success": true,
"data": {
"results": [
{
"text": "Text1",
"hash": "hash1",
"index": 0
},
{
"text": "Text2",
"hash": "hash2",
"index": 1
},
{
"text": "Text3",
"hash": "hash3",
"index": 2
}
],
"total": 3,
"encoding": "utf8",
"outputFormat": "hex"
},
"message": "Batch calculation successful",
"timestamp": "2024-01-20T10:30:00Z"
}
File MD5 Calculation
Upload File Calculation
Endpoint: POST /api/md5/file
Request Format: multipart/form-data
Request Parameters:
Parameter | Type | Required | Description |
---|---|---|---|
file | file | Yes | File to calculate |
outputFormat | string | No | Output format, default hex |
Response Example:
{
"success": true,
"data": {
"filename": "example.txt",
"size": 1024,
"hash": "d41d8cd98f00b204e9800998ecf8427e",
"outputFormat": "hex",
"processingTime": 150
},
"message": "File calculation successful",
"timestamp": "2024-01-20T10:30:00Z"
}
Batch File Calculation
Endpoint: POST /api/md5/file/batch
Request Format: multipart/form-data
Request Parameters:
Parameter | Type | Required | Description |
---|---|---|---|
files | file | Yes | Array of files to calculate |
outputFormat | string | No | Output format, default hex |
Response Example:
{
"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": "Batch file calculation successful",
"timestamp": "2024-01-20T10:30:00Z"
}
Hash Value Verification
Verify Hash Value
Endpoint: POST /api/md5/verify
Request Parameters:
{
"text": "Text to verify",
"expectedHash": "Expected hash value",
"encoding": "utf8"
}
Response Example:
{
"success": true,
"data": {
"text": "Hello, World!",
"expectedHash": "65a8e27d8879283831b664bd8b7f0ad4",
"calculatedHash": "65a8e27d8879283831b664bd8b7f0ad4",
"isValid": true,
"encoding": "utf8"
},
"message": "Verification successful",
"timestamp": "2024-01-20T10:30:00Z"
}
File Integrity Verification
Endpoint: POST /api/md5/verify/file
Request Format: multipart/form-data
Request Parameters:
Parameter | Type | Required | Description |
---|---|---|---|
file | file | Yes | File to verify |
expectedHash | string | Yes | Expected hash value |
Response Example:
{
"success": true,
"data": {
"filename": "example.txt",
"expectedHash": "d41d8cd98f00b204e9800998ecf8427e",
"calculatedHash": "d41d8cd98f00b204e9800998ecf8427e",
"isValid": true,
"size": 1024
},
"message": "File integrity verification successful",
"timestamp": "2024-01-20T10:30:00Z"
}
Error Handling
Error Response Format
{
"success": false,
"error": {
"code": "ERROR_CODE",
"message": "Error description",
"details": "Detailed error information"
},
"timestamp": "2024-01-20T10:30:00Z"
}
Common Error Codes
Error Code | Description | HTTP Status Code |
---|---|---|
INVALID_INPUT | Invalid input parameters | 400 |
FILE_TOO_LARGE | File too large | 413 |
UNSUPPORTED_ENCODING | Unsupported encoding format | 400 |
INVALID_HASH_FORMAT | Invalid hash format | 400 |
RATE_LIMIT_EXCEEDED | Request rate exceeded | 429 |
INTERNAL_ERROR | Server internal error | 500 |
Usage Examples
JavaScript Example
// Calculate text 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;
};
// Calculate file 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;
};
// Verify hash value
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 Example
import requests
import json
# Calculate text 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"])
# Calculate file 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"])
# Verify hash value
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 Example
# Calculate text 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"
}'
# Calculate file MD5
curl -X POST "https://api.example.com/md5/file" \
-H "Authorization: Bearer YOUR_API_KEY" \
-F "file=@example.txt"
# Verify hash value
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"
}'
Limitations
Request Limits
- Text length: Maximum 10MB
- File size: Maximum 100MB
- Batch quantity: Maximum 100 items
- Request frequency: Maximum 1000 requests per minute
Supported Formats
- Text encoding: UTF-8, ASCII, UTF-16, GBK
- Output format: Hexadecimal (uppercase/lowercase), Base64
- File types: All file types
Last Updated: January 20, 2024