1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package unionpay
- import (
- "crypto"
- "crypto/rand"
- "crypto/rsa"
- "crypto/sha1"
- "crypto/sha256"
- )
- // encrypt RSA 加密
- func encrypt(publicKey *rsa.PublicKey, src []byte) (data []byte, err error) {
- data, err = rsa.EncryptPKCS1v15(rand.Reader, publicKey, src)
- return
- }
- // decrypt RSA 解密
- func decrypt(priveKey *rsa.PrivateKey, ciphertext []byte) (data []byte, err error) {
- data, err = rsa.DecryptPKCS1v15(rand.Reader, priveKey, ciphertext)
- return
- }
- // sign RSA 签名
- func sign(priveKey *rsa.PrivateKey, src []byte) (data []byte, err error) {
- hashed := sha256.Sum256(src)
- //hashed = sha256.Sum256(hashed[:])
- data, err = rsa.SignPKCS1v15(rand.Reader, priveKey, crypto.SHA256, hashed[:])
- return
- }
- // signsha1 RSA sha1签名
- func signsha1(priveKey *rsa.PrivateKey, src []byte) (data []byte, err error) {
- hashed := sha1.Sum(src)
- data, err = rsa.SignPKCS1v15(rand.Reader, priveKey, crypto.SHA1, hashed[:])
- return
- }
- // verify RSA 签名验证
- func verify(publicKey *rsa.PublicKey, src, sig []byte) (err error) {
- hashed := sha256.Sum256(src)
- err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, hashed[:], sig)
- return
- }
|