You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
4.6 KiB
190 lines
4.6 KiB
package crypto
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/base64"
|
|
"encoding/pem"
|
|
"errors"
|
|
"fmt"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"os"
|
|
)
|
|
|
|
// KeyPair 表示生成的密钥对
|
|
type KeyPair struct {
|
|
PrivateKey *rsa.PrivateKey
|
|
PublicKey *rsa.PublicKey
|
|
}
|
|
|
|
// GenerateRSAKeyPair 生成指定位数的RSA密钥对
|
|
func GenerateRSAKeyPair(bits int) (*KeyPair, error) {
|
|
// 生成RSA私钥
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("生成RSA密钥失败: %v", err)
|
|
}
|
|
|
|
// 验证私钥
|
|
err = privateKey.Validate()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("验证RSA密钥失败: %v", err)
|
|
}
|
|
|
|
// 获取公钥
|
|
publicKey := &privateKey.PublicKey
|
|
|
|
return &KeyPair{
|
|
PrivateKey: privateKey,
|
|
PublicKey: publicKey,
|
|
}, nil
|
|
}
|
|
|
|
// EncryptPrivateKey 使用密码加密私钥
|
|
func EncryptPrivateKey(privateKey *rsa.PrivateKey, password string) ([]byte, error) {
|
|
if password == "" {
|
|
return nil, errors.New("密码不能为空")
|
|
}
|
|
|
|
// 将私钥转换为PKCS#1格式
|
|
privDER := x509.MarshalPKCS1PrivateKey(privateKey)
|
|
|
|
// 使用密码加密私钥
|
|
block, err := x509.EncryptPEMBlock(
|
|
rand.Reader,
|
|
"RSA PRIVATE KEY",
|
|
privDER,
|
|
[]byte(password),
|
|
x509.PEMCipherAES256,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("加密私钥失败: %v", err)
|
|
}
|
|
|
|
// 将加密后的私钥转换为PEM格式
|
|
encprivPEM := pem.EncodeToMemory(block)
|
|
|
|
return encprivPEM, nil
|
|
}
|
|
|
|
// ExportPublicKey 将公钥导出为PEM格式
|
|
func ExportPublicKey(publicKey *rsa.PublicKey) ([]byte, error) {
|
|
// 将公钥转换为DER格式
|
|
pubDER, err := x509.MarshalPKIXPublicKey(publicKey)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("编码公钥失败: %v", err)
|
|
}
|
|
|
|
// 创建PEM块
|
|
block := &pem.Block{
|
|
Type: "RSA PUBLIC KEY",
|
|
Bytes: pubDER,
|
|
}
|
|
|
|
// 将公钥转换为PEM格式
|
|
pubPEM := pem.EncodeToMemory(block)
|
|
|
|
return pubPEM, nil
|
|
}
|
|
|
|
// HashPassword 对密码进行哈希处理
|
|
func HashPassword(password string) (string, error) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(hash), nil
|
|
}
|
|
|
|
// CheckPasswordHash 验证密码是否与哈希值匹配
|
|
func CheckPasswordHash(password, hash string) bool {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
return err == nil
|
|
}
|
|
|
|
// GenerateKeysWithPassword 生成加密的密钥对,返回公钥和加密后的私钥
|
|
func GenerateKeysWithPassword(bits int, password string) (publicKeyPEM []byte, encryptedPrivateKeyPEM []byte, err error) {
|
|
// 生成密钥对
|
|
keyPair, err := GenerateRSAKeyPair(bits)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
// 加密私钥
|
|
encryptedPrivateKeyPEM, err = EncryptPrivateKey(keyPair.PrivateKey, password)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
// 导出公钥
|
|
publicKeyPEM, err = ExportPublicKey(keyPair.PublicKey)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return publicKeyPEM, encryptedPrivateKeyPEM, nil
|
|
}
|
|
|
|
// DecryptPrivateKey 使用密码解密私钥
|
|
func DecryptPrivateKey(encryptedPrivateKeyPEM []byte, password string) (*rsa.PrivateKey, error) {
|
|
if password == "" {
|
|
return nil, errors.New("密码不能为空")
|
|
}
|
|
|
|
// 解码PEM块
|
|
block, _ := pem.Decode(encryptedPrivateKeyPEM)
|
|
if block == nil {
|
|
return nil, errors.New("无法解码PEM块")
|
|
}
|
|
|
|
// 解密私钥
|
|
decryptedDER, err := x509.DecryptPEMBlock(block, []byte(password))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("解密私钥失败: %v", err)
|
|
}
|
|
|
|
// 解析私钥
|
|
privateKey, err := x509.ParsePKCS1PrivateKey(decryptedDER)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("解析私钥失败: %v", err)
|
|
}
|
|
|
|
return privateKey, nil
|
|
}
|
|
|
|
// SaveKeyToFile 将密钥数据保存到指定文件
|
|
func SaveKeyToFile(filePath string, keyData []byte) error {
|
|
// 写入文件
|
|
err := os.WriteFile(filePath, keyData, 0600) // 仅用户可读写权限
|
|
if err != nil {
|
|
return fmt.Errorf("保存密钥到文件失败: %v", err)
|
|
}
|
|
|
|
fmt.Printf("密钥已成功保存到: %s\n", filePath)
|
|
return nil
|
|
}
|
|
|
|
// GenerateAndSaveKeysWithPassword 生成密钥对并用密码保护私钥,然后保存到文件
|
|
func GenerateAndSaveKeysWithPassword(bits int, password, privateKeyPath, publicKeyPath string) error {
|
|
// 生成密钥对
|
|
publicKeyPEM, encryptedPrivateKeyPEM, err := GenerateKeysWithPassword(bits, password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 保存公钥到文件
|
|
err = SaveKeyToFile(publicKeyPath, publicKeyPEM)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 保存加密后的私钥到文件
|
|
err = SaveKeyToFile(privateKeyPath, encryptedPrivateKeyPEM)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Println("密钥对已成功生成并保存!")
|
|
return nil
|
|
}
|