-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
678 additions
and
299 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,135 @@ | ||
### DSA 使用说明 | ||
|
||
* 使用 [pkcs1 / pkcs8] 证书,默认为 pkcs1 证书 | ||
* 包引入 / import pkg | ||
~~~go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/deatil/lakego-filesystem/filesystem" | ||
cryptobin_dsa "github.com/deatil/go-cryptobin/cryptobin/dsa" | ||
"github.com/deatil/go-cryptobin/cryptobin/dsa" | ||
) | ||
~~~ | ||
|
||
func main() { | ||
// 文件管理器 | ||
fs := filesystem.New() | ||
* 数据输入方式 / input funcs | ||
~~~go | ||
FromBytes(data []byte) | ||
FromString(data string) | ||
FromBase64String(data string) | ||
FromHexString(data string) | ||
~~~ | ||
|
||
* 数据输出方式 / output funcs | ||
~~~go | ||
ToBytes() | ||
ToString() | ||
ToBase64String() | ||
ToHexString() | ||
~~~ | ||
|
||
* 获取 error / get error | ||
~~~go | ||
Error() | ||
~~~ | ||
|
||
// 生成证书 | ||
* 生成证书 / make keys | ||
~~~go | ||
func main() { | ||
// 可用参数 [L1024N160 | L2048N224 | L2048N256 | L3072N256] | ||
dsa := cryptobin_dsa.New().GenerateKey("L2048N256") | ||
dsaPriKey := dsa. | ||
obj := dsa.New().GenerateKey("L2048N256") | ||
|
||
// 生成私钥 | ||
// create private key | ||
var PriKeyPem string = obj. | ||
CreatePrivateKey(). | ||
// CreatePrivateKeyWithPassword("123", "AES256CBC"). | ||
// CreatePKCS1PrivateKey(). | ||
// CreatePKCS1PrivateKeyWithPassword("123", "AES256CBC"). | ||
// CreatePKCS8PrivateKey(). | ||
// CreatePKCS8PrivateKeyWithPassword("123", "AES256CBC"). | ||
// CreatePKCS8PrivateKeyWithPassword("123", "AES256CBC", "SHA256"). | ||
// CreateXMLPrivateKey(). | ||
ToKeyString() | ||
dsaPubKey := dsa. | ||
CreatePublicKey(). | ||
|
||
// 自定义私钥加密类型 | ||
// use custom encrypt options | ||
var PriKeyPem string = obj. | ||
CreatePKCS8PrivateKeyWithPassword("123", rsa.Opts{ | ||
Cipher: rsa.GetCipherFromName("AES256CBC"), | ||
KDFOpts: rsa.ScryptOpts{ | ||
CostParameter: 1 << 15, | ||
BlockSize: 8, | ||
ParallelizationParameter: 1, | ||
SaltSize: 8, | ||
}, | ||
}). | ||
ToKeyString() | ||
|
||
// 生成公钥 | ||
// create public key | ||
var PubKeyPem string = obj. | ||
CreatePKCS1PublicKey(). | ||
// CreatePKCS8PublicKey(). | ||
// CreateXMLPublicKey(). | ||
ToKeyString() | ||
fs.Put("./runtime/key/dsa", dsaPriKey) | ||
fs.Put("./runtime/key/dsa.pub", dsaPubKey) | ||
} | ||
~~~ | ||
|
||
* 签名验证 / sign data | ||
~~~go | ||
func main() { | ||
obj := dsa.New() | ||
|
||
// 待签名数据 | ||
// no sign data | ||
var data string = "..." | ||
|
||
// 验证 | ||
dsa := cryptobin_dsa.New() | ||
// 签名数据 | ||
// sign data | ||
var sigBase64String string = "..." | ||
|
||
dsaPri, _ := fs.Get("./runtime/key/dsa") | ||
dsacypt := dsa. | ||
FromString("test-pass"). | ||
FromPrivateKey([]byte(dsaPri)). | ||
// FromPrivateKeyWithPassword([]byte(dsaPri), "123"). | ||
// FromPKCS8PrivateKey([]byte(dsaPri)). | ||
// FromPKCS8PrivateKeyWithPassword([]byte(dsaPri), "123"). | ||
// 私钥签名 | ||
// private key sign data | ||
var priKeyPem string = "" | ||
sigBase64String = obj. | ||
FromString(data). | ||
FromPrivateKey([]byte(priKeyPem)). | ||
// FromPrivateKeyWithPassword([]byte(priKeyPem), "123"). | ||
// FromPKCS1PrivateKey([]byte(priKeyPem)). | ||
// FromPKCS1PrivateKeyWithPassword([]byte(priKeyPem), "123"). | ||
// FromPKCS8PrivateKey([]byte(priKeyPem)). | ||
// FromPKCS8PrivateKeyWithPassword([]byte(priKeyPem), "123"). | ||
// FromXMLPrivateKey([]byte(priKeyXML)). | ||
SetSignHash("SHA256"). | ||
Sign(). | ||
ToBase64String() | ||
dsaPub, _ := fs.Get("./runtime/key/dsa.pub") | ||
dsacyptde := dsa. | ||
FromBase64String("MjkzNzYzMDE1NjgzNDExMTM0ODE1MzgxOTAxMDIxNzQ0Nzg3NTc3NTAxNTU2MDIwNzg4OTc1MzY4Mzc0OTE5NzcyOTg3NjI1MTc2OTErNDgzNDU3NDAyMzYyODAzMDM3MzE1NjE1NDk1NDEzOTQ4MDQ3NDQ3ODA0MDE4NDY5NDA1OTA3ODExNjM1Mzk3MDEzOTY4MTM5NDg2NDc="). | ||
FromPublicKey([]byte(dsaPub)). | ||
// FromPKCS8PublicKey([]byte(dsaPub)). | ||
Verify([]byte("test-pass")). | ||
|
||
// 公钥验证 | ||
// public key verify signed data | ||
var pubKeyPem string = "" | ||
var res bool = obj. | ||
FromBase64String(sigBase64String). | ||
FromPublicKey([]byte(pubKeyPem)). | ||
// FromPKCS1PublicKey([]byte(pubKeyPem)). | ||
// FromPKCS8PublicKey([]byte(pubKeyPem)). | ||
// FromXMLPublicKey([]byte(pubKeyXML)). | ||
SetSignHash("SHA256"). | ||
Verify([]byte(data)). | ||
ToVerify() | ||
} | ||
~~~ | ||
|
||
// 检测私钥公钥是否匹配 | ||
pri, _ := fs.Get(prifile) | ||
pub, _ := fs.Get(pubfile) | ||
* 检测私钥公钥是否匹配 / Check KeyPair | ||
~~~go | ||
func main() { | ||
var prikeyPem string = "..." | ||
var pubkeyPem string = "..." | ||
|
||
res := cryptobin_dsa.New(). | ||
FromPKCS8PrivateKey([]byte(pri)). | ||
// FromPrivateKey([]byte(pri)). | ||
// FromPrivateKeyWithPassword([]byte(pri), "123"). | ||
// FromPKCS8PrivateKeyWithPassword([]byte(pri), "123"). | ||
// FromPublicKey([]byte(pub)). | ||
FromPKCS8PublicKey([]byte(pub)). | ||
var res bool = dsa.New(). | ||
// FromPrivateKey([]byte(prikeyPem)). | ||
// FromPrivateKeyWithPassword([]byte(prikeyPem), "123"). | ||
// FromPKCS1PrivateKey([]byte(prikeyPem)). | ||
// FromPKCS1PrivateKeyWithPassword([]byte(prikeyPem), "123"). | ||
FromPKCS8PrivateKey([]byte(prikeyPem)). | ||
// FromPKCS8PrivateKeyWithPassword([]byte(prikeyPem), "123"). | ||
// FromPublicKey([]byte(pubkeyPem)). | ||
// FromPKCS1PublicKey([]byte(pubkeyPem)). | ||
FromPKCS8PublicKey([]byte(pubkeyPem)). | ||
CheckKeyPair() | ||
|
||
fmt.Printf("check res: %#v", res) | ||
|
||
} | ||
~~~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,104 @@ | ||
### ECDH 使用文档 | ||
|
||
该版本使用 go 标准库,go 最低版本需要 `1.20.1`。 | ||
|
||
* 包引入 / import pkg | ||
~~~go | ||
import ( | ||
"github.com/deatil/go-cryptobin/cryptobin/ecdh" | ||
) | ||
~~~ | ||
|
||
* ecdh 使用 | ||
* 数据输入方式 / input funcs | ||
~~~go | ||
package main | ||
FromBytes(data []byte) | ||
FromString(data string) | ||
FromBase64String(data string) | ||
FromHexString(data string) | ||
~~~ | ||
|
||
import ( | ||
"fmt" | ||
* 数据输出方式 / output funcs | ||
~~~go | ||
ToBytes() | ||
ToString() | ||
ToBase64String() | ||
ToHexString() | ||
~~~ | ||
|
||
"github.com/deatil/lakego-filesystem/filesystem" | ||
cryptobin_ecdh "github.com/deatil/go-cryptobin/cryptobin/ecdh" | ||
) | ||
* 获取 error / get error | ||
~~~go | ||
Error() | ||
~~~ | ||
|
||
* 生成证书 / make keys | ||
~~~go | ||
func main() { | ||
// 文件管理器 | ||
fs := filesystem.New() | ||
|
||
// 生成证书 | ||
// 可用参数 [P521 | P384 | P256 | X25519] | ||
obj := cryptobin_ecdh.New(). | ||
SetCurve("P256"). | ||
GenerateKey() | ||
obj := ecdh.New(). | ||
SetCurve("P256"). | ||
GenerateKey() | ||
|
||
// 私钥密码 | ||
// privatekey password | ||
var psssword string = "" | ||
|
||
objPriKey := obj. | ||
// 生成私钥 | ||
// create private key | ||
var PriKeyPem string = obj. | ||
CreatePrivateKey(). | ||
// CreatePrivateKeyWithPassword("123", "AES256CBC"). | ||
// CreatePrivateKeyWithPassword(psssword, "DESEDE3CBC"). | ||
ToKeyString() | ||
objPubKey := obj. | ||
|
||
// 自定义私钥加密类型 | ||
// use custom encrypt options | ||
var PriKeyPem string = obj. | ||
CreatePrivateKeyWithPassword(psssword, sm2.Opts{ | ||
Cipher: sm2.GetCipherFromName("AES256CBC"), | ||
KDFOpts: sm2.ScryptOpts{ | ||
CostParameter: 1 << 15, | ||
BlockSize: 8, | ||
ParallelizationParameter: 1, | ||
SaltSize: 8, | ||
}, | ||
}). | ||
ToKeyString() | ||
|
||
// 生成公钥 | ||
// create public key | ||
var PubKeyPem string = obj. | ||
CreatePublicKey(). | ||
ToKeyString() | ||
fs.Put("./runtime/key/ecdh/ecdh", objPriKey) | ||
fs.Put("./runtime/key/ecdh/ecdh.pub", objPubKey) | ||
} | ||
~~~ | ||
|
||
// 生成对称加密密钥 | ||
obj := cryptobin_ecdh.New() | ||
* 生成对称加密密钥 | ||
~~~go | ||
func main() { | ||
var prikeyPem1 string = "..." | ||
var pubkeyPem1 string = "..." | ||
|
||
objPri1, _ := fs.Get("./runtime/key/ecdh/ecdh") | ||
objPub1, _ := fs.Get("./runtime/key/ecdh/ecdh.pub") | ||
var prikeyPem2 string = "..." | ||
var pubkeyPem2 string = "..." | ||
|
||
objPri2, _ := fs.Get("./runtime/key/ecdh/ecdh2") | ||
objPub2, _ := fs.Get("./runtime/key/ecdh/ecdh2.pub") | ||
// 私钥密码 | ||
// privatekey password | ||
var psssword string = "" | ||
|
||
objSecret1 := obj. | ||
FromPrivateKey([]byte(objPri1)). | ||
// FromPrivateKeyWithPassword([]byte(objPri1), "123"). | ||
FromPublicKey([]byte(objPub2)). | ||
var secret1 string = obj. | ||
FromPrivateKey([]byte(prikeyPem1)). | ||
// FromPrivateKeyWithPassword([]byte(prikeyPem1), psssword). | ||
FromPublicKey([]byte(pubkeyPem2)). | ||
CreateSecretKey(). | ||
ToHexString() | ||
|
||
objSecret2 := obj. | ||
FromPrivateKey([]byte(objPri2)). | ||
// FromPrivateKeyWithPassword([]byte(objPri2), "123"). | ||
FromPublicKey([]byte(objPub1)). | ||
var secret2 string = obj. | ||
FromPrivateKey([]byte(prikeyPem2)). | ||
// FromPrivateKeyWithPassword([]byte(prikeyPem2), psssword). | ||
FromPublicKey([]byte(pubkeyPem1)). | ||
CreateSecretKey(). | ||
ToHexString() | ||
|
||
dhStatus := false | ||
if objSecret1 == objSecret2) { | ||
dhStatus = true | ||
status := false | ||
if secret1 == secret2) { | ||
status = true | ||
} | ||
|
||
fmt.Println("生成的密钥是否相同结果: ", dhStatus) | ||
} | ||
~~~ |
Oops, something went wrong.