utils

package
v0.0.0-...-1bcd10a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2019 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TOP_AGENT      = 8888888888
	SUPERIOR_AGNET = 8888888888

	LOGIN    = "LOGIN"
	REGISTER = "REGISTER"
)
View Source
const (
	EC_PUBLIC_KEY_BLOCK_BEGIN = "-----BEGIN PUBLIC KEY-----"
	EC_PUBLIC_KEY_BLOCK_END   = "-----END PUBLIC KEY-----"
)
View Source
const (
	// 200*
	REQUEST_SUCCESS              uint = 200000
	PARAMS_MISSING               uint = 200400
	TOKEN_IS_EXPIRED             uint = 200401
	TELEPHONE_NEED_VERIFY        uint = 200203
	TELEPHONE_HAS_NOT_REGISTERED uint = 200402
	TELEPHONE_HAS_BEEN_USED      uint = 200403

	// 400*
	REQUEST_FAIL          uint = 400000
	SERVER_REJECT_REQUEST uint = 400403

	// 500*
	SERVER_UNKNOW_ERROR uint = 500000

	// business code
	RECORD_NOT_FOUND           uint = 200404
	USER_TELEPHONE_PSW_INVALID uint = 200404
	ADDRESS_NOT_FOUND          uint = 200404
	ORDER_NOT_FOUND            uint = 200404
	SECURITY_CODE_INVALID      uint = 200400
)
View Source
const StrSrouce string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Variables

View Source
var ErrAddressNotFound = errors.New("this address was not found, please check it")
View Source
var ErrClosedPipe = errors.New("io: read/write on closed pipe")
View Source
var ErrCreateRecordsIsEmpty = errors.New("insert records is empty, please check your option")
View Source
var ErrCurrentUserIsExist = errors.New("current user is already exist, please login")
View Source
var ErrDecrypt = errors.New("sorry, your password verification failed")
View Source
var ErrEof = errors.New("EOF")

Error

View Source
var ErrInvitationCodeInvalid = errors.New("this invitation code is invalid, please check it")
View Source
var ErrMysqlInitFailure = errors.New("error, server initial [mysql] pool failure, please check it")
View Source
var ErrNoProgress = errors.New("multiple Read calls return no data or error")
View Source
var ErrOrderNotFound = "operation failure, this order was not exist or already completed"
View Source
var ErrPEMIsNotExist = errors.New("sorry, parse key fail, the PEM file is empty")
View Source
var ErrParamsInValid = errors.New("sorry, your params are invalid, please check it")
View Source
var ErrParamsMissing = errors.New("sorry, your request params missing")
View Source
var ErrPrivKeyParseFailedFromPEM = errors.New("failed to parse private key from PEM file")
View Source
var ErrProductNotFound = "sorry, this product was not found or has been removed"
View Source
var ErrPubKeyParseFailedFromPEM = errors.New("failed to parse public key from PEM file")
View Source
var ErrRecordNotFound = errors.New("this record was not found, please check it")

common

View Source
var ErrRedisInitFailure = errors.New("error, server initial [redis] pool failure, please check it")
View Source
var ErrSecurityCodeInvalid = "security code is invalid or expired, please check it or obtain another one"
View Source
var ErrSignatureInvalid = errors.New("error, request data signature is invalid")
View Source
var ErrTelOrPswInvalid = errors.New("telephone or password is invalid")
View Source
var WarnAccountNeedVerify = errors.New("your account needs to be verified by mobile phone and then set a password")
View Source
var WarnClassifiesMissing = "sorry, there are not have any classifies"
View Source
var WarnParamsMissing = errors.New("params missing in user checking")

Warning

View Source
var WarnTelephoneAlreadyRegistered = errors.New("this phone number has been registered, please login with this telephone")
View Source
var WarnTelephoneNotRegistered = errors.New("sorry, the phone number has not been registered yet, please register first")
View Source
var WarnUserTeamMissing = "sorry, your agent teams is not initial, please call the system's administrator"

Functions

func AESDecrypt

func AESDecrypt(decodeStr string, secretKeyStr string) (string, error)

func AESEncrypt

func AESEncrypt(encodeStr string, secretKeyStr string) (string, error)

AES加密的具体算法为: AES-128-CBC,输出格式为 base64 AES加密时需要指定 iv:0102030405060708 AES加密时需要 padding either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256. https://github.com/darknessomi/musicbox/wiki/%E7%BD%91%E6%98%93%E4%BA%91%E9%9F%B3%E4%B9%90%E6%96%B0%E7%99%BB%E5%BD%95API%E5%88%86%E6%9E%90

func GenerateAndAddDurationFromNow

func GenerateAndAddDurationFromNow(duration time.Duration) time.Time

func GenerateNowDateString

func GenerateNowDateString() string

func GenerateRandString

func GenerateRandString(length int) string

func GetRedis

func GetRedis() (cache.Cache, error)

func HexToBigInt

func HexToBigInt(hexStr string) (bi *big.Int, err error)

func IsEmptyString

func IsEmptyString(strArgs ...string) bool

func MergeMaps

func MergeMaps(m1 map[string]interface{}, m2 map[string]interface{}) map[string]interface{}

func OsFileReader

func OsFileReader(fileName string) []byte

func ReadApiCache

func ReadApiCache(ct *context.Context)

func ReadCacheDataByKey

func ReadCacheDataByKey(cacheKey string) interface{}

func SHA1Encrypt

func SHA1Encrypt(plainText string) ([]byte, error)

func StringsToJSON

func StringsToJSON(str string) string

func TransformByteToJSON

func TransformByteToJSON(str []byte) interface{}

func TransformInterfaceToMap

