first commit
This commit is contained in:
106
pkg/lzkit/md5/README.md
Normal file
106
pkg/lzkit/md5/README.md
Normal file
@@ -0,0 +1,106 @@
|
||||
# MD5 工具包
|
||||
|
||||
这个包提供了全面的 MD5 哈希功能,包括字符串加密、文件加密、链式操作、加盐哈希等。
|
||||
|
||||
## 主要功能
|
||||
|
||||
- 字符串和字节切片的 MD5 哈希计算
|
||||
- 文件 MD5 哈希计算(支持大文件分块处理)
|
||||
- 链式 API,支持构建复杂的哈希内容
|
||||
- 带盐值的 MD5 哈希,提高安全性
|
||||
- 哈希验证功能
|
||||
- 16 位和 8 位 MD5 哈希(短版本)
|
||||
- HMAC-MD5 实现,增强安全性
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 基本使用
|
||||
|
||||
```go
|
||||
// 计算字符串的MD5哈希
|
||||
hash := md5.EncryptString("hello world")
|
||||
fmt.Println(hash) // 5eb63bbbe01eeed093cb22bb8f5acdc3
|
||||
|
||||
// 计算字节切片的MD5哈希
|
||||
bytes := []byte("hello world")
|
||||
hash = md5.EncryptBytes(bytes)
|
||||
|
||||
// 计算文件的MD5哈希
|
||||
fileHash, err := md5.EncryptFile("path/to/file.txt")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(fileHash)
|
||||
```
|
||||
|
||||
### 链式 API
|
||||
|
||||
```go
|
||||
// 创建一个新的MD5实例并添加内容
|
||||
hash := md5.New().
|
||||
Add("hello").
|
||||
Add(" ").
|
||||
Add("world").
|
||||
Sum()
|
||||
fmt.Println(hash) // 5eb63bbbe01eeed093cb22bb8f5acdc3
|
||||
|
||||
// 或者从字符串初始化
|
||||
hash = md5.FromString("hello").
|
||||
Add(" world").
|
||||
Sum()
|
||||
|
||||
// 从字节切片初始化
|
||||
hash = md5.FromBytes([]byte("hello")).
|
||||
AddBytes([]byte(" world")).
|
||||
Sum()
|
||||
```
|
||||
|
||||
### 安全性增强
|
||||
|
||||
```go
|
||||
// 使用盐值加密(提高安全性)
|
||||
hashedPassword := md5.EncryptStringWithSalt("password123", "main@example.com")
|
||||
|
||||
// 使用前缀加密
|
||||
hashedValue := md5.EncryptStringWithPrefix("secret-data", "prefix-")
|
||||
|
||||
// 验证带盐值的哈希
|
||||
isValid := md5.VerifyMD5WithSalt("password123", "main@example.com", hashedPassword)
|
||||
|
||||
// 使用HMAC-MD5提高安全性
|
||||
hmacHash := md5.MD5HMAC("message", "secret-key")
|
||||
```
|
||||
|
||||
### 短哈希值
|
||||
|
||||
```go
|
||||
// 获取16位MD5(32位MD5的中间部分)
|
||||
hash16 := md5.Get16("hello world")
|
||||
fmt.Println(hash16) // 中间16个字符
|
||||
|
||||
// 获取8位MD5
|
||||
hash8 := md5.Get8("hello world")
|
||||
fmt.Println(hash8) // 中间8个字符
|
||||
```
|
||||
|
||||
### 文件验证
|
||||
|
||||
```go
|
||||
// 验证文件MD5是否匹配
|
||||
match, err := md5.VerifyFileMD5("path/to/file.txt", "expected-hash")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if match {
|
||||
fmt.Println("文件MD5校验通过")
|
||||
} else {
|
||||
fmt.Println("文件MD5校验失败")
|
||||
}
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. MD5 主要用于校验,不适合用于安全存储密码等敏感信息
|
||||
2. 如果用于密码存储,请务必使用加盐处理并考虑使用更安全的算法
|
||||
3. 处理大文件时请使用`EncryptFileChunk`以优化性能
|
||||
4. 返回的 MD5 哈希值都是 32 位的小写十六进制字符串(除非使用 Get16/Get8 函数)
|
||||
79
pkg/lzkit/md5/example_test.go
Normal file
79
pkg/lzkit/md5/example_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package md5_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"znc-server/pkg/lzkit/md5"
|
||||
)
|
||||
|
||||
func Example() {
|
||||
// 简单的字符串MD5
|
||||
hashValue := md5.EncryptString("hello world")
|
||||
fmt.Println("MD5(hello world):", hashValue)
|
||||
|
||||
// 使用链式API
|
||||
chainHash := md5.New().
|
||||
Add("hello").
|
||||
Add(" ").
|
||||
Add("world").
|
||||
Sum()
|
||||
fmt.Println("链式MD5:", chainHash)
|
||||
|
||||
// 使用盐值
|
||||
saltedHash := md5.EncryptStringWithSalt("password123", "main@example.com")
|
||||
fmt.Println("加盐MD5:", saltedHash)
|
||||
|
||||
// 验证哈希
|
||||
isValid := md5.VerifyMD5("hello world", hashValue)
|
||||
fmt.Println("验证结果:", isValid)
|
||||
|
||||
// 生成短版本的MD5
|
||||
fmt.Println("16位MD5:", md5.Get16("hello world"))
|
||||
fmt.Println("8位MD5:", md5.Get8("hello world"))
|
||||
|
||||
// 文件MD5计算
|
||||
filePath := "example.txt" // 这只是示例,实际上这个文件可能不存在
|
||||
fileHash, err := md5.EncryptFile(filePath)
|
||||
if err != nil {
|
||||
// 在实际代码中执行正确的错误处理
|
||||
log.Printf("计算文件MD5出错: %v", err)
|
||||
} else {
|
||||
fmt.Println("文件MD5:", fileHash)
|
||||
}
|
||||
|
||||
// HMAC-MD5
|
||||
hmacHash := md5.MD5HMAC("重要消息", "secret-key")
|
||||
fmt.Println("HMAC-MD5:", hmacHash)
|
||||
}
|
||||
|
||||
func ExampleEncryptString() {
|
||||
hash := md5.EncryptString("HelloWorld")
|
||||
fmt.Println(hash)
|
||||
// Output: 68e109f0f40ca72a15e05cc22786f8e6
|
||||
}
|
||||
|
||||
func ExampleMD5_Sum() {
|
||||
hash := md5.New().
|
||||
Add("Hello").
|
||||
Add("World").
|
||||
Sum()
|
||||
fmt.Println(hash)
|
||||
// Output: 68e109f0f40ca72a15e05cc22786f8e6
|
||||
}
|
||||
|
||||
func ExampleEncryptStringWithSalt() {
|
||||
// 为用户密码加盐,通常使用用户唯一标识(如邮箱)作为盐值
|
||||
hash := md5.EncryptStringWithSalt("password123", "main@example.com")
|
||||
fmt.Println("盐值哈希长度:", len(hash))
|
||||
fmt.Println("是否为有效哈希:", md5.VerifyMD5WithSalt("password123", "main@example.com", hash))
|
||||
// Output:
|
||||
// 盐值哈希长度: 32
|
||||
// 是否为有效哈希: true
|
||||
}
|
||||
|
||||
func ExampleGet16() {
|
||||
// 获取16位MD5,适合不需要完全防碰撞场景
|
||||
hash := md5.Get16("HelloWorld")
|
||||
fmt.Println(hash)
|
||||
// Output: f0f40ca72a15e05c
|
||||
}
|
||||
206
pkg/lzkit/md5/md5.go
Normal file
206
pkg/lzkit/md5/md5.go
Normal file
@@ -0,0 +1,206 @@
|
||||
package md5
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// MD5结构体,可用于链式调用
|
||||
type MD5 struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
// New 创建一个新的MD5实例
|
||||
func New() *MD5 {
|
||||
return &MD5{
|
||||
data: []byte{},
|
||||
}
|
||||
}
|
||||
|
||||
// FromString 从字符串创建MD5
|
||||
func FromString(s string) *MD5 {
|
||||
return &MD5{
|
||||
data: []byte(s),
|
||||
}
|
||||
}
|
||||
|
||||
// FromBytes 从字节切片创建MD5
|
||||
func FromBytes(b []byte) *MD5 {
|
||||
return &MD5{
|
||||
data: b,
|
||||
}
|
||||
}
|
||||
|
||||
// Add 向MD5中添加字符串
|
||||
func (m *MD5) Add(s string) *MD5 {
|
||||
m.data = append(m.data, []byte(s)...)
|
||||
return m
|
||||
}
|
||||
|
||||
// AddBytes 向MD5中添加字节切片
|
||||
func (m *MD5) AddBytes(b []byte) *MD5 {
|
||||
m.data = append(m.data, b...)
|
||||
return m
|
||||
}
|
||||
|
||||
// Sum 计算并返回MD5哈希值(16进制字符串)
|
||||
func (m *MD5) Sum() string {
|
||||
hash := md5.New()
|
||||
hash.Write(m.data)
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
|
||||
// SumBytes 计算并返回MD5哈希值(字节切片)
|
||||
func (m *MD5) SumBytes() []byte {
|
||||
hash := md5.New()
|
||||
hash.Write(m.data)
|
||||
return hash.Sum(nil)
|
||||
}
|
||||
|
||||
// 直接调用的工具函数
|
||||
|
||||
// EncryptString 加密字符串
|
||||
func EncryptString(s string) string {
|
||||
hash := md5.New()
|
||||
hash.Write([]byte(s))
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
|
||||
// EncryptBytes 加密字节切片
|
||||
func EncryptBytes(b []byte) string {
|
||||
hash := md5.New()
|
||||
hash.Write(b)
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
|
||||
// EncryptFile 加密文件内容
|
||||
func EncryptFile(filePath string) (string, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
hash := md5.New()
|
||||
if _, err := io.Copy(hash, file); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(hash.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// EncryptFileChunk 对大文件分块计算MD5,提高效率
|
||||
func EncryptFileChunk(filePath string, chunkSize int) (string, error) {
|
||||
if chunkSize <= 0 {
|
||||
chunkSize = 1024 * 1024 // 默认1MB
|
||||
}
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
hash := md5.New()
|
||||
buf := make([]byte, chunkSize)
|
||||
reader := bufio.NewReader(file)
|
||||
|
||||
for {
|
||||
n, err := reader.Read(buf)
|
||||
if err != nil && err != io.EOF {
|
||||
return "", err
|
||||
}
|
||||
if n == 0 {
|
||||
break
|
||||
}
|
||||
hash.Write(buf[:n])
|
||||
}
|
||||
|
||||
return hex.EncodeToString(hash.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// EncryptStringWithSalt 使用盐值加密字符串
|
||||
func EncryptStringWithSalt(s, salt string) string {
|
||||
return EncryptString(s + salt)
|
||||
}
|
||||
|
||||
// EncryptStringWithPrefix 使用前缀加密字符串
|
||||
func EncryptStringWithPrefix(s, prefix string) string {
|
||||
return EncryptString(prefix + s)
|
||||
}
|
||||
|
||||
// VerifyMD5 验证字符串的MD5哈希是否匹配
|
||||
func VerifyMD5(s, hash string) bool {
|
||||
return EncryptString(s) == strings.ToLower(hash)
|
||||
}
|
||||
|
||||
// VerifyMD5WithSalt 验证带盐值的字符串MD5哈希是否匹配
|
||||
func VerifyMD5WithSalt(s, salt, hash string) bool {
|
||||
return EncryptStringWithSalt(s, salt) == strings.ToLower(hash)
|
||||
}
|
||||
|
||||
// VerifyFileMD5 验证文件的MD5哈希是否匹配
|
||||
func VerifyFileMD5(filePath, hash string) (bool, error) {
|
||||
fileHash, err := EncryptFile(filePath)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return fileHash == strings.ToLower(hash), nil
|
||||
}
|
||||
|
||||
// MD5格式化为指定位数
|
||||
|
||||
// Get16 获取16位MD5值(取32位结果的中间16位)
|
||||
func Get16(s string) string {
|
||||
result := EncryptString(s)
|
||||
return result[8:24]
|
||||
}
|
||||
|
||||
// Get8 获取8位MD5值
|
||||
func Get8(s string) string {
|
||||
result := EncryptString(s)
|
||||
return result[12:20]
|
||||
}
|
||||
|
||||
// MD5主要用于校验而非安全存储,对于需要高安全性的场景,应考虑:
|
||||
// 1. bcrypt, scrypt或Argon2等专门为密码设计的算法
|
||||
// 2. HMAC-MD5等方式以防御彩虹表攻击
|
||||
// 3. 加盐并使用多次哈希迭代提高安全性
|
||||
|
||||
// MD5HMAC 使用HMAC-MD5算法
|
||||
func MD5HMAC(message, key string) string {
|
||||
hash := md5.New()
|
||||
|
||||
// 如果key长度超出block size,先进行哈希
|
||||
if len(key) > 64 {
|
||||
hash.Write([]byte(key))
|
||||
key = hex.EncodeToString(hash.Sum(nil))
|
||||
hash.Reset()
|
||||
}
|
||||
|
||||
// 内部填充
|
||||
k_ipad := make([]byte, 64)
|
||||
k_opad := make([]byte, 64)
|
||||
copy(k_ipad, []byte(key))
|
||||
copy(k_opad, []byte(key))
|
||||
|
||||
for i := 0; i < 64; i++ {
|
||||
k_ipad[i] ^= 0x36
|
||||
k_opad[i] ^= 0x5c
|
||||
}
|
||||
|
||||
// 内部哈希
|
||||
hash.Write(k_ipad)
|
||||
hash.Write([]byte(message))
|
||||
innerHash := hash.Sum(nil)
|
||||
hash.Reset()
|
||||
|
||||
// 外部哈希
|
||||
hash.Write(k_opad)
|
||||
hash.Write(innerHash)
|
||||
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
192
pkg/lzkit/md5/md5_test.go
Normal file
192
pkg/lzkit/md5/md5_test.go
Normal file
@@ -0,0 +1,192 @@
|
||||
package md5
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEncryptString(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"", "d41d8cd98f00b204e9800998ecf8427e"},
|
||||
{"hello", "5d41402abc4b2a76b9719d911017c592"},
|
||||
{"123456", "e10adc3949ba59abbe56e057f20f883e"},
|
||||
{"Hello World!", "ed076287532e86365e841e92bfc50d8c"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := EncryptString(test.input)
|
||||
fmt.Println(result)
|
||||
if result != test.expected {
|
||||
t.Errorf("EncryptString(%s) = %s; want %s", test.input, result, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptBytes(t *testing.T) {
|
||||
tests := []struct {
|
||||
input []byte
|
||||
expected string
|
||||
}{
|
||||
{[]byte(""), "d41d8cd98f00b204e9800998ecf8427e"},
|
||||
{[]byte("hello"), "5d41402abc4b2a76b9719d911017c592"},
|
||||
{[]byte{0, 1, 2, 3, 4}, "5267768822ee624d48fce15ec5ca79b6"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := EncryptBytes(test.input)
|
||||
if result != test.expected {
|
||||
t.Errorf("EncryptBytes(%v) = %s; want %s", test.input, result, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMD5Chain(t *testing.T) {
|
||||
// 测试链式调用
|
||||
result := New().
|
||||
Add("hello").
|
||||
Add(" ").
|
||||
Add("world").
|
||||
Sum()
|
||||
|
||||
expected := "fc5e038d38a57032085441e7fe7010b0" // MD5("hello world")
|
||||
if result != expected {
|
||||
t.Errorf("Chain MD5 = %s; want %s", result, expected)
|
||||
}
|
||||
|
||||
// 测试从字符串初始化
|
||||
result = FromString("hello").Add(" world").Sum()
|
||||
if result != expected {
|
||||
t.Errorf("FromString MD5 = %s; want %s", result, expected)
|
||||
}
|
||||
|
||||
// 测试从字节切片初始化
|
||||
result = FromBytes([]byte("hello")).AddBytes([]byte(" world")).Sum()
|
||||
if result != expected {
|
||||
t.Errorf("FromBytes MD5 = %s; want %s", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyMD5(t *testing.T) {
|
||||
if !VerifyMD5("hello", "5d41402abc4b2a76b9719d911017c592") {
|
||||
t.Error("VerifyMD5 failed for correct match")
|
||||
}
|
||||
|
||||
if VerifyMD5("hello", "wrong-hash") {
|
||||
t.Error("VerifyMD5 succeeded for incorrect match")
|
||||
}
|
||||
|
||||
// 测试大小写不敏感
|
||||
if !VerifyMD5("hello", "5D41402ABC4B2A76B9719D911017C592") {
|
||||
t.Error("VerifyMD5 failed for uppercase hash")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaltAndPrefix(t *testing.T) {
|
||||
// 测试加盐
|
||||
saltResult := EncryptStringWithSalt("password", "salt123")
|
||||
expectedSalt := EncryptString("passwordsalt123")
|
||||
if saltResult != expectedSalt {
|
||||
t.Errorf("EncryptStringWithSalt = %s; want %s", saltResult, expectedSalt)
|
||||
}
|
||||
|
||||
// 测试前缀
|
||||
prefixResult := EncryptStringWithPrefix("password", "prefix123")
|
||||
expectedPrefix := EncryptString("prefix123password")
|
||||
if prefixResult != expectedPrefix {
|
||||
t.Errorf("EncryptStringWithPrefix = %s; want %s", prefixResult, expectedPrefix)
|
||||
}
|
||||
|
||||
// 验证带盐值的MD5
|
||||
if !VerifyMD5WithSalt("password", "salt123", saltResult) {
|
||||
t.Error("VerifyMD5WithSalt failed for correct match")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGet16And8(t *testing.T) {
|
||||
full := EncryptString("test-string")
|
||||
|
||||
// 测试16位MD5
|
||||
result16 := Get16("test-string")
|
||||
expected16 := full[8:24]
|
||||
if result16 != expected16 {
|
||||
t.Errorf("Get16 = %s; want %s", result16, expected16)
|
||||
}
|
||||
|
||||
// 测试8位MD5
|
||||
result8 := Get8("test-string")
|
||||
expected8 := full[12:20]
|
||||
if result8 != expected8 {
|
||||
t.Errorf("Get8 = %s; want %s", result8, expected8)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMD5HMAC(t *testing.T) {
|
||||
// 已知的HMAC-MD5结果
|
||||
tests := []struct {
|
||||
message string
|
||||
key string
|
||||
expected string
|
||||
}{
|
||||
{"message", "key", "4e4748e62b463521f6775fbf921234b5"},
|
||||
{"test", "secret", "8b11d99898918564dda1a9fe205b5310"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := MD5HMAC(test.message, test.key)
|
||||
if result != test.expected {
|
||||
t.Errorf("MD5HMAC(%s, %s) = %s; want %s",
|
||||
test.message, test.key, result, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptFile(t *testing.T) {
|
||||
// 创建临时测试文件
|
||||
content := []byte("test file content for MD5")
|
||||
tmpFile, err := os.CreateTemp("", "md5test-*.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("无法创建临时文件: %v", err)
|
||||
}
|
||||
defer os.Remove(tmpFile.Name())
|
||||
|
||||
if _, err := tmpFile.Write(content); err != nil {
|
||||
t.Fatalf("无法写入临时文件: %v", err)
|
||||
}
|
||||
if err := tmpFile.Close(); err != nil {
|
||||
t.Fatalf("无法关闭临时文件: %v", err)
|
||||
}
|
||||
|
||||
// 计算文件MD5
|
||||
fileHash, err := EncryptFile(tmpFile.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("计算文件MD5失败: %v", err)
|
||||
}
|
||||
|
||||
// 验证文件MD5
|
||||
expectedHash := EncryptBytes(content)
|
||||
if fileHash != expectedHash {
|
||||
t.Errorf("文件MD5 = %s; 应为 %s", fileHash, expectedHash)
|
||||
}
|
||||
|
||||
// 测试VerifyFileMD5
|
||||
match, err := VerifyFileMD5(tmpFile.Name(), expectedHash)
|
||||
if err != nil {
|
||||
t.Fatalf("验证文件MD5失败: %v", err)
|
||||
}
|
||||
if !match {
|
||||
t.Error("VerifyFileMD5返回false,应返回true")
|
||||
}
|
||||
|
||||
// 测试不匹配的情况
|
||||
match, err = VerifyFileMD5(tmpFile.Name(), "wronghash")
|
||||
if err != nil {
|
||||
t.Fatalf("验证文件MD5失败: %v", err)
|
||||
}
|
||||
if match {
|
||||
t.Error("VerifyFileMD5对错误的哈希返回true,应返回false")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user