forked from One-com/dkimcrypt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsign_verify.go
52 lines (42 loc) · 1.17 KB
/
sign_verify.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
package dkimcrypt
import (
"crypto"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"io"
)
// Sign will return the signature of the message in 'message' using the private
// key in the file at 'privkeypath'.
func Sign(message []byte, privkeypath string) (out []byte, err error) {
var privatekey *rsa.PrivateKey
if privatekey, err = getPrivKeyFromFile(privkeypath); err != nil {
return nil, err
}
// SignPKCS1v15
var h crypto.Hash
hash := md5.New()
io.WriteString(hash, string(message))
hashed := hash.Sum(nil)
h = crypto.MD5
signature, err := rsa.SignPKCS1v15(rand.Reader, privatekey, h, hashed)
if err != nil {
return nil, err
}
return signature, nil
}
// Verify a signature given the signature, the message it signed and the
// selector and domain that signed it. If err is nil, then the signature is
// good.
func Verify(message, signature []byte, selector, domain string) (err error) {
var pubkey *rsa.PublicKey
if pubkey, err = getPubKey(selector, domain); err != nil {
return err
}
var h crypto.Hash
hash := md5.New()
io.WriteString(hash, string(message))
hashed := hash.Sum(nil)
h = crypto.MD5
return rsa.VerifyPKCS1v15(pubkey, h, hashed, signature)
}