Skip to content

Commit

Permalink
更改获取 SM2 明文方法
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jun 11, 2024
1 parent acd425f commit 60b7164
Show file tree
Hide file tree
Showing 40 changed files with 304 additions and 330 deletions.
4 changes: 2 additions & 2 deletions cryptobin/ca/error.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ca

import (
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
"github.com/deatil/go-cryptobin/tool"
)

// 添加错误
Expand All @@ -13,5 +13,5 @@ func (this CA) AppendError(err ...error) CA {

// 获取错误
func (this CA) Error() error {
return cryptobin_tool.NewError(this.Errors...)
return tool.NewError(this.Errors...)
}
258 changes: 129 additions & 129 deletions cryptobin/crypto/encrypt_multiple.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions cryptobin/crypto/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func FromString(data string) Cryptobin {
// 设置数据 Base64
// set data Base64
func (this Cryptobin) FromBase64String(data string) Cryptobin {
newData, err := tool.NewEncoding().Base64Decode(data)
newData, err := tool.Base64Decode(data)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -54,7 +54,7 @@ func FromBase64String(data string) Cryptobin {
// 设置数据 Hex
// set data Hex
func (this Cryptobin) FromHexString(data string) Cryptobin {
newData, err := tool.NewEncoding().HexDecode(data)
newData, err := tool.HexDecode(data)
if err != nil {
return this.AppendError(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cryptobin/dh/curve25519/error.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package curve25519

import (
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
"github.com/deatil/go-cryptobin/tool"
)

// 添加错误
Expand All @@ -13,5 +13,5 @@ func (this Curve25519) AppendError(err ...error) Curve25519 {

// 获取错误
func (this Curve25519) Error() error {
return cryptobin_tool.NewError(this.Errors...)
return tool.NewError(this.Errors...)
}
14 changes: 4 additions & 10 deletions cryptobin/dh/curve25519/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@ func FromPublicKey(key []byte) Curve25519 {

// 根据私钥 x, y 生成
func (this Curve25519) FromKeyXYHexString(xString string, yString string) Curve25519 {
encoding := tool.NewEncoding()

x, _ := encoding.HexDecode(xString)
y, _ := encoding.HexDecode(yString)
x, _ := tool.HexDecode(xString)
y, _ := tool.HexDecode(yString)

priv := &curve25519.PrivateKey{}
priv.X = x
Expand All @@ -112,9 +110,7 @@ func FromKeyXYHexString(xString string, yString string) Curve25519 {

// 根据私钥 x 生成
func (this Curve25519) FromPrivateKeyXHexString(xString string) Curve25519 {
encoding := tool.NewEncoding()

x, _ := encoding.HexDecode(xString)
x, _ := tool.HexDecode(xString)

priv := &curve25519.PrivateKey{}
priv.X = x
Expand All @@ -134,9 +130,7 @@ func FromPrivateKeyXHexString(xString string) Curve25519 {

// 根据公钥 y 生成
func (this Curve25519) FromPublicKeyYHexString(yString string) Curve25519 {
encoding := tool.NewEncoding()

y, _ := encoding.HexDecode(yString)
y, _ := tool.HexDecode(yString)

public := &curve25519.PublicKey{}
public.Y = y
Expand Down
11 changes: 3 additions & 8 deletions cryptobin/dh/curve25519/get.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package curve25519

import (
"github.com/deatil/go-cryptobin/tool"
"github.com/deatil/go-cryptobin/dh/curve25519"

cryptobin_tool "github.com/deatil/go-cryptobin/tool"
)

// 获取 PrivateKey
Expand All @@ -15,9 +14,7 @@ func (this Curve25519) GetPrivateKey() *curve25519.PrivateKey {
func (this Curve25519) GetPrivateKeyXHexString() string {
data := this.privateKey.X

dataHex := cryptobin_tool.
NewEncoding().
HexEncode(data)
dataHex := tool.HexEncode(data)

return dataHex
}
Expand All @@ -31,9 +28,7 @@ func (this Curve25519) GetPublicKey() *curve25519.PublicKey {
func (this Curve25519) GetPublicKeyYHexString() string {
data := this.publicKey.Y

dataHex := cryptobin_tool.
NewEncoding().
HexEncode(data)
dataHex := tool.HexEncode(data)

return dataHex
}
Expand Down
6 changes: 3 additions & 3 deletions cryptobin/dh/curve25519/to.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package curve25519

import (
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
"github.com/deatil/go-cryptobin/tool"
)

// 私钥/公钥
Expand All @@ -28,10 +28,10 @@ func (this Curve25519) ToString() string {

// 输出Base64
func (this Curve25519) ToBase64String() string {
return cryptobin_tool.NewEncoding().Base64Encode(this.secretData)
return tool.Base64Encode(this.secretData)
}

// 输出Hex
func (this Curve25519) ToHexString() string {
return cryptobin_tool.NewEncoding().HexEncode(this.secretData)
return tool.HexEncode(this.secretData)
}
4 changes: 2 additions & 2 deletions cryptobin/dh/dh/error.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dh

import (
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
"github.com/deatil/go-cryptobin/tool"
)

// 添加错误
Expand All @@ -13,5 +13,5 @@ func (this DH) AppendError(err ...error) DH {

// 获取错误
func (this DH) Error() error {
return cryptobin_tool.NewError(this.Errors...)
return tool.NewError(this.Errors...)
}
2 changes: 1 addition & 1 deletion cryptobin/dh/dh/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (this DH) GetPublicKey() *dh.PublicKey {
}

// 获取 Y 16进制字符
func (this DH) GetPublicKeyYHexString() string {
func (this DH) GetPublicKeyYString() string {
if this.publicKey == nil {
return ""
}
Expand Down
6 changes: 3 additions & 3 deletions cryptobin/dh/dh/to.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dh

import (
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
"github.com/deatil/go-cryptobin/tool"
)

// 私钥/公钥
Expand All @@ -28,10 +28,10 @@ func (this DH) ToString() string {

// 输出Base64
func (this DH) ToBase64String() string {
return cryptobin_tool.NewEncoding().Base64Encode(this.secretData)
return tool.Base64Encode(this.secretData)
}

// 输出Hex
func (this DH) ToHexString() string {
return cryptobin_tool.NewEncoding().HexEncode(this.secretData)
return tool.HexEncode(this.secretData)
}
4 changes: 2 additions & 2 deletions cryptobin/dh/ecdh/error.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ecdh

import (
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
"github.com/deatil/go-cryptobin/tool"
)

// 添加错误
Expand All @@ -13,5 +13,5 @@ func (this ECDH) AppendError(err ...error) ECDH {

// 获取错误
func (this ECDH) Error() error {
return cryptobin_tool.NewError(this.Errors...)
return tool.NewError(this.Errors...)
}
14 changes: 4 additions & 10 deletions cryptobin/dh/ecdh/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ func FromPublicKey(key []byte) ECDH {

// 根据私钥 x, y 生成
func (this ECDH) FromKeyXYHexString(xString string, yString string) ECDH {
encoding := tool.NewEncoding()

x, _ := encoding.HexDecode(xString)
y, _ := encoding.HexDecode(yString)
x, _ := tool.HexDecode(xString)
y, _ := tool.HexDecode(yString)

priv := &ecdh.PrivateKey{}
priv.X = x
Expand All @@ -115,9 +113,7 @@ func FromKeyXYHexString(xString string, yString string) ECDH {

// 根据私钥 x 生成
func (this ECDH) FromPrivateKeyXHexString(xString string) ECDH {
encoding := tool.NewEncoding()

x, _ := encoding.HexDecode(xString)
x, _ := tool.HexDecode(xString)

priv := &ecdh.PrivateKey{}
priv.X = x
Expand All @@ -138,9 +134,7 @@ func FromPrivateKeyXHexString(xString string) ECDH {

// 根据公钥 y 生成
func (this ECDH) FromPublicKeyYHexString(yString string) ECDH {
encoding := tool.NewEncoding()

y, _ := encoding.HexDecode(yString)
y, _ := tool.HexDecode(yString)

public := &ecdh.PublicKey{}
public.Y = y
Expand Down
13 changes: 4 additions & 9 deletions cryptobin/dh/ecdh/get.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package ecdh

import (
"github.com/deatil/go-cryptobin/tool"
"github.com/deatil/go-cryptobin/dh/ecdh"

cryptobin_tool "github.com/deatil/go-cryptobin/tool"
)

// 获取 PrivateKey
Expand All @@ -15,9 +14,7 @@ func (this ECDH) GetPrivateKey() *ecdh.PrivateKey {
func (this ECDH) GetPrivateKeyXHexString() string {
data := this.privateKey.X

dataHex := cryptobin_tool.
NewEncoding().
HexEncode(data)
dataHex := tool.HexEncode(data)

return dataHex
}
Expand All @@ -28,12 +25,10 @@ func (this ECDH) GetPublicKey() *ecdh.PublicKey {
}

// 获取 Y 16进制字符
func (this ECDH) GetPublicKeyYHexString() string {
func (this ECDH) GetPublicKeyYString() string {
data := this.publicKey.Y

dataHex := cryptobin_tool.
NewEncoding().
HexEncode(data)
dataHex := tool.HexEncode(data)

return dataHex
}
Expand Down
6 changes: 3 additions & 3 deletions cryptobin/dh/ecdh/to.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ecdh

import (
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
"github.com/deatil/go-cryptobin/tool"
)

// 私钥/公钥
Expand All @@ -28,10 +28,10 @@ func (this ECDH) ToString() string {

// 输出Base64
func (this ECDH) ToBase64String() string {
return cryptobin_tool.NewEncoding().Base64Encode(this.secretData)
return tool.Base64Encode(this.secretData)
}

// 输出Hex
func (this ECDH) ToHexString() string {
return cryptobin_tool.NewEncoding().HexEncode(this.secretData)
return tool.HexEncode(this.secretData)
}
4 changes: 2 additions & 2 deletions cryptobin/dsa/error.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dsa

import (
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
"github.com/deatil/go-cryptobin/tool"
)

// 添加错误
Expand All @@ -13,5 +13,5 @@ func (this DSA) AppendError(err ...error) DSA {

// 获取错误
func (this DSA) Error() error {
return cryptobin_tool.NewError(this.Errors...)
return tool.NewError(this.Errors...)
}
14 changes: 7 additions & 7 deletions cryptobin/dsa/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/dsa"
"crypto/rand"

cryptobin_tool "github.com/deatil/go-cryptobin/tool"
"github.com/deatil/go-cryptobin/tool"
)

// 生成密钥
Expand Down Expand Up @@ -239,7 +239,7 @@ func FromPKCS8PublicKey(key []byte) DSA {

// Pkcs1 DER 私钥
func (this DSA) FromPKCS1PrivateKeyDer(der []byte) DSA {
key := cryptobin_tool.EncodeDerToPem(der, "DSA PRIVATE KEY")
key := tool.EncodeDerToPem(der, "DSA PRIVATE KEY")

parsedKey, err := this.ParsePKCS1PrivateKeyFromPEM(key)
if err != nil {
Expand All @@ -253,7 +253,7 @@ func (this DSA) FromPKCS1PrivateKeyDer(der []byte) DSA {

// PKCS1 DER 公钥
func (this DSA) FromPKCS1PublicKeyDer(der []byte) DSA {
key := cryptobin_tool.EncodeDerToPem(der, "DSA PUBLIC KEY")
key := tool.EncodeDerToPem(der, "DSA PUBLIC KEY")

parsedKey, err := this.ParsePKCS1PublicKeyFromPEM(key)
if err != nil {
Expand All @@ -269,7 +269,7 @@ func (this DSA) FromPKCS1PublicKeyDer(der []byte) DSA {

// Pkcs8 DER 私钥
func (this DSA) FromPKCS8PrivateKeyDer(der []byte) DSA {
key := cryptobin_tool.EncodeDerToPem(der, "PRIVATE KEY")
key := tool.EncodeDerToPem(der, "PRIVATE KEY")

parsedKey, err := this.ParsePKCS8PrivateKeyFromPEM(key)
if err != nil {
Expand All @@ -283,7 +283,7 @@ func (this DSA) FromPKCS8PrivateKeyDer(der []byte) DSA {

// PKCS8 DER 公钥
func (this DSA) FromPKCS8PublicKeyDer(der []byte) DSA {
key := cryptobin_tool.EncodeDerToPem(der, "PUBLIC KEY")
key := tool.EncodeDerToPem(der, "PUBLIC KEY")

parsedKey, err := this.ParsePKCS8PublicKeyFromPEM(key)
if err != nil {
Expand Down Expand Up @@ -359,7 +359,7 @@ func FromString(data string) DSA {

// Base64
func (this DSA) FromBase64String(data string) DSA {
newData, err := cryptobin_tool.NewEncoding().Base64Decode(data)
newData, err := tool.Base64Decode(data)

this.data = newData

Expand All @@ -373,7 +373,7 @@ func FromBase64String(data string) DSA {

// Hex
func (this DSA) FromHexString(data string) DSA {
newData, err := cryptobin_tool.NewEncoding().HexDecode(data)
newData, err := tool.HexDecode(data)

this.data = newData

Expand Down
6 changes: 3 additions & 3 deletions cryptobin/dsa/to.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dsa

import (
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
"github.com/deatil/go-cryptobin/tool"
)

// 私钥/公钥
Expand All @@ -28,12 +28,12 @@ func (this DSA) ToString() string {

// 输出Base64
func (this DSA) ToBase64String() string {
return cryptobin_tool.NewEncoding().Base64Encode(this.parsedData)
return tool.Base64Encode(this.parsedData)
}

// 输出Hex
func (this DSA) ToHexString() string {
return cryptobin_tool.NewEncoding().HexEncode(this.parsedData)
return tool.HexEncode(this.parsedData)
}

// ==========
Expand Down
Loading

0 comments on commit 60b7164

Please sign in to comment.