diff --git a/pkgs/kyc/kyc.go b/pkgs/kyc/kyc.go index 4b5b09d..11c1a72 100644 --- a/pkgs/kyc/kyc.go +++ b/pkgs/kyc/kyc.go @@ -1,15 +1,19 @@ package kyc import ( + "crypto/md5" "encoding/base64" + "encoding/hex" "encoding/json" "errors" + "fmt" "nixcn-cms/internal/cryptography" + "unicode/utf8" "github.com/spf13/viper" ) -func EncodeB64Json(b64Json string) (*string, error) { +func DecodeB64Json(b64Json string) (*KycInfo, error) { rawJson, err := base64.StdEncoding.DecodeString(b64Json) if err != nil { return nil, errors.New("invalid base64 json") @@ -20,6 +24,10 @@ func EncodeB64Json(b64Json string) (*string, error) { return nil, errors.New("invalid json structure") } + return &kyc, nil +} + +func EncodeAES(kyc *KycInfo) (*string, error) { plainJson, err := json.Marshal(kyc) if err != nil { return nil, err @@ -48,3 +56,50 @@ func DecodeAES(cipherStr string) (*KycInfo, error) { return &kyc, nil } + +func MD5AliEnc(kyc *KycInfo) (*MD5Ali, error) { + if kyc.Type != "Chinese" { + return nil, nil + } + + // MD5 Legal Name rule: First Chinese char md5enc, remaining plain, at least 2 Chinese chars + if len(kyc.LegalName) < 2 || utf8.RuneCountInString(kyc.LegalName) < 2 { + return nil, fmt.Errorf("input string must have at least 2 Chinese characters") + } + + lnFirstRune, size := utf8.DecodeRuneInString(kyc.LegalName) + if lnFirstRune == utf8.RuneError { + return nil, fmt.Errorf("invalid first character") + } + + lnHash := md5.New() + lnHash.Write([]byte(string(lnFirstRune))) + lnFirstHash := hex.EncodeToString(lnHash.Sum(nil)) + + lnRemaining := kyc.LegalName[size:] + + ln := lnFirstHash + lnRemaining + + // MD5 Resident Id rule: First 6 char plain, middle birthdate md5enc, last 4 char plain, at least 18 chars + if len(kyc.ResidentId) < 18 { + return nil, fmt.Errorf("input string must have at least 18 characters") + } + + ridPrefix := kyc.ResidentId[:6] + ridSuffix := kyc.ResidentId[len(kyc.ResidentId)-4:] + ridMiddle := kyc.ResidentId[6 : len(kyc.ResidentId)-4] + + ridHash := md5.New() + ridHash.Write([]byte(ridMiddle)) + ridMiddleHash := hex.EncodeToString(ridHash.Sum(nil)) + + rid := ridPrefix + ridMiddleHash + ridSuffix + + // Aliyun Id2MetaVerify API Params + var kycAli MD5Ali + kycAli.ParamType = "md5" + kycAli.UserName = ln + kycAli.IdentifyNum = rid + + return &kycAli, nil +} diff --git a/pkgs/kyc/types.go b/pkgs/kyc/types.go index 92595e3..74eb69e 100644 --- a/pkgs/kyc/types.go +++ b/pkgs/kyc/types.go @@ -5,3 +5,9 @@ type KycInfo struct { LegalName string `json:"legal_name"` ResidentId string `json:"rsident_id"` } + +type MD5Ali struct { + ParamType string `json:"param_type"` + IdentifyNum string `json:"identify_num"` + UserName string `json:"user_name"` +}