Files
yhy-mager/backend/internal/payments/crypto.go
xiewenjun dc2efcde08
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled
feat: add online payment management and checkout
Co-Authored-By: Craft Agent <agents-noreply@craft.do>
2026-10-15 09:26:40 +08:00

120 lines
2.7 KiB
Go

package payments
import (
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"io"
"os"
"strconv"
"strings"
)
func readKey(v string) ([]byte, error) {
if strings.HasPrefix(v, "@") {
return os.ReadFile(strings.TrimPrefix(v, "@"))
}
return []byte(strings.ReplaceAll(v, "\\n", "\n")), nil
}
func privateKey(v string) (*rsa.PrivateKey, error) {
b, e := readKey(v)
if e != nil {
return nil, e
}
p, _ := pem.Decode(b)
if p == nil {
return nil, errors.New("invalid PEM private key")
}
if k, e := x509.ParsePKCS8PrivateKey(p.Bytes); e == nil {
if r, ok := k.(*rsa.PrivateKey); ok {
return r, nil
}
}
if r, e := x509.ParsePKCS1PrivateKey(p.Bytes); e == nil {
return r, nil
}
return nil, errors.New("invalid RSA private key")
}
func publicKey(v string) (*rsa.PublicKey, error) {
b, e := readKey(v)
if e != nil {
return nil, e
}
p, _ := pem.Decode(b)
if p == nil {
return nil, errors.New("invalid PEM public key")
}
if c, e := x509.ParseCertificate(p.Bytes); e == nil {
if r, ok := c.PublicKey.(*rsa.PublicKey); ok {
return r, nil
}
}
if k, e := x509.ParsePKIXPublicKey(p.Bytes); e == nil {
if r, ok := k.(*rsa.PublicKey); ok {
return r, nil
}
}
return nil, errors.New("invalid RSA public key")
}
func signRSA(priv *rsa.PrivateKey, s string) (string, error) {
h := sha256.Sum256([]byte(s))
b, e := rsa.SignPKCS1v15(rand.Reader, priv, cryptoHash(), h[:])
return base64.StdEncoding.EncodeToString(b), e
}
func cryptoHash() crypto.Hash { return crypto.SHA256 }
func decryptGCM(key, nonce, ad, ct string) (string, error) {
block, e := aes.NewCipher([]byte(key))
if e != nil {
return "", e
}
g, e := cipher.NewGCM(block)
if e != nil {
return "", e
}
if len(nonce) != g.NonceSize() {
return "", errors.New("invalid GCM nonce length")
}
raw, e := base64.StdEncoding.DecodeString(ct)
if e != nil {
return "", e
}
plain, e := g.Open(nil, []byte(nonce), raw, []byte(ad))
return string(plain), e
}
func cents(v any) (int64, error) {
switch x := v.(type) {
case json.Number:
return strconv.ParseInt(string(x), 10, 64)
case string:
return strconv.ParseInt(x, 10, 64)
}
return 0, errors.New("invalid amount")
}
func verifyRSA(key *rsa.PublicKey, message, signature string) error {
b, e := base64.StdEncoding.DecodeString(signature)
if e != nil {
return e
}
h := sha256.Sum256([]byte(message))
return rsa.VerifyPKCS1v15(key, crypto.SHA256, h[:], b)
}
func readLimited(r io.Reader) ([]byte, error) {
b, e := io.ReadAll(io.LimitReader(r, (2<<20)+1))
if e != nil {
return nil, e
}
if len(b) > 2<<20 {
return nil, errors.New("payment response too large")
}
return b, nil
}