https://github.com/melf-xyzh/go-rsa
Go语言常用的rsa方法封装,RSA加密解密,sha1withrsa签名验签,PFX证书读取
https://github.com/melf-xyzh/go-rsa
decrypt encrypt go golang pfx rsa sha1withrsa sign verify
Last synced: 5 months ago
JSON representation
Go语言常用的rsa方法封装,RSA加密解密,sha1withrsa签名验签,PFX证书读取
- Host: GitHub
- URL: https://github.com/melf-xyzh/go-rsa
- Owner: melf-xyzh
- License: apache-2.0
- Created: 2022-06-07T04:33:20.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-06-08T01:39:23.000Z (about 4 years ago)
- Last Synced: 2024-06-21T18:50:28.565Z (almost 2 years ago)
- Topics: decrypt, encrypt, go, golang, pfx, rsa, sha1withrsa, sign, verify
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 12
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## go-rsa
### 安装
```bash
go get github.com/melf-xyzh/go-rsa
```
### 使用方法
### sha1withrsa
##### 签名
```go
// 需要签名的文本
context := "helloworld"
// 签名
sign, err := sha1withrsa.Sign(privateKey, context)
if err != nil {
log.Fatalln(err)
}
fmt.Println(sign)
```
##### 验签
```go
// 验签
verify, err := sha1withrsa.RSAVerify(publicKey, context, sign)
if err != nil {
return
}
fmt.Println(verify)
```
### 加密 / 解密
```go
// 加密
encrypt, err := myrsa.RsaEncrypt(publicKey, content)
if err != nil {
return
}
// 解密
data, err := myrsa.RsaDecrypt(privateKey,encrypt)
if err != nil {
return
}
```
### 生成读取公私钥文件(pem格式 / PKCS1)
```go
// 生成公钥文件(pem格式)
err = myrsa.CreatePrivatePem(privateKey, "")
if err != nil {
log.Fatal(err)
}
// 读取私钥文件(pem格式)
err = myrsa.CreatePublicPem(publicKey, "")
if err != nil {
log.Fatal(err)
}
// 读取私钥文件(pem格式)
privateKey, err = myrsa.ReadPrivatePem("cert/private.pem")
if err != nil {
log.Fatal(err)
}
// 读取公钥文件(pem格式)
publicKey, err = myrsa.ReadPublicPem("cert/public.pem")
if err != nil {
log.Fatal(err)
}
```
### 生成读取公私钥文件(ec格式 / PKCS8)
```go
// 生成私钥文件(ec格式)
err = myrsa.CreatePrivateEC(privateKey, "cert/private.key")
if err != nil {
log.Fatal(err)
}
// 生成公钥文件(ec格式)
err = myrsa.CreatePublicEC(publicKey, "cert/public.key")
if err != nil {
log.Fatal(err)
}
// 读取私钥文件(ec格式)
privateKey, err = myrsa.ReadPrivateEC("cert/private.key")
if err != nil {
log.Fatal(err)
}
// 读取公钥文件(ec格式)
publicKey, err = myrsa.ReadPublicEC("cert/public.key")
if err != nil {
log.Fatal(err)
}
```
### 生成证书文件(CA)
```go
// 生成CA证书
err = myrsa.CreateCertificate(publicKey, privateKey, "cert/ca/")
if err != nil {
log.Fatal(err)
}
// 导入CA证书
certificate, err := myrsa.LoadCertificate("cert/ca/ca.crt")
if err != nil {
log.Fatal(err)
}
```
### PFX证书
```go
// 从PFX证书中解析公私钥
privateKey, publicKey, err := pfx.GetPublicAndPrivateKeyFromPfx("zhengshu.pfx", "123456")
if err != nil {
return
}
pfx.PrintPublicKey(publicKey)
pfx.PrintPrivateKey(privateKey)
```