init: v1.0.0
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package fpe
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"xdx.jelly/xgcl/sm/sm4"
|
||||
)
|
||||
|
||||
// return the check code of id.
|
||||
func checkCodeOfID(id0 string) byte {
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
// EncryptID 加密身份证号。最后一位为校验位。
|
||||
// key为SM4密钥,16字节
|
||||
// tweak随机数,任意长度,可明文保存。
|
||||
func EncryptID(id string, key []byte, tweak []byte) (string, error) {
|
||||
if len(id) != 18 {
|
||||
return "", errors.New("wrong id")
|
||||
}
|
||||
|
||||
if len(key) != 16 {
|
||||
return "", errors.New("wrong key")
|
||||
}
|
||||
b, err := sm4.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
f := NewFF1(b, Numeric)
|
||||
numerals, err := f.Encode(id[:len(id)-1])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
cipher, err := f.Encrypt(tweak, numerals)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
encryptedID, err := f.Decode(cipher)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// TODO: add the check code
|
||||
|
||||
return encryptedID, nil
|
||||
|
||||
}
|
||||
|
||||
// 加密纯数字的手机号
|
||||
func EncryptPhoneNumber(phoneNumber string, key []byte, tweak []byte) (string, error) {
|
||||
|
||||
panic("todo")
|
||||
}
|
||||
Reference in New Issue
Block a user