Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed May 24, 2023
1 parent cdf1fd8 commit a0fa93c
Show file tree
Hide file tree
Showing 27 changed files with 313 additions and 565 deletions.
2 changes: 1 addition & 1 deletion app/example/cmd/make_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var MakeKeyCmd = &cobra.Command{

},
Run: func(cmd *cobra.Command, args []string) {
key.NewCurve25519().Make()
key.NewRsa().Make()

// key.KeyCheck()

Expand Down
11 changes: 6 additions & 5 deletions app/example/controller/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
_ "github.com/deatil/go-cryptobin/pkcs7/encrypt"
_ "github.com/deatil/go-cryptobin/pkcs8"
_ "github.com/deatil/go-cryptobin/pkcs8pbe"
_ "github.com/deatil/go-cryptobin/pkcs8s"
_ "github.com/deatil/go-cryptobin/pkcs12"
_ "github.com/deatil/go-cryptobin/jceks"
_ "github.com/deatil/go-cryptobin/cryptobin/ca"
Expand Down Expand Up @@ -396,16 +397,16 @@ func (this *Data) Error(ctx *gin.Context) {
// 验证
obj2 := cryptobin_rsa.New()

obj2Pri, _ := fs.Get("./runtime/key/rsa/xml/rsa_xml")
obj2Pri, _ := fs.Get("./runtime/key/key-pem/rsa/2048/rsa-pkcs8-pbe-en-SHA1AndDES")
obj2cypt := obj2.
FromString("test-pass").
FromXMLPrivateKey([]byte(obj2Pri)).
FromPKCS8PrivateKeyWithPassword([]byte(obj2Pri), "123").
Sign().
ToBase64String()
obj2Pub, _ := fs.Get("./runtime/key/rsa/xml/rsa_xml.pub")
obj2Pub, _ := fs.Get("./runtime/key/key-pem/rsa/2048/rsa-pkcs8-pbe-en-SHA1AndDES.pub")
obj2cyptde := obj2.
FromBase64String("IM8Hs9L7/VUuMuacQ1RZN2YtCnQpZgAYMby6gskPZy7eMktHIbnXLfU2gznkkUM15Kr5dgP+64Cz/cJvQRyNMK0it/GkBLaVAgrRd86XuKOFj9/ilEbHF0doRjsPjjpl+HzN5xDWkJCQ9Ag8Z9SplcFxO7svS1eV4mu0638wY9EmYW89JyEkN00ecps8sde0Ky+7zlqXjUqzLmte2+W7OdOq7+oezQmsKirmqjLuMywgFFax7yLy3+rZ2gvhQWu10PzePIRbssOCTLf2aSZbAKKuqBMwo3oWT9pZBUFKRCbR8KxqdAHou4haM9gxmDCrymdj+5Na+mNv7i4pkG8NOg==").
FromXMLPublicKey([]byte(obj2Pub)).
FromBase64String("Pok10M8e9u1WicbS08/IvoKChoYXfKbljcJYr6srL5TkaAJTYD4thgPDV/EzRvCqfJsQyDb0cOqM2kmwKDt5zl+Amf6TitTPKb9LxCCuKcz6VKHtoUZ+t4ENZM4y2bjRNjkChWdjjEb0kjoljWoaZ+zoWl+6QWRRug6NQJag78J3crqVA34iulsygC/sVEy/LKSJ76PBDx9srdqXpf03HiJgYUSso7YnZ3RT+AS13GgZy7BFZskrjIX2Qw64X8Ydtt5TrfMckjxf0QWdNSwmFxSeNh1Cn2gozG9sJl7yiELNiG0JqRDIOYQTpszj314W5CYEIa/y4eRTDmiNiKr3cA==").
FromPKCS8PublicKey([]byte(obj2Pub)).
Verify([]byte("test-pass")).
ToVerify()

Expand Down
22 changes: 19 additions & 3 deletions pkg/lakego-pkg/go-cryptobin/cipher/salsa20/salsa20.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,36 @@ func NewCipher(key, nonce []byte) (cipher.Stream, error) {
if len(key) != 32 {
return nil, errors.New("key size must be 32")
}

copy(fixedSizedKey[:], key)

return &Cipher{
key: fixedSizedKey,
nonce: nonce,
}, nil
}

// key is 32 bytes, nonce is 16 bytes.
func NewCipherWithCounter(key, nonce []byte, counter uint64) (cipher.Stream, error) {
var fixedSizedKey [32]byte
if len(key) != 32 {
return nil, errors.New("key size must be 32")
}

copy(fixedSizedKey[:], key)

return &Cipher{
key: fixedSizedKey,
nonce: nonce,
counter: counter,
}, nil
}

func (c *Cipher) XORKeyStream(dst, src []byte) {
if len(dst) < len(src) {
panic("cipher/salsa20: dst buffer is to small")
}

paddingLength := int(c.counter % 64)
buf := make([]byte, len(src)+paddingLength)

Expand Down
56 changes: 4 additions & 52 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/dh/curve25519/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/deatil/go-cryptobin/dh/curve25519"
cryptobin_pkcs8 "github.com/deatil/go-cryptobin/pkcs8"
cryptobin_pkcs8pbe "github.com/deatil/go-cryptobin/pkcs8pbe"
cryptobin_pkcs8s "github.com/deatil/go-cryptobin/pkcs8s"
)

type (
Expand Down Expand Up @@ -54,77 +54,29 @@ func (this Curve25519) CreatePrivateKey() Curve25519 {
// 生成 PKCS8 私钥带密码 pem 数据
// CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256")
func (this Curve25519) CreatePrivateKeyWithPassword(password string, opts ...any) Curve25519 {
if len(opts) > 0 {
switch optString := opts[0].(type) {
case string:
isPkcs8Pbe := cryptobin_pkcs8pbe.CheckCipherFromName(optString)

if isPkcs8Pbe {
return this.createPbePrivateKeyWithPassword(password, optString)
}
}
}

return this.createKdfPrivateKeyWithPassword(password, opts...)
}

// 生成私钥带密码 pem 数据
func (this Curve25519) createKdfPrivateKeyWithPassword(password string, opts ...any) Curve25519 {
if this.privateKey == nil {
err := errors.New("Curve25519: privateKey error.")
return this.AppendError(err)
}

opt, err := cryptobin_pkcs8.ParseOpts(opts...)
if err != nil {
return this.AppendError(err)
}

// 生成私钥
privateKey, err := curve25519.MarshalPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}

// 生成加密数据
privateBlock, err := cryptobin_pkcs8.EncryptPKCS8PrivateKey(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
[]byte(password),
opt,
)
if err != nil {
return this.AppendError(err)
}

this.keyData = pem.EncodeToMemory(privateBlock)

return this
}

// 生成 PKCS8 私钥带密码 pem 数据
func (this Curve25519) createPbePrivateKeyWithPassword(password string, alg string) Curve25519 {
if this.privateKey == nil {
err := errors.New("Curve25519: privateKey error.")
return this.AppendError(err)
}

// 生成私钥
privateKey, err := curve25519.MarshalPrivateKey(this.privateKey)
opt, err := cryptobin_pkcs8s.ParseOpts(opts...)
if err != nil {
return this.AppendError(err)
}

pemCipher := cryptobin_pkcs8pbe.GetCipherFromName(alg)

// 生成加密数据
privateBlock, err := cryptobin_pkcs8pbe.EncryptPKCS8PrivateKey(
privateBlock, err := cryptobin_pkcs8s.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
[]byte(password),
pemCipher,
opt,
)
if err != nil {
return this.AppendError(err)
Expand Down
14 changes: 5 additions & 9 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/dh/curve25519/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"crypto"
"encoding/pem"

cryptobin_pkcs8 "github.com/deatil/go-cryptobin/pkcs8"
cryptobin_pkcs8pbe "github.com/deatil/go-cryptobin/pkcs8pbe"

"github.com/deatil/go-cryptobin/dh/curve25519"

cryptobin_pkcs8s "github.com/deatil/go-cryptobin/pkcs8s"
)

var (
Expand Down Expand Up @@ -52,15 +51,12 @@ func (this Curve25519) ParsePrivateKeyFromPEMWithPassword(key []byte, password s
return nil, ErrKeyMustBePEMEncoded
}

var parsedKey any

var blockDecrypted []byte
if blockDecrypted, err = cryptobin_pkcs8.DecryptPKCS8PrivateKey(block.Bytes, []byte(password)); err != nil {
if blockDecrypted, err = cryptobin_pkcs8pbe.DecryptPKCS8PrivateKey(block.Bytes, []byte(password)); err != nil {
return nil, err
}
if blockDecrypted, err = cryptobin_pkcs8s.DecryptPEMBlock(block, []byte(password)); err != nil {
return nil, err
}

var parsedKey any
if parsedKey, err = curve25519.ParsePrivateKey(blockDecrypted); err != nil {
return nil, err
}
Expand Down
56 changes: 4 additions & 52 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/dh/dh/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/deatil/go-cryptobin/dh/dh"
cryptobin_pkcs8 "github.com/deatil/go-cryptobin/pkcs8"
cryptobin_pkcs8pbe "github.com/deatil/go-cryptobin/pkcs8pbe"
cryptobin_pkcs8s "github.com/deatil/go-cryptobin/pkcs8s"
)

type (
Expand Down Expand Up @@ -54,77 +54,29 @@ func (this Dh) CreatePrivateKey() Dh {
// 生成 PKCS8 私钥带密码 pem 数据
// CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256")
func (this Dh) CreatePrivateKeyWithPassword(password string, opts ...any) Dh {
if len(opts) > 0 {
switch optString := opts[0].(type) {
case string:
isPkcs8Pbe := cryptobin_pkcs8pbe.CheckCipherFromName(optString)

if isPkcs8Pbe {
return this.createPbePrivateKeyWithPassword(password, optString)
}
}
}

return this.createKdfPrivateKeyWithPassword(password, opts...)
}

// 生成私钥带密码 pem 数据
func (this Dh) createKdfPrivateKeyWithPassword(password string, opts ...any) Dh {
if this.privateKey == nil {
err := errors.New("Dh: privateKey error.")
return this.AppendError(err)
}

opt, err := cryptobin_pkcs8.ParseOpts(opts...)
if err != nil {
return this.AppendError(err)
}

// 生成私钥
privateKey, err := dh.MarshalPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}

// 生成加密数据
privateBlock, err := cryptobin_pkcs8.EncryptPKCS8PrivateKey(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
[]byte(password),
opt,
)
if err != nil {
return this.AppendError(err)
}

this.keyData = pem.EncodeToMemory(privateBlock)

return this
}

// 生成 PKCS8 私钥带密码 pem 数据
func (this Dh) createPbePrivateKeyWithPassword(password string, alg string) Dh {
if this.privateKey == nil {
err := errors.New("Dh: privateKey error.")
return this.AppendError(err)
}

// 生成私钥
privateKey, err := dh.MarshalPrivateKey(this.privateKey)
opt, err := cryptobin_pkcs8s.ParseOpts(opts...)
if err != nil {
return this.AppendError(err)
}

pemCipher := cryptobin_pkcs8pbe.GetCipherFromName(alg)

// 生成加密数据
privateBlock, err := cryptobin_pkcs8pbe.EncryptPKCS8PrivateKey(
privateBlock, err := cryptobin_pkcs8s.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
[]byte(password),
pemCipher,
opt,
)
if err != nil {
return this.AppendError(err)
Expand Down
14 changes: 5 additions & 9 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/dh/dh/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"crypto"
"encoding/pem"

cryptobin_pkcs8 "github.com/deatil/go-cryptobin/pkcs8"
cryptobin_pkcs8pbe "github.com/deatil/go-cryptobin/pkcs8pbe"

"github.com/deatil/go-cryptobin/dh/dh"

cryptobin_pkcs8s "github.com/deatil/go-cryptobin/pkcs8s"
)

var (
Expand Down Expand Up @@ -52,15 +51,12 @@ func (this Dh) ParsePrivateKeyFromPEMWithPassword(key []byte, password string) (
return nil, ErrKeyMustBePEMEncoded
}

var parsedKey any

var blockDecrypted []byte
if blockDecrypted, err = cryptobin_pkcs8pbe.DecryptPKCS8PrivateKey(block.Bytes, []byte(password)); err != nil {
if blockDecrypted, err = cryptobin_pkcs8.DecryptPKCS8PrivateKey(block.Bytes, []byte(password)); err != nil {
return nil, err
}
if blockDecrypted, err = cryptobin_pkcs8s.DecryptPEMBlock(block, []byte(password)); err != nil {
return nil, err
}

var parsedKey any
if parsedKey, err = dh.ParsePrivateKey(blockDecrypted); err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit a0fa93c

Please sign in to comment.