|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto" |
| 5 | + "crypto/ed25519" |
| 6 | + "crypto/hmac" |
| 7 | + "crypto/rand" |
| 8 | + "crypto/rsa" |
| 9 | + "crypto/sha256" |
| 10 | + "crypto/x509" |
| 11 | + "encoding/base64" |
| 12 | + "encoding/pem" |
| 13 | + "errors" |
| 14 | + "fmt" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + KeyTypeHmac = "HMAC" |
| 19 | + KeyTypeRsa = "RSA" |
| 20 | + KeyTypeEd25519 = "ED25519" |
| 21 | +) |
| 22 | + |
| 23 | +func SignFunc(keyType string) (func(string, string) (*string, error), error) { |
| 24 | + switch { |
| 25 | + case keyType == KeyTypeHmac: |
| 26 | + return Hmac, nil |
| 27 | + case keyType == KeyTypeRsa: |
| 28 | + return Rsa, nil |
| 29 | + case keyType == KeyTypeEd25519: |
| 30 | + return Ed25519, nil |
| 31 | + default: |
| 32 | + return nil, fmt.Errorf("unsupported keyType=%s", keyType) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func Hmac(secretKey string, data string) (*string, error) { |
| 37 | + mac := hmac.New(sha256.New, []byte(secretKey)) |
| 38 | + _, err := mac.Write([]byte(data)) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + encodeData := fmt.Sprintf("%x", (mac.Sum(nil))) |
| 43 | + return &encodeData, nil |
| 44 | +} |
| 45 | + |
| 46 | +func Rsa(secretKey string, data string) (*string, error) { |
| 47 | + block, _ := pem.Decode([]byte(secretKey)) |
| 48 | + if block == nil { |
| 49 | + return nil, errors.New("Rsa pem.Decode failed, invalid pem format secretKey") |
| 50 | + } |
| 51 | + privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes) |
| 52 | + if err != nil { |
| 53 | + return nil, fmt.Errorf("Rsa ParsePKCS8PrivateKey failed, error=%v", err.Error()) |
| 54 | + } |
| 55 | + rsaPrivateKey, ok := privateKey.(*rsa.PrivateKey) |
| 56 | + if !ok { |
| 57 | + return nil, fmt.Errorf("Rsa convert PrivateKey failed") |
| 58 | + } |
| 59 | + hashed := sha256.Sum256([]byte(data)) |
| 60 | + signature, err := rsa.SignPKCS1v15(rand.Reader, rsaPrivateKey, crypto.SHA256, hashed[:]) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + encodedSignature := base64.StdEncoding.EncodeToString(signature) |
| 65 | + return &encodedSignature, nil |
| 66 | +} |
| 67 | + |
| 68 | +func Ed25519(secretKey string, data string) (*string, error) { |
| 69 | + block, _ := pem.Decode([]byte(secretKey)) |
| 70 | + if block == nil { |
| 71 | + return nil, fmt.Errorf("Ed25519 pem.Decode failed, invalid pem format secretKey") |
| 72 | + } |
| 73 | + privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("Ed25519 call ParsePKCS8PrivateKey failed, error=%v", err.Error()) |
| 76 | + } |
| 77 | + ed25519PrivateKey, ok := privateKey.(ed25519.PrivateKey) |
| 78 | + if !ok { |
| 79 | + return nil, fmt.Errorf("Ed25519 convert PrivateKey failed") |
| 80 | + } |
| 81 | + pk := ed25519.PrivateKey(ed25519PrivateKey) |
| 82 | + signature := ed25519.Sign(pk, []byte(data)) |
| 83 | + encodedSignature := base64.StdEncoding.EncodeToString(signature) |
| 84 | + return &encodedSignature, nil |
| 85 | +} |
0 commit comments