/
signed.go
108 lines (91 loc) · 2.81 KB
/
signed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"os"
myrsa "github.com/Eric-GreenComb/contrib/crypto/rsa"
)
func main() {
hexPublicKey := hex.EncodeToString([]byte("gopper.in"))
fmt.Println(hexPublicKey)
initData := "pub" + hexPublicKey
init := []byte(initData)
hashed := sha256.Sum256([]byte(init))
// hashed := md5.Sum([]byte(init))
_sign, err := myrsa.Sign(crypto.SHA256, hashed[:], key)
// _sign, err := RsaSign(crypto.MD5, hashed[:], key)
fmt.Println(hex.EncodeToString(_sign))
err = myrsa.Verify(crypto.SHA256, hashed[:], pub, _sign)
// err = RsaVerify(crypto.MD5, hashed[:], pub, _sign)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Sign is right")
}
}
// RsaSign rsa sign
// hashed := md5.Sum(src)
// hashed := sha256.Sum256(src)
func RsaSign(hash crypto.Hash, hashed, privateKey []byte) ([]byte, error) {
// Only small messages can be signed directly; thus the hash of a
// message, rather than the message itself, is signed. This requires
// that the hash function be collision resistant. SHA-256 is the
// least-strong hash function that should be used for this at the time
// of writing (2016).
block, _ := pem.Decode(privateKey)
if block == nil {
return nil, errors.New("private key error")
}
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return rsa.SignPKCS1v15(rand.Reader, priv, hash, hashed)
// return rsa.SignPKCS1v15(rand.Reader, priv, crypto.MD5, hashed[:])
// return rsa.SignPKCS1v15(rand.Reader, priv, crypto.SHA256, hashed[:])
}
// RsaVerify rsa verify
// hashed := md5.Sum(src)
// hashed := sha256.Sum256(src)
// signature is a valid signature of message from the public key.
func RsaVerify(hash crypto.Hash, hashed, publicKey, signature []byte) error {
// Only small messages can be signed directly; thus the hash of a
// message, rather than the message itself, is signed. This requires
// that the hash function be collision resistant. SHA-256 is the
// least-strong hash function that should be used for this at the time
// of writing (2016).
block, _ := pem.Decode(publicKey)
if block == nil {
return errors.New("public key error")
}
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return err
}
pub := pubInterface.(*rsa.PublicKey)
return rsa.VerifyPKCS1v15(pub, hash, hashed, signature)
// return rsa.VerifyPKCS1v15(pub, crypto.MD5, hashed[:], signature)
// return rsa.VerifyPKCS1v15(pub, crypto.SHA256, hashed[:], signature)
}
var pub, key []byte
func init() {
var err error
pub, err = ioutil.ReadFile("public.pem")
if err != nil {
fmt.Println(err.Error())
os.Exit(-1)
}
key, err = ioutil.ReadFile("private.pem")
if err != nil {
fmt.Println(err.Error())
os.Exit(-1)
}
}