golang数字签名

Posted chaselogs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang数字签名相关的知识,希望对你有一定的参考价值。

 1 package main
 2 
 3 import (
 4     "crypto"
 5     "crypto/rand"
 6     "crypto/rsa"
 7     "crypto/sha256"
 8     "crypto/x509"
 9     "encoding/pem"
10     "fmt"
11 )
12 
13 //openssl genrsa -out rsa_private_key.pem 1024
14 var privateKey = []byte(`-----BEGIN RSA PRIVATE KEY-----
15 MIICXAIBAAKBgQDbzwsz/q1DSjGk+K5kJdFwqVxhwvO+Mlu7uwdUP0mK/nh5I5O6
16 01bovvHS9hauujlFxbvP/ekREnuyHYOiUe5Lf5m7zqd0cWmLu88gnphQf0aDvYpJ
17 +/iVo4RqjC8A/fJvvdnYJfY9C+rRggy4DUlX+xyUC2VE1J+lQ1DOYy9uvwIDAQAB
18 AoGBANuk+LzsMJac/R6soRbR84dwtS+7b3UPPTtKmPhGhWavAhOKyYRWyvGoXF1C
19 K0hOLgzameItB0hX/+LsOy1oUYSl0egk91dpn6AnStASz8BPNAWr44RPIiaQSDmu
20 +BiQNOLkAvh56hEIrnBch6u1QTBlHaaD/E8LCiBcKTaqiR6BAkEA75e6nnH/giXB
21 vULzPLpIj4pmgXAE0EJ11zukb4oFjRMUvuBTgEUNK6Pq/iNMNZZIf9AuPVjXwu5y
22 OPteVcEJUwJBAOrcfDBmAaivhGqVd8IwRDwWGTtw9ToSW2UGn75BgUGpDbeWRYz6
23 ApUJUe0VG8vD7P6G9SdKVaXz1CJAWUVpG2UCQGiGKh0boJt3u1jFD3aL0kIE4AlS
24 BFeS1vQKCge7EAbmryNCnEsWVNOYFFnArmumX4SbHsEWtDJPcjdXumTw7Q8CQGA/
25 371N8Jwa4h90BdJVSYF+ZPTRRA56e24Zmtmzqq8oAvEb6oDt7KT7XtHUdsG5F6Tv
26 1gQ2GIecN3GaAXvKqWkCQCEn3r0gQQza4NvKX2vp8huK8v8nE32e4vkcZLeJJxC1
27 3xV2R8U5YufBdbhQdGzVYnoVAkh/ZvTkFr/vCAl11Bo=
28 -----END RSA PRIVATE KEY-----
29 `)
30 
31 //openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
32 var publicKey = []byte(`-----BEGIN PUBLIC KEY-----
33 MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDbzwsz/q1DSjGk+K5kJdFwqVxh
34 wvO+Mlu7uwdUP0mK/nh5I5O601bovvHS9hauujlFxbvP/ekREnuyHYOiUe5Lf5m7
35 zqd0cWmLu88gnphQf0aDvYpJ+/iVo4RqjC8A/fJvvdnYJfY9C+rRggy4DUlX+xyU
36 C2VE1J+lQ1DOYy9uvwIDAQAB
37 -----END PUBLIC KEY-----
38 `)
39 
40 func main() {
41     sourceData := []byte("123456")
42     signData, err := SignatureRSA(sourceData)
43     if err != nil {
44         fmt.Println("加密出错:", err)
45         return
46     }
47     fmt.Println("加密正确:")
48 
49     err = VerifyRSA(sourceData, signData)
50     if err != nil {
51         fmt.Println("校验出错:", err)
52         return
53     }
54     fmt.Println("校验正确:")
55 }
56 
57 // To Byte
58 func SignatureRSA(sourceData []byte) ([]byte, error) {
59     //解析
60     block, _ := pem.Decode(privateKey)
61     privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
62     if err != nil {
63         return nil, err
64     }
65     //哈希加密
66     myHash := sha256.New()
67     myHash.Write(sourceData)
68     hashRes := myHash.Sum(nil)
69     //对哈希结果进行签名
70     res, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hashRes)
71     if err != nil {
72         return nil, err
73     }
74     return res, nil
75 }
76 
77 func VerifyRSA(sourceData []byte, signedData []byte) error {
78     //pem解密
79     block, _ := pem.Decode(publicKey)
80     publicInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
81     if err != nil {
82         return err
83     }
84     publicKey := publicInterface.(*rsa.PublicKey)
85     //元数据哈希加密
86     mySha := sha256.New()
87     mySha.Write(sourceData)
88     res := mySha.Sum(nil)
89 
90     //校验签名
91     err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, res, signedData)
92     if err != nil {
93         return err
94     }
95     return nil
96 }

 

以上是关于golang数字签名的主要内容,如果未能解决你的问题,请参考以下文章

代码片段 - Golang 实现简单的 Web 服务器

代码片段 - Golang 实现集合操作

GoLang学习之变量定义和初始化

json [Golang] golang #golang #snippets中有用的片段

java golang oop 2文章片段

golang 去练习片段