Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
chain33-sdk-go
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
chain33-sdk-go
Commits
2271c8a9
Unverified
Commit
2271c8a9
authored
Aug 13, 2020
by
andyYuanFZM
Committed by
GitHub
Aug 13, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4 from jpeng-go/master
update sm2
parents
eeac28ea
d2271f7a
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
29 additions
and
23 deletions
+29
-23
account.go
account.go
+1
-1
crypto_test.go
crypto/crypto_test.go
+7
-3
sm2.go
crypto/gm/sm2.go
+18
-18
go.mod
go.mod
+1
-1
go.sum
go.sum
+2
-0
No files found.
account.go
View file @
2271c8a9
...
...
@@ -31,7 +31,7 @@ func NewAccount(signType string) (*Account, error) {
}
account
.
Address
=
addr
}
else
if
signType
==
crypto
.
SM2
{
account
.
PrivateKey
,
account
.
PublicKey
=
gm
.
Gene
t
ateKey
()
account
.
PrivateKey
,
account
.
PublicKey
=
gm
.
Gene
r
ateKey
()
addr
,
err
:=
crypto
.
PubKeyToAddress
(
account
.
PublicKey
)
if
err
!=
nil
{
return
nil
,
err
...
...
crypto/crypto_test.go
View file @
2271c8a9
...
...
@@ -39,15 +39,19 @@ func TestSign(t *testing.T) {
}
func
TestSM2
(
t
*
testing
.
T
)
{
priv
,
pub
:=
gm
.
GenetateKey
()
priv
,
pub
:=
gm
.
GenerateKey
()
fmt
.
Println
(
priv
)
fmt
.
Println
(
pub
)
fmt
.
Println
(
types
.
ToHex
(
priv
))
fmt
.
Println
(
types
.
ToHex
(
pub
))
msg
:=
[]
byte
(
"sign test"
)
sig
:=
gm
.
SM2Sign
(
msg
,
priv
,
nil
)
sig
:=
gm
.
SM2Sign
(
priv
,
msg
,
nil
)
fmt
.
Printf
(
"sig = %x
\n
"
,
sig
)
result
:=
gm
.
SM2Verify
(
msg
,
pub
,
sig
,
nil
)
result
:=
gm
.
SM2Verify
(
pub
,
msg
,
nil
,
sig
)
assert
.
Equal
(
t
,
true
,
result
)
}
...
...
crypto/gm/sm2.go
View file @
2271c8a9
...
...
@@ -14,8 +14,7 @@ const (
SM2PrivateKeyLength
=
32
)
var
DefaultUID
=
[]
byte
{
0x31
,
0x32
,
0x33
,
0x34
,
0x35
,
0x36
,
0x37
,
0x38
,
0x13
,
0x23
,
0x33
,
0x43
,
0x53
,
0x63
,
0x73
,
0x83
}
var
DefaultUID
=
[]
byte
{
0x31
,
0x32
,
0x33
,
0x34
,
0x35
,
0x36
,
0x37
,
0x38
,
0x31
,
0x32
,
0x33
,
0x34
,
0x35
,
0x36
,
0x37
,
0x38
}
func
getRandBytes
(
numBytes
int
)
[]
byte
{
b
:=
make
([]
byte
,
numBytes
)
...
...
@@ -26,7 +25,7 @@ func getRandBytes(numBytes int) []byte {
return
b
}
func
p
rivKeyFromBytes
(
curve
elliptic
.
Curve
,
pk
[]
byte
)
(
*
sm2
.
PrivateKey
,
*
sm2
.
PublicKey
)
{
func
P
rivKeyFromBytes
(
curve
elliptic
.
Curve
,
pk
[]
byte
)
(
*
sm2
.
PrivateKey
,
*
sm2
.
PublicKey
)
{
x
,
y
:=
curve
.
ScalarBaseMult
(
pk
)
priv
:=
&
sm2
.
PrivateKey
{
...
...
@@ -46,12 +45,12 @@ func parsePubKey(pubKeyStr []byte) (key *sm2.PublicKey) {
}
//SerializePublicKey 公钥序列化
func
s
erializePublicKey
(
p
*
sm2
.
PublicKey
)
[]
byte
{
func
S
erializePublicKey
(
p
*
sm2
.
PublicKey
)
[]
byte
{
return
sm2
.
Compress
(
p
)
}
//SerializePrivateKey 私钥序列化
func
s
erializePrivateKey
(
p
*
sm2
.
PrivateKey
)
[]
byte
{
func
S
erializePrivateKey
(
p
*
sm2
.
PrivateKey
)
[]
byte
{
b
:=
make
([]
byte
,
0
,
SM2PrivateKeyLength
)
return
paddedAppend
(
SM2PrivateKeyLength
,
b
,
p
.
D
.
Bytes
())
}
...
...
@@ -76,7 +75,7 @@ func canonicalizeInt(val *big.Int) []byte {
return
b
}
func
s
erializeSignature
(
r
,
s
*
big
.
Int
)
[]
byte
{
func
S
erializeSignature
(
r
,
s
*
big
.
Int
)
[]
byte
{
rb
:=
canonicalizeInt
(
r
)
sb
:=
canonicalizeInt
(
s
)
...
...
@@ -95,7 +94,7 @@ func serializeSignature(r, s *big.Int) []byte {
return
b
}
func
d
eserializeSignature
(
sigStr
[]
byte
)
(
*
big
.
Int
,
*
big
.
Int
,
error
)
{
func
D
eserializeSignature
(
sigStr
[]
byte
)
(
*
big
.
Int
,
*
big
.
Int
,
error
)
{
sig
,
err
:=
btcec
.
ParseDERSignature
(
sigStr
,
sm2
.
P256Sm2
())
if
err
!=
nil
{
return
nil
,
nil
,
err
...
...
@@ -104,7 +103,7 @@ func deserializeSignature(sigStr []byte) (*big.Int, *big.Int, error) {
return
sig
.
R
,
sig
.
S
,
nil
}
func
Gene
t
ateKey
()
([]
byte
,
[]
byte
)
{
func
Gene
r
ateKey
()
([]
byte
,
[]
byte
)
{
privKeyBytes
:=
[
SM2PrivateKeyLength
]
byte
{}
for
{
...
...
@@ -115,32 +114,32 @@ func GenetateKey() ([]byte, []byte) {
copy
(
privKeyBytes
[
:
],
key
)
break
}
priv
,
pub
:=
p
rivKeyFromBytes
(
sm2
.
P256Sm2
(),
privKeyBytes
[
:
])
priv
,
pub
:=
P
rivKeyFromBytes
(
sm2
.
P256Sm2
(),
privKeyBytes
[
:
])
return
serializePrivateKey
(
priv
),
s
erializePublicKey
(
pub
)
return
SerializePrivateKey
(
priv
),
S
erializePublicKey
(
pub
)
}
func
SM2Sign
(
msg
[]
byte
,
privateKey
[]
byte
,
uid
[]
byte
)
[]
byte
{
func
SM2Sign
(
privateKey
[]
byte
,
msg
[]
byte
,
uid
[]
byte
)
[]
byte
{
if
uid
==
nil
{
uid
=
DefaultUID
}
priv
,
_
:=
p
rivKeyFromBytes
(
sm2
.
P256Sm2
(),
privateKey
)
priv
,
_
:=
P
rivKeyFromBytes
(
sm2
.
P256Sm2
(),
privateKey
)
r
,
s
,
err
:=
sm2
.
Sm2Sign
(
priv
,
msg
,
uid
)
if
err
!=
nil
{
return
nil
}
return
s
erializeSignature
(
r
,
s
)
return
S
erializeSignature
(
r
,
s
)
}
func
SM2Verify
(
msg
[]
byte
,
publicKey
[]
byte
,
sig
[]
byte
,
uid
[]
byte
)
bool
{
func
SM2Verify
(
publicKey
[]
byte
,
msg
[]
byte
,
uid
[]
byte
,
sig
[]
byte
,
)
bool
{
if
uid
==
nil
{
uid
=
DefaultUID
}
pub
:=
parsePubKey
(
publicKey
[
:
])
r
,
s
,
err
:=
d
eserializeSignature
(
sig
)
r
,
s
,
err
:=
D
eserializeSignature
(
sig
)
if
err
!=
nil
{
fmt
.
Errorf
(
"unmarshal sign failed"
)
return
false
...
...
@@ -156,12 +155,12 @@ func SM2Encrypt(publicKey []byte, data []byte) ([]byte, error) {
}
func
SM2Decrypt
(
privateKey
[]
byte
,
data
[]
byte
)
([]
byte
,
error
)
{
priv
,
_
:=
p
rivKeyFromBytes
(
sm2
.
P256Sm2
(),
privateKey
)
priv
,
_
:=
P
rivKeyFromBytes
(
sm2
.
P256Sm2
(),
privateKey
)
return
sm2
.
Decrypt
(
priv
,
data
)
}
func
PubKeyFromPrivate
(
privKey
[]
byte
)
[]
byte
{
_
,
pub
:=
p
rivKeyFromBytes
(
sm2
.
P256Sm2
(),
privKey
)
return
s
erializePublicKey
(
pub
)
_
,
pub
:=
P
rivKeyFromBytes
(
sm2
.
P256Sm2
(),
privKey
)
return
S
erializePublicKey
(
pub
)
}
\ No newline at end of file
go.mod
View file @
2271c8a9
...
...
@@ -26,7 +26,7 @@ require (
github.com/spf13/cobra v1.0.0 // indirect
github.com/stretchr/testify v1.5.1
github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/tjfoc/gmsm v1.3.
1
github.com/tjfoc/gmsm v1.3.
2
golang.org/x/crypto v0.0.0-20191219195013-becbf705a915
google.golang.org/grpc v1.29.1 // indirect
)
go.sum
View file @
2271c8a9
...
...
@@ -256,6 +256,8 @@ github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFd
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
github.com/tjfoc/gmsm v1.3.1 h1:+k3IAlF81c31/TllJmIfuCYnjl8ziMdTWGWJcP9J1uo=
github.com/tjfoc/gmsm v1.3.1/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
github.com/tjfoc/gmsm v1.3.2 h1:7JVkAn5bvUJ7HtU08iW6UiD+UTmJTIToHCfeFzkcCxM=
github.com/tjfoc/gmsm v1.3.2/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment