Skip to content

Commit 9895c71

Browse files
committed
feat(): add generate rsa privatekey
1 parent 2f9168d commit 9895c71

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

signer.go

+10
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ func NewSigner(b []byte) (*Signer, error) {
5656
}, nil
5757
}
5858

59+
func NewSignerByPrivateKey(privateKey *rsa.PrivateKey) *Signer {
60+
pub := &privateKey.PublicKey
61+
addr := sha256.Sum256(pub.N.Bytes())
62+
return &Signer{
63+
Address: utils.Base64Encode(addr[:]),
64+
PubKey: pub,
65+
PrvKey: privateKey,
66+
}
67+
}
68+
5969
func (s *Signer) SignTx(tx *types.Transaction) error {
6070
return utils.SignTransaction(tx, s.PrvKey)
6171
}

utils/crypto.go

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"crypto/sha256"
88
)
99

10+
func GenerateRsaKey(bits int) (*rsa.PrivateKey, error) {
11+
return rsa.GenerateKey(rand.Reader, bits)
12+
}
13+
1014
func Sign(msg []byte, prvKey *rsa.PrivateKey) ([]byte, error) {
1115
hashed := sha256.Sum256(msg)
1216

utils/crypto_test.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package utils
22

33
import (
4-
"crypto/rand"
5-
"crypto/rsa"
6-
"testing"
7-
84
"github.com/stretchr/testify/assert"
5+
"testing"
96
)
107

118
func TestSignAndVerify(t *testing.T) {
12-
rightKey, err := rsa.GenerateKey(rand.Reader, 2048)
9+
rightKey, err := GenerateRsaKey(4096)
1310
assert.NoError(t, err)
14-
wrongKey, err := rsa.GenerateKey(rand.Reader, 2048)
11+
wrongKey, err := GenerateRsaKey(4096)
1512
assert.NoError(t, err)
1613
msg := []byte("123")
1714

0 commit comments

Comments
 (0)