Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Feb 23, 2024
1 parent 157fe8e commit 6cb19b7
Show file tree
Hide file tree
Showing 20 changed files with 329 additions and 265 deletions.
16 changes: 8 additions & 8 deletions cryptobin/ca/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,44 +174,44 @@ func (this CA) CreatePrivateKey() CA {

switch privateKey := this.privateKey.(type) {
case *rsa.PrivateKey:
x509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey)
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)

privateBlock = &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509PrivateKey,
Bytes: privateKeyBytes,
}

case *ecdsa.PrivateKey:
x509PrivateKey, err := x509.MarshalECPrivateKey(privateKey)
privateKeyBytes, err := x509.MarshalECPrivateKey(privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock = &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: x509PrivateKey,
Bytes: privateKeyBytes,
}

case ed25519.PrivateKey:
x509PrivateKey, err := x509.MarshalPKCS8PrivateKey(privateKey)
privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock = &pem.Block{
Type: "PRIVATE KEY",
Bytes: x509PrivateKey,
Bytes: privateKeyBytes,
}

case *sm2.PrivateKey:
x509PrivateKey, err := sm2.MarshalPrivateKey(privateKey)
privateKeyBytes, err := sm2.MarshalPrivateKey(privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock = &pem.Block{
Type: "PRIVATE KEY",
Bytes: x509PrivateKey,
Bytes: privateKeyBytes,
}

default:
Expand Down
8 changes: 4 additions & 4 deletions cryptobin/dh/curve25519/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func (this Curve25519) CreatePrivateKey() Curve25519 {
return this.AppendError(err)
}

privateKey, err := curve25519.MarshalPrivateKey(this.privateKey)
privateKeyBytes, err := curve25519.MarshalPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKey,
Bytes: privateKeyBytes,
}

this.keyData = pem.EncodeToMemory(privateBlock)
Expand All @@ -64,7 +64,7 @@ func (this Curve25519) CreatePrivateKeyWithPassword(password string, opts ...any
}

// 生成私钥
privateKey, err := curve25519.MarshalPrivateKey(this.privateKey)
privateKeyBytes, err := curve25519.MarshalPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -73,7 +73,7 @@ func (this Curve25519) CreatePrivateKeyWithPassword(password string, opts ...any
privateBlock, err := pkcs8.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
privateKeyBytes,
[]byte(password),
opt,
)
Expand Down
8 changes: 4 additions & 4 deletions cryptobin/dh/dh/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func (this DH) CreatePrivateKey() DH {
return this.AppendError(err)
}

privateKey, err := dh.MarshalPrivateKey(this.privateKey)
privateKeyBytes, err := dh.MarshalPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKey,
Bytes: privateKeyBytes,
}

this.keyData = pem.EncodeToMemory(privateBlock)
Expand All @@ -64,7 +64,7 @@ func (this DH) CreatePrivateKeyWithPassword(password string, opts ...any) DH {
}

// 生成私钥
privateKey, err := dh.MarshalPrivateKey(this.privateKey)
privateKeyBytes, err := dh.MarshalPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -73,7 +73,7 @@ func (this DH) CreatePrivateKeyWithPassword(password string, opts ...any) DH {
privateBlock, err := pkcs8.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
privateKeyBytes,
[]byte(password),
opt,
)
Expand Down
8 changes: 4 additions & 4 deletions cryptobin/dh/ecdh/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func (this ECDH) CreatePrivateKey() ECDH {
return this.AppendError(err)
}

privateKey, err := ecdh.MarshalPrivateKey(this.privateKey)
privateKeyBytes, err := ecdh.MarshalPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKey,
Bytes: privateKeyBytes,
}

this.keyData = pem.EncodeToMemory(privateBlock)
Expand All @@ -64,7 +64,7 @@ func (this ECDH) CreatePrivateKeyWithPassword(password string, opts ...any) ECDH
}

// 生成私钥
privateKey, err := ecdh.MarshalPrivateKey(this.privateKey)
privateKeyBytes, err := ecdh.MarshalPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -73,7 +73,7 @@ func (this ECDH) CreatePrivateKeyWithPassword(password string, opts ...any) ECDH
privateBlock, err := pkcs8.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
privateKeyBytes,
[]byte(password),
opt,
)
Expand Down
8 changes: 4 additions & 4 deletions cryptobin/dsa/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (this DSA) CreatePKCS1PrivateKeyWithPassword(password string, opts ...strin
}

// 生成私钥
x509PrivateKey, err := dsa.MarshalPKCS1PrivateKey(this.privateKey)
privateKeyBytes, err := dsa.MarshalPKCS1PrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -100,7 +100,7 @@ func (this DSA) CreatePKCS1PrivateKeyWithPassword(password string, opts ...strin
privateBlock, err := pkcs1.EncryptPEMBlock(
rand.Reader,
"DSA PRIVATE KEY",
x509PrivateKey,
privateKeyBytes,
[]byte(password),
cipher,
)
Expand Down Expand Up @@ -173,7 +173,7 @@ func (this DSA) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any)
}

// 生成私钥
x509PrivateKey, err := dsa.MarshalPKCS8PrivateKey(this.privateKey)
privateKeyBytes, err := dsa.MarshalPKCS8PrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -182,7 +182,7 @@ func (this DSA) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any)
privateBlock, err := pkcs8.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
x509PrivateKey,
privateKeyBytes,
[]byte(password),
opt,
)
Expand Down
24 changes: 12 additions & 12 deletions cryptobin/ecdh/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ func (this ECDH) CreatePrivateKey() ECDH {
return this.AppendError(err)
}

privateKey, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKey,
Bytes: privateKeyBytes,
}

this.keyData = pem.EncodeToMemory(privateBlock)
Expand All @@ -66,7 +66,7 @@ func (this ECDH) CreatePrivateKeyWithPassword(password string, opts ...any) ECDH
}

// 生成私钥
privateKey, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -75,7 +75,7 @@ func (this ECDH) CreatePrivateKeyWithPassword(password string, opts ...any) ECDH
privateBlock, err := pkcs8.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
privateKeyBytes,
[]byte(password),
opt,
)
Expand Down Expand Up @@ -119,19 +119,19 @@ func (this ECDH) CreateECDHPrivateKey() ECDH {
return this.AppendError(err)
}

priv, err := ecdh.FromPrivateKey(this.privateKey)
privateKey, err := ecdh.FromPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}

privateKey, err := ecdh_key.MarshalPrivateKey(priv)
privateKeyBytes, err := ecdh_key.MarshalPrivateKey(privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKey,
Bytes: privateKeyBytes,
}

this.keyData = pem.EncodeToMemory(privateBlock)
Expand All @@ -151,13 +151,13 @@ func (this ECDH) CreateECDHPrivateKeyWithPassword(password string, opts ...any)
return this.AppendError(err)
}

priv, err := ecdh.FromPrivateKey(this.privateKey)
privateKey, err := ecdh.FromPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}

// 生成私钥
privateKey, err := ecdh_key.MarshalPrivateKey(priv)
privateKeyBytes, err := ecdh_key.MarshalPrivateKey(privateKey)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -166,7 +166,7 @@ func (this ECDH) CreateECDHPrivateKeyWithPassword(password string, opts ...any)
privateBlock, err := pkcs8.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
privateKeyBytes,
[]byte(password),
opt,
)
Expand All @@ -186,12 +186,12 @@ func (this ECDH) CreateECDHPublicKey() ECDH {
return this.AppendError(err)
}

pub, err := ecdh.FromPublicKey(this.publicKey)
publicKey, err := ecdh.FromPublicKey(this.publicKey)
if err != nil {
return this.AppendError(err)
}

publicKeyBytes, err := ecdh_key.MarshalPublicKey(pub)
publicKeyBytes, err := ecdh_key.MarshalPublicKey(publicKey)
if err != nil {
return this.AppendError(err)
}
Expand Down
20 changes: 10 additions & 10 deletions cryptobin/ecdsa/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func (this ECDSA) CreatePKCS1PrivateKey() ECDSA {
return this.AppendError(err)
}

x509PrivateKey, err := x509.MarshalECPrivateKey(this.privateKey)
publicKeyBytes, err := x509.MarshalECPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock := &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: x509PrivateKey,
Bytes: publicKeyBytes,
}

this.keyData = pem.EncodeToMemory(privateBlock)
Expand Down Expand Up @@ -85,7 +85,7 @@ func (this ECDSA) CreatePKCS1PrivateKeyWithPassword(password string, opts ...str
}

// 生成私钥
x509PrivateKey, err := x509.MarshalECPrivateKey(this.privateKey)
publicKeyBytes, err := x509.MarshalECPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -94,7 +94,7 @@ func (this ECDSA) CreatePKCS1PrivateKeyWithPassword(password string, opts ...str
privateBlock, err := pkcs1.EncryptPEMBlock(
rand.Reader,
"EC PRIVATE KEY",
x509PrivateKey,
publicKeyBytes,
[]byte(password),
cipher,
)
Expand All @@ -116,14 +116,14 @@ func (this ECDSA) CreatePKCS8PrivateKey() ECDSA {
return this.AppendError(err)
}

x509PrivateKey, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
publicKeyBytes, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: x509PrivateKey,
Bytes: publicKeyBytes,
}

this.keyData = pem.EncodeToMemory(privateBlock)
Expand All @@ -145,7 +145,7 @@ func (this ECDSA) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any
}

// 生成私钥
x509PrivateKey, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
publicKeyBytes, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -154,7 +154,7 @@ func (this ECDSA) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any
privateBlock, err := pkcs8.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
x509PrivateKey,
publicKeyBytes,
[]byte(password),
opt,
)
Expand All @@ -176,14 +176,14 @@ func (this ECDSA) CreatePublicKey() ECDSA {
return this.AppendError(err)
}

x509PublicKey, err := x509.MarshalPKIXPublicKey(this.publicKey)
publicKeyBytes, err := x509.MarshalPKIXPublicKey(this.publicKey)
if err != nil {
return this.AppendError(err)
}

publicBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: x509PublicKey,
Bytes: publicKeyBytes,
}

this.keyData = pem.EncodeToMemory(publicBlock)
Expand Down
Loading

0 comments on commit 6cb19b7

Please sign in to comment.