func (_ Test) EncryptXXTea() { str := "Hello World! 你好,中国!" key := "1234567890" encrypt_data := xxtea.Encrypt([]byte(str), []byte(key)) decrypt_data := string(xxtea.Decrypt(encrypt_data, []byte(key))) if str == decrypt_data { e.InfoLog.Println("success!") } else { e.InfoLog.Println("fail!") } }
// Authenticate authenticates a token and return the original model.Authtoken func (au Authority) Authenticate(token string) (model.Authtoken, error) { bytes, err := hex.DecodeString(token) if err != nil { return model.Authtoken{}, errors.New(errors.ErrServerInternal, "hack??", fmt.Sprintf("auth error %v", err)) } for i := range au.privateKeys { bytes = xxtea.Decrypt(bytes, au.privateKeys[i]) } authtoken := bytes2authtoken(bytes) if time.Since(authtoken.created) > au.tokenLifeTime { return model.Authtoken{}, errors.New(errors.ErrUnauthorized, "", "token has been expired") } return authtoken.authtoken, nil }