package main
import ("crypto/rsa""crypto/rand""crypto/x509""encoding/pem""os""crypto/sha256""crypto""fmt"
)func main() {sourceData := []byte("这一份机密文件")signData, err := SignatureRSA(sourceData)if err != nil {fmt.Println("加密出错:", err)}err = VerifyRSA(sourceData, signData)if err != nil {fmt.Println("校验出错:", err)}fmt.Println("校验正确:")
}func SignatureRSA(sourceData []byte) ([]byte, error) {msg := []byte("")//从文件读取私钥file, err := os.Open("E:/go/src/sign/privateKey.pem")if err != nil {return msg, err}info, err := file.Stat()if err != nil {return msg, err}buf := make([]byte, info.Size())file.Read(buf)//解析block, _ := pem.Decode(buf)privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)if err != nil {return msg, err}//哈希加密myHash := sha256.New()myHash.Write(sourceData)hashRes := myHash.Sum(nil)//对哈希结果进行签名res, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hashRes)if err != nil {return msg, err}defer file.Close()return res, nil
}func VerifyRSA(sourceData, signedData []byte) error {file, err := os.Open("E:/go/src/sign/publicKey.pem")if err != nil {return err}info, err := file.Stat()if err != nil {return err}buf := make([]byte, info.Size())file.Read(buf)//pem解密block, _ := pem.Decode(buf)publicInterface, err := x509.ParsePKIXPublicKey(block.Bytes)if err != nil {return err}publicKey := publicInterface.(*rsa.PublicKey)//元数据哈希加密mySha := sha256.New()mySha.Write(sourceData)res := mySha.Sum(nil)//校验签名err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, res, signedData)if err != nil {return err}defer file.Close()return nil}//生成密钥对
func GenerateRsaKey(bit int) error {private, err := rsa.GenerateKey(rand.Reader, bit)if err != nil {return err}//x509私钥序列化privateStream := x509.MarshalPKCS1PrivateKey(private)//将私钥设置到pem结构中block := pem.Block{Type: "Rsa Private Key",Bytes: privateStream,}//保存磁盘file, err := os.Create("E:/go/src/sign/privateKey.pem")if err != nil {return err}//pem编码err = pem.Encode(file, &block)if err != nil {return err}//=========public=========public := private.PublicKey//509序列化publicStream, err := x509.MarshalPKIXPublicKey(&public)if err != nil {return err}//公钥赋值pem结构体pubblock := pem.Block{Type: "Rsa Public Key", Bytes: publicStream,}//保存磁盘pubfile, err := os.Create("E:/go/src/sign/publicKey.pem")if err != nil {return err}//pem编码err = pem.Encode(pubfile, &pubblock)if err != nil {return err}return nil}
欢迎加微信一起学习:13671593005
如有错误,敬请留言指点,我立马改正,以免误导他人~谢谢!