sign

package
v3.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package sign implements signature and verify for wechat pay. It includes all encryption and decryption related implementations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecryptByAes256Gcm

func DecryptByAes256Gcm(key, nonce, additionalData []byte, cipherText string) ([]byte, error)

DecryptByAes256Gcm uses algorithm aes-256-gcm to decrypt text. The key argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

Example
key := []byte("AES256Key-32Characters1234567890")
nonce := []byte("eabb3e044577")
data := []byte("certificate")
ciphertext := "tJjSQMG758oX39qpn/RoZPZ3qh8LRIIwcnQeFhU/alQ="

plaintext, _ := DecryptByAes256Gcm(key, nonce, data, ciphertext)
fmt.Println(string(plaintext))
Output:

exampleplaintext

func EncryptByAes256Gcm

func EncryptByAes256Gcm(key, nonce, additionalData []byte, plainText string) (string, error)

EncryptByAes256Gcm uses algorithm aes-256-gcm to encrypt text and return a base64 string. The key argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

Example
key := []byte("AES256Key-32Characters1234567890")
nonce := []byte("eabb3e044577")
data := []byte("certificate")
text := "exampleplaintext"
ciphertext, _ := EncryptByAes256Gcm(key, nonce, data, text)
fmt.Println(ciphertext)
Output:

tJjSQMG758oX39qpn/RoZPZ3qh8LRIIwcnQeFhU/alQ=

func GenerateSignature

func GenerateSignature(privateKey *rsa.PrivateKey, reqSign *RequestSignature, mchId, serialNo string) (string, error)

GenerateSignature generate a signature string, privateKey is an RSA key.

func LoadRSAPrivateKey

func LoadRSAPrivateKey(buffer []byte) (*rsa.PrivateKey, error)

LoadRSAPrivateKey load the buffer about rsa private cert and return private key.

func LoadRSAPrivateKeyFromFile

func LoadRSAPrivateKeyFromFile(filename string) (*rsa.PrivateKey, error)

LoadRSAPrivateKeyFromFile load the file about rsa private key and return private key.

func LoadRSAPrivateKeyFromTxt

func LoadRSAPrivateKeyFromTxt(privateKeyTxt string) (*rsa.PrivateKey, error)

LoadRSAPrivateKeyFromTxt load the string about rsa private key and return private key.

Example
var mockRSAPrivateKeyTxt = `-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCprsmcXPHqLtnP
oPDGUoMULK2WOo5FW8c72Svnqn/4aXPaJhlOtPxtX2frqIhTjwcOs6hNm3XFTGBL
MrdB94YQvj+Q7P12GNmxXG+9Ms+uUyJToYjlYDAG6UFKE10Jkm9cDGuLSkekU1Ao
rKE1G1wndH37w4AzVXoGBQ3NIiyW8jIm8Zi3/WNCVpHUoXYUuyhFEZ23fXytnps4
hARgg6NvPncIKtWvlUh85ZVOSsqc1T8dFaeDRXaj7r3jdJJ74tsGRMvZyUipJXyE
3uR2QkrGyia+0phDpC6zeMMpP+MQO9ohh+xQWBCeyvQjjnOPAlGThl+ThfXImU30
HL17oHdBAgMBAAECggEADm6FSz1Efgx6DgS8NcHy0BZ0tSBJ1XBW46o2579Cnxgo
+FbhNCaEibDhn9N3tNOnYAK7v84HGD7EueCYYY3x4x6rPWJKtG6spT8dadQWgdck
RkSo5glmTFAuc2RuN1AzFHsh8njg2wMTAEKee2vWTKzFwlIAZ11PwY9Qey/65uOT
Bi8q1Rssu6xofNadO5MbqMJ1Tl8DDIaLGnzTzbHrk9thBUo1FwFjJWTVI7nz2En4
Yc/G1/LQJfiQ31F+lkL3j6ABRJqtsgb07r9H/hT6+fd1hGDt2qKuS+E1mLDp9fHw
n6UyS4HyB7DA/XtFZ9z0VtAlmcGoUkyJLtXjEmwsGQKBgQDeCtE3spULpC7VPqk1
xv034C6zybZ7y8kSKwRvyYwkzdgSRgVaKsTVb8RNYor8hoGrVgdXFqQUI8O/v1cN
9wFoGYJT0LHre/YzOg31TkQkBfHHCFH/L50uOJcIQueftctz5Bwj6bJO/ih5iIAK
yjrHse4PdIiEJfz2D9hc4wnxrwKBgQDDogrWlUCTj2fvmZfkWR3Hbs0kIHd7zjIk
bJJONGtD8gE4i562tajC1mKoQEwt4YSwWsBkGAw1LhvMROQFT6AOaIIhHNex1Z3t
c2gAdEeWOMmzZnnhwWzTiYJomixrFkmEwT3EJK89GO3E0FH5S+G1P1tNXq38Vpty
1YVqOgMSDwKBgFrzuWGEQDMljJ2C7lL98KlbpiW1AY/SGMndXxLfTw2gV9qcXgLi
NABtqM4+CEqKWkExmw4cUxeA0uUPXnx06lmW4WCtwsN/4oh3RlJuPdE3siLiEJxk
B5FwUsVqinBMSktta+12A7kBuNiXhkNlNRCpnKcuB+GBog20zd62jVM3AoGBALcA
zFazQ7dFfRq7eUUYwCyhT7Et1dewqWM9VRdnHbhvmAjHQu7zvCyW069Ehn6c6bz3
B+YaQME2orZQ82SsebNAvAoxquwmQhevz2gtXhH+iWASyo0Onbi8d4tWPZrnPFq9
UgQ7tNnYigOEREqKW1drLwOPP/4/Hicr6iPWpKytAoGAEQ6J/RB/olEAC46ACoFo
FBgA+GUbDB0xBcA2inEt3q//208YMkjnKM871n89HpAgms5xrK32T69lduebk7Ar
9wWvkJVUwI9VDXomCFQqtiGzHlTl1Xq31BfeIDyq1ayQmTkRpRqIagbDZVtM+ha/
0I2SEzTObt07wcYcYG2Chvg=
-----END PRIVATE KEY-----`

