Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jun 1, 2023
1 parent dbd0e88 commit cfeda1c
Show file tree
Hide file tree
Showing 38 changed files with 686 additions and 259 deletions.
2 changes: 1 addition & 1 deletion app/example/key/key_ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Ecdsa struct {
func (this Ecdsa) Make() {
for _, curve := range curves {
obj := cryptobin_ecdsa.New().
WithCurve(curve).
SetCurve(curve).
GenerateKey()

this.pkcs1(obj, curve)
Expand Down
4 changes: 2 additions & 2 deletions app/example/key/key_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func MakeEcdsaUnenSSHKey(curve string) {

// P521 | P384 | P256
obj := cryptobin_ecdsa.New().
WithCurve(curve).
SetCurve(curve).
GenerateKey()

block, _ := cryptobin_ssh.MarshalOpenSSHPrivateKey(
Expand Down Expand Up @@ -213,7 +213,7 @@ func MakeEcdsaSSHKey(name string, curve string) {

// P521 | P384 | P256
obj := cryptobin_ecdsa.New().
WithCurve(curve).
SetCurve(curve).
GenerateKey()

block, _ := cryptobin_ssh.MarshalOpenSSHPrivateKeyWithPassword(
Expand Down
36 changes: 27 additions & 9 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/dsa/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,34 @@ var (
)

// 生成私钥 pem 数据
// 使用:
// dsa := New().GenerateKey("L2048N256")
// priKey := dsa.CreatePrivateKey().ToKeyString()
func (this DSA) CreatePrivateKey() DSA {
return this.CreatePKCS1PrivateKey()
}

// 生成私钥带密码 pem 数据
// CreatePrivateKeyWithPassword("123", "AES256CBC")
// PEMCipher: DESCBC | DESEDE3CBC | AES128CBC | AES192CBC | AES256CBC
func (this DSA) CreatePrivateKeyWithPassword(password string, opts ...string) DSA {
return this.CreatePKCS1PrivateKeyWithPassword(password, opts...)
}

// 生成公钥 pem 数据
func (this DSA) CreatePublicKey() DSA {
return this.CreatePKCS1PublicKey()
}

// ==========

// 生成 pkcs1 私钥 pem 数据
func (this DSA) CreatePKCS1PrivateKey() DSA {
if this.privateKey == nil {
err := errors.New("dsa: privateKey error.")
return this.AppendError(err)
}

privateKeyBytes, err := cryptobin_dsa.MarshalPrivateKey(this.privateKey)
privateKeyBytes, err := cryptobin_dsa.MarshalPKCS1PrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -53,10 +71,10 @@ func (this DSA) CreatePrivateKey() DSA {
return this
}

// 生成私钥带密码 pem 数据
// CreatePrivateKeyWithPassword("123", "AES256CBC")
// 生成 pkcs1 私钥带密码 pem 数据
// CreatePKCS1PrivateKeyWithPassword("123", "AES256CBC")
// PEMCipher: DESCBC | DESEDE3CBC | AES128CBC | AES192CBC | AES256CBC
func (this DSA) CreatePrivateKeyWithPassword(password string, opts ...string) DSA {
func (this DSA) CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) DSA {
if this.privateKey == nil {
err := errors.New("dsa: privateKey error.")
return this.AppendError(err)
Expand All @@ -75,7 +93,7 @@ func (this DSA) CreatePrivateKeyWithPassword(password string, opts ...string) DS
}

// 生成私钥
x509PrivateKey, err := cryptobin_dsa.MarshalPrivateKey(this.privateKey)
x509PrivateKey, err := cryptobin_dsa.MarshalPKCS1PrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -97,14 +115,14 @@ func (this DSA) CreatePrivateKeyWithPassword(password string, opts ...string) DS
return this
}

// 生成公钥 pem 数据
func (this DSA) CreatePublicKey() DSA {
// 生成 pkcs1 公钥 pem 数据
func (this DSA) CreatePKCS1PublicKey() DSA {
if this.publicKey == nil {
err := errors.New("dsa: publicKey error.")
return this.AppendError(err)
}

publicKeyBytes, err := cryptobin_dsa.MarshalPublicKey(this.publicKey)
publicKeyBytes, err := cryptobin_dsa.MarshalPKCS1PublicKey(this.publicKey)
if err != nil {
return this.AppendError(err)
}
Expand Down
15 changes: 11 additions & 4 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/dsa/dsa.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package dsa

import (
"hash"
"crypto/dsa"
"crypto/sha256"
)

type (
// HashFunc
HashFunc = func() hash.Hash
)

/**
Expand All @@ -17,6 +24,9 @@ type DSA struct {
// 公钥
publicKey *dsa.PublicKey

// 签名验证类型
signHash HashFunc

// [私钥/公钥]数据
keyData []byte

Expand All @@ -26,9 +36,6 @@ type DSA struct {
// 解析后的数据
paredData []byte

// 签名验证类型
signHash string

// 验证结果
verify bool

Expand All @@ -39,7 +46,7 @@ type DSA struct {
// 构造函数
func NewDSA() DSA {
return DSA{
signHash: "SHA512",
signHash: sha256.New,
verify: false,
Errors: make([]error, 0),
}
Expand Down
67 changes: 64 additions & 3 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/dsa/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import (

// 私钥
func (this DSA) FromPrivateKey(key []byte) DSA {
parsedKey, err := this.ParsePrivateKeyFromPEM(key)
parsedKey, err := this.ParsePKCS8PrivateKeyFromPEM(key)
if err == nil {
this.privateKey = parsedKey

return this
}

parsedKey, err = this.ParsePKCS1PrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -21,7 +28,14 @@ func (this DSA) FromPrivateKey(key []byte) DSA {

// 私钥带密码
func (this DSA) FromPrivateKeyWithPassword(key []byte, password string) DSA {
parsedKey, err := this.ParsePrivateKeyFromPEMWithPassword(key, password)
parsedKey, err := this.ParsePKCS8PrivateKeyFromPEMWithPassword(key, password)
if err == nil {
this.privateKey = parsedKey

return this
}

parsedKey, err = this.ParsePKCS1PrivateKeyFromPEMWithPassword(key, password)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -33,7 +47,14 @@ func (this DSA) FromPrivateKeyWithPassword(key []byte, password string) DSA {

// 公钥
func (this DSA) FromPublicKey(key []byte) DSA {
parsedKey, err := this.ParsePublicKeyFromPEM(key)
parsedKey, err := this.ParsePKCS8PublicKeyFromPEM(key)
if err == nil {
this.publicKey = parsedKey

return this
}

parsedKey, err = this.ParsePKCS1PublicKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -43,6 +64,8 @@ func (this DSA) FromPublicKey(key []byte) DSA {
return this
}

// ==========

// 生成密钥
// 可用参数 [L1024N160 | L2048N224 | L2048N256 | L3072N256]
func (this DSA) GenerateKey(ln string) DSA {
Expand Down Expand Up @@ -74,6 +97,44 @@ func (this DSA) GenerateKey(ln string) DSA {

// ==========

// PKCS1 私钥
func (this DSA) FromPKCS1PrivateKey(key []byte) DSA {
parsedKey, err := this.ParsePKCS1PrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}

this.privateKey = parsedKey

return this
}

// PKCS1 私钥带密码
func (this DSA) FromPKCS1PrivateKeyWithPassword(key []byte, password string) DSA {
parsedKey, err := this.ParsePKCS1PrivateKeyFromPEMWithPassword(key, password)
if err != nil {
return this.AppendError(err)
}

this.privateKey = parsedKey

return this
}

// PKCS1 公钥
func (this DSA) FromPKCS1PublicKey(key []byte) DSA {
parsedKey, err := this.ParsePKCS1PublicKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}

this.publicKey = parsedKey

return this
}

// ==========

// PKCS8 私钥
func (this DSA) FromPKCS8PrivateKey(key []byte) DSA {
parsedKey, err := this.ParsePKCS8PrivateKeyFromPEM(key)
Expand Down
10 changes: 5 additions & 5 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/dsa/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func (this DSA) GetPublicKey() *dsa.PublicKey {
return this.publicKey
}

// 获取 hash 类型
func (this DSA) GetSignHash() HashFunc {
return this.signHash
}

// 获取 keyData
func (this DSA) GetKeyData() []byte {
return this.keyData
Expand All @@ -29,11 +34,6 @@ func (this DSA) GetParedData() []byte {
return this.paredData
}

// 获取 hash 类型
func (this DSA) GetSignHash() string {
return this.signHash
}

// 获取验证后情况
func (this DSA) GetVerify() bool {
return this.verify
Expand Down
13 changes: 7 additions & 6 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/dsa/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
)

// 解析私钥
func (this DSA) ParsePrivateKeyFromPEM(key []byte) (*dsa.PrivateKey, error) {
func (this DSA) ParsePKCS1PrivateKeyFromPEM(key []byte) (*dsa.PrivateKey, error) {
var err error

// Parse PEM block
Expand All @@ -28,7 +28,7 @@ func (this DSA) ParsePrivateKeyFromPEM(key []byte) (*dsa.PrivateKey, error) {

// Parse the key
var parsedKey any
if parsedKey, err = cryptobin_dsa.ParsePrivateKey(block.Bytes); err != nil {
if parsedKey, err = cryptobin_dsa.ParsePKCS1PrivateKey(block.Bytes); err != nil {
return nil, err
}

Expand All @@ -42,7 +42,7 @@ func (this DSA) ParsePrivateKeyFromPEM(key []byte) (*dsa.PrivateKey, error) {
}

// 解析私钥带密码
func (this DSA) ParsePrivateKeyFromPEMWithPassword(key []byte, password string) (*dsa.PrivateKey, error) {
func (this DSA) ParsePKCS1PrivateKeyFromPEMWithPassword(key []byte, password string) (*dsa.PrivateKey, error) {
var err error

// Parse PEM block
Expand All @@ -58,7 +58,7 @@ func (this DSA) ParsePrivateKeyFromPEMWithPassword(key []byte, password string)

// Parse the key
var parsedKey any
if parsedKey, err = cryptobin_dsa.ParsePrivateKey(blockDecrypted); err != nil {
if parsedKey, err = cryptobin_dsa.ParsePKCS1PrivateKey(blockDecrypted); err != nil {
return nil, err
}

Expand All @@ -72,7 +72,7 @@ func (this DSA) ParsePrivateKeyFromPEMWithPassword(key []byte, password string)
}

// 解析公钥
func (this DSA) ParsePublicKeyFromPEM(key []byte) (*dsa.PublicKey, error) {
func (this DSA) ParsePKCS1PublicKeyFromPEM(key []byte) (*dsa.PublicKey, error) {
var err error

// Parse PEM block
Expand All @@ -83,7 +83,7 @@ func (this DSA) ParsePublicKeyFromPEM(key []byte) (*dsa.PublicKey, error) {

// Parse the key
var parsedKey any
if parsedKey, err = cryptobin_dsa.ParsePublicKey(block.Bytes); err != nil {
if parsedKey, err = cryptobin_dsa.ParsePKCS1PublicKey(block.Bytes); err != nil {
if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
parsedKey = cert.PublicKey
} else {
Expand Down Expand Up @@ -179,6 +179,7 @@ func (this DSA) ParsePKCS8PublicKeyFromPEM(key []byte) (*dsa.PublicKey, error) {

var pkey *dsa.PublicKey
var ok bool

if pkey, ok = parsedKey.(*dsa.PublicKey); !ok {
return nil, ErrNotDSAPublicKey
}
Expand Down
13 changes: 8 additions & 5 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/dsa/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (this DSA) SignAsn1() DSA {
paredData, err := asn1.Marshal(DSASignature{r, s})

this.paredData = paredData

return this.AppendError(err)
}

Expand Down Expand Up @@ -178,9 +178,9 @@ func (this DSA) SignHex() DSA {
sign := encoding.HexPadding(rHex, 64) + encoding.HexPadding(sHex, 64)

paredData, err := encoding.HexDecode(sign)

this.paredData = paredData

return this.AppendError(err)
}

Expand Down Expand Up @@ -279,6 +279,9 @@ func (this DSA) VerifyBytes(data []byte) DSA {
// ===============

// 签名后数据
func (this DSA) DataHash(signHash string, data []byte) ([]byte, error) {
return cryptobin_tool.HashSum(signHash, data)
func (this DSA) DataHash(fn HashFunc, data []byte) ([]byte, error) {
h := fn()
h.Write(data)

return h.Sum(nil), nil
}
Loading

0 comments on commit cfeda1c

Please sign in to comment.