ca登录认证公钥校验
This commit is contained in:
34
core/utils/cakey.go
Normal file
34
core/utils/cakey.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 解析PKCS1公钥
|
||||||
|
// https://uutool.cn/rsa-generate/
|
||||||
|
func ParsePKCS1PublicKey(publicKeyPEM string) (*rsa.PublicKey, error) {
|
||||||
|
block, _ := pem.Decode([]byte(publicKeyPEM))
|
||||||
|
if block == nil {
|
||||||
|
return nil, fmt.Errorf("无效的公钥 -----BEGIN RSA PUBLIC KEY----- 编码")
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return pubKey, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证签名
|
||||||
|
func VerifySignature(publicKey *rsa.PublicKey, data, signature []byte) bool {
|
||||||
|
hashed := sha256.Sum256(data)
|
||||||
|
err := rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, hashed[:], signature)
|
||||||
|
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user