func TransformInterfaceToMap(origin interface{}) map[string]interface{}

func TransformStructToJSONMap

func TransformStructToJSONMap(model interface{}) (map[string]interface{}, error)

func TransformStructToMap

func TransformStructToMap(st interface{}) map[string]interface{}

can not handle key as Hump named

func WriteApiCache

func WriteApiCache(ct *context.Context, response interface{})

Types

type Crypto

type Crypto struct {
	SecretKey  string
	OriginData interface{}
}

func (*Crypto) CreateSecretKey

func (ct *Crypto) CreateSecretKey(size int) string

func (*Crypto) RSAEncrypt

func (ct *Crypto) RSAEncrypt(secKey string, pubKey string, modulus string) string

type ECDH

type ECDH interface {
	GenerateECKeyPair() (*EllipticPrivateKey, *EllipticPublicKey, error)
	GenerateECKeyPairToPEM(curve elliptic.Curve) ([]byte, []byte, error)
	GeneratePKIXPublicKey(publicKeyBlock string) string
	Marshal(pub crypto.PublicKey) []byte
	Unmarshal(data []byte) (crypto.PublicKey, bool)
	ParsePKCS8ECPrivateKey(privateKeyDerBytes []byte) (*EllipticECDH, error)
	ParseECPrivateKeyFromPEM(filePath string) (ellipticECDH *EllipticECDH, err error)
	ParseECPublicKeyFromPEM(publicKeyStr string) (ecPubKey *EllipticPublicKey, ecdsaPubKey *ecdsa.PublicKey, err error)
	ParsePKIXECPublicKey(publicKeyDerBytes []byte) (*EllipticPublicKey, *ecdsa.PublicKey, error)
	GetPKIXPublicKeyBlockFromPEM(pemBytes []byte) string
	DecodePEMToDERBytes(pemBytes []byte) []byte
	ComputeSecret(privKey *EllipticPrivateKey, pubKey *EllipticPublicKey) ([]byte, error)

	Signature(messageBytes []byte, privateKey *ecdsa.PrivateKey) (string, error)
	VerifySignature(signatureData *SignatureData, publicKey *ecdsa.PublicKey) (status bool)
}

type EllipticECDH

type EllipticECDH struct {
	PrivateKey      *EllipticPrivateKey
	PublicKey       *EllipticPublicKey
	ECDSAPrivateKey *ecdsa.PrivateKey
}

func (*EllipticECDH) ComputeSecret

func (e *EllipticECDH) ComputeSecret(privKey *EllipticPrivateKey, pubKey *EllipticPublicKey) ([]byte, error)

ECDH Compute Secret

func (*EllipticECDH) DecodePEMToDERBytes

func (e *EllipticECDH) DecodePEMToDERBytes(pemBytes []byte) []byte

decode PEM, support private/public key, return block data

func (*EllipticECDH) GenerateECKeyPair

func (e *EllipticECDH) GenerateECKeyPair() (*EllipticPrivateKey, *EllipticPublicKey, error)

generate ec key pair and return

func (*EllipticECDH) GenerateECKeyPairToPEM

func (e *EllipticECDH) GenerateECKeyPairToPEM(curve elliptic.Curve) ([]byte, []byte, error)

generate ec key pair and write them to PEM file, then return them

func (*EllipticECDH) GeneratePKIXPublicKey

func (e *EllipticECDH) GeneratePKIXPublicKey(publicKeyBlock string) string

handle client public key which is less of begin and end

func (*EllipticECDH) GetPKIXPublicKeyBlockFromPEM

func (e *EllipticECDH) GetPKIXPublicKeyBlockFromPEM(pemBytes []byte) string

get public key form pem and format to base64

func (*EllipticECDH) Marshal

func (e *EllipticECDH) Marshal(pub crypto.PublicKey) []byte

Marshal ec public key

func (*EllipticECDH) ParseECPrivateKeyFromPEM

func (e *EllipticECDH) ParseECPrivateKeyFromPEM(filePath string) (*EllipticECDH, error)

func (*EllipticECDH) ParseECPublicKeyFromPEM

func (e *EllipticECDH) ParseECPublicKeyFromPEM(publicKeyStr string) (*EllipticPublicKey, *ecdsa.PublicKey, error)

func (*EllipticECDH) ParsePKCS8ECPrivateKey

func (e *EllipticECDH) ParsePKCS8ECPrivateKey(privateKeyDerBytes []byte) (*EllipticECDH, error)

private pem file, parse PKCS#8 private key

func (*EllipticECDH) ParsePKIXECPublicKey

func (e *EllipticECDH) ParsePKIXECPublicKey(publicKeyDerBytes []byte) (*EllipticPublicKey, *ecdsa.PublicKey, error)

func (*EllipticECDH) Signature

func (e *EllipticECDH) Signature(messageBytes []byte, privateKey *ecdsa.PrivateKey) (string, error)

func (*EllipticECDH) Unmarshal

func (e *EllipticECDH) Unmarshal(data []byte) (crypto.PublicKey, bool)

Unmarshal ec public key

func (*EllipticECDH) VerifySignature

func (e *EllipticECDH) VerifySignature(signatureData *SignatureData, publicKey *ecdsa.PublicKey) (status bool)

type EllipticPrivateKey

type EllipticPrivateKey struct {
	D []byte
}

type EllipticPublicKey

type EllipticPublicKey struct {
	X *big.Int
	Y *big.Int
}

type SignatureData

type SignatureData struct {
	// contains filtered or unexported fields
}

SignatureData 用于保存签名的数据

func HandleSignatureData

func HandleSignatureData(data string, signatureBase64 string) (signatureData *SignatureData, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL