Add Aliyun Id2MetaVerify encode impl
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
@@ -1,15 +1,19 @@
|
|||||||
package kyc
|
package kyc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/md5"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"nixcn-cms/internal/cryptography"
|
"nixcn-cms/internal/cryptography"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func EncodeB64Json(b64Json string) (*string, error) {
|
func DecodeB64Json(b64Json string) (*KycInfo, error) {
|
||||||
rawJson, err := base64.StdEncoding.DecodeString(b64Json)
|
rawJson, err := base64.StdEncoding.DecodeString(b64Json)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("invalid base64 json")
|
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 nil, errors.New("invalid json structure")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return &kyc, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func EncodeAES(kyc *KycInfo) (*string, error) {
|
||||||
plainJson, err := json.Marshal(kyc)
|
plainJson, err := json.Marshal(kyc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -48,3 +56,50 @@ func DecodeAES(cipherStr string) (*KycInfo, error) {
|
|||||||
|
|
||||||
return &kyc, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,3 +5,9 @@ type KycInfo struct {
|
|||||||
LegalName string `json:"legal_name"`
|
LegalName string `json:"legal_name"`
|
||||||
ResidentId string `json:"rsident_id"`
|
ResidentId string `json:"rsident_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MD5Ali struct {
|
||||||
|
ParamType string `json:"param_type"`
|
||||||
|
IdentifyNum string `json:"identify_num"`
|
||||||
|
UserName string `json:"user_name"`
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user