privateKey, _ := LoadRSAPrivateKeyFromTxt(mockRSAPrivateKeyTxt)
fmt.Println(privateKey.N.String())
Output:

21420464488684196166133246342556659636550593370148748831102541581055400283034358001792168425545200813983192883572960524746874583639926463744817032344327797479417553360454010600259473981755446176276791560274198974049554918857785979093044935805583861562312917387112389597175890991416299121425504032865468169023321187470378144804054107007826722660772241802037293444028379616716611420761841023299077453591435611852308858738362465482152136667219327839959299377797228243297859693630646114090882339966320817036494553227805348660968120598764552671360953337161474984336270587583567150172241067256189055044836175172103951120193

func LoadRSAPublicKeyFromCert

func LoadRSAPublicKeyFromCert(buffer []byte) (*rsa.PublicKey, error)

LoadRSAPublicKeyFromCert load the buffer about rsa cert and return public key.

func SignatureSHA256WithRSA

func SignatureSHA256WithRSA(privateKey *rsa.PrivateKey, plain []byte) (string, error)

SignatureSHA256WithRSA calculates the signature of hashed using SHA256 with RSA.

func VerifySHA256WithRSA

func VerifySHA256WithRSA(publicKey *rsa.PublicKey, signature string, plain []byte) error

VerifySHA256WithRSA verify that the signature is available using SHA256 with RSA.

func VerifySignature

func VerifySignature(publicKey *rsa.PublicKey, respSign *ResponseSignature, signature string) error

VerifySignature verify that the signature is passed. privateKey is an RSA key.

Types

type RequestSignature

type RequestSignature struct {
	Method    string
	Url       string
	Timestamp int64
	Nonce     string
	Body      []byte
}

RequestSignature is request signature information. The format as shown below: HTTP Method\nURL\nTimestamp\nNonce string\nHTTP Body\n

func NewRequestSignature

func NewRequestSignature(method, url string, body []byte) *RequestSignature

NewRequestSignature return a request signature

func (*RequestSignature) Marshal

func (r *RequestSignature) Marshal() ([]byte, error)

Marshal returns the array byte about the request signature.

type ResponseSignature

type ResponseSignature struct {
	Body      []byte
	Timestamp int64
	Nonce     string
}

ResponseSignature is response signature information from the response of wechat pay. The format as shown below: Timestamp\nNonce string\nHTTP Body\n

func (*ResponseSignature) Marshal

func (r *ResponseSignature) Marshal() ([]byte, error)

Marshal returns the array byte about the response signature.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL