This is a creation in Article, where the information may have evolved or changed.
There is a project just need to use a symmetric plus decryption function, remembered, dz of that authcode function, so found on the Internet ha, did not find, do not know whether I search technology is not enough ha, in short, is not found, well, own hands and clothing ... GO.
Probably originally the secret key in the encrypted string, the expiration time is also in the inside, and then the verification string, the decryption time to go out before the first few dynamic secret key, of course, can not ha, then each encryption results are the same.
Key + text
TEXT[0:10] Expiration 0: no expiration
TEXT[10:26] Validation string
Text[26:] Original string
Add decryption function According to DZ Authcode rewritten go version//params[0] encryption or decryption BOOL true: Encryption false: Decryption default false//params[1] key//params[2] Encryption: Expiration Time/params [3] Dynamic key length default: 4 bits cannot be greater than 32 bit func authcode (text string, params ... interface{}) string {l: = len (params) Isencode: = Falsekey: = "" Expiry: = 0cKeyLen: = 4if L > 0 {isencode = params[0]. ( BOOL)}if L > 1 {key = Params[1]. ( String)}if L > 2 {expiry = params[2]. ( int) If expiry < 0 {expiry = 0}}if L > 3 {ckeylen = params[3]. ( int) If Ckeylen < 0 {Ckeylen = 0}}if ckeylen > + = Ckeylen = 32}timestamp: = time. Now (). Unix ()//MD5 encryption Keymkey: = Md5sum (key)//Keya involved in encryption: = Md5sum (mkey[0:16])//keyb for validating data validity: = Md5sum (mkey[16:])//dynamic partial var ke YC Stringif Ckeylen > 0 {if Isencode {///encryption, dynamically acquires a secret key KEYC = Md5sum (FMT. Sprint (timestamp)) [32-ckeylen:]} else {//decrypt the dynamic key portion from the head KEYC = text[0:ckeylen]}}//added dynamic key Cryptkey: = Keya + md5sum ( KEYA+KEYC)//Key length Keylen: = Len (cryptkey) if Isencode {////encryption first 10 bits is expired validation string 10-26 bit string validation var d int64if expiry > 0 {d = timest AMP + int64 (expiry)}Text = FMT. Sprintf ("%010d%s%s", D, md5sum (text + KeyB) [0:16], text)} else {//Decrypt Text = string (Base64decode (Text[ckeylen:]))}//string length Textlen: = Len (text) if Textlen <= 0 {return ""}//key Book Box: = Range (0, 256)//symmetric algorithm var rndkey []intcryptkeyb: = []byte (Cry Ptkey) for I: = 0; I < 256; i++ {pos: = i% Keylenrndkey = append (rndkey, int (Cryptkeyb[pos]))}j: = 0for I: = 0; I < 256; i++ {j = (j + box[i] + rndkey[i]% 256box[i], box[j] = Box[j], BOX[I]}TEXTB: = []byte (text) A: = 0J = 0var result []bytefo R I: = 0; i < Textlen; i++ {a = (A + 1)% 256j = (j + box[a])% 256box[a], box[j] = box[j], Box[a]result = append (result, byte (int (textb[i)) ^ (box [(Box[a]+box[j])%256]))} If Isencode {return KEYC + strings. Replace (Base64Encode (Result), "=", "" ",-1)}//get the first 10 bits, determine the expiry time D: = Atoi64 (String (result[0:10]), 0) if (d = = 0 | | D-timestamp > 0) && string (result[10:26]) = = Md5sum (String (result[26:]) + KeyB) [0:16] {return string (result[ :])}return ""}There are a few custom related functions, relatively simple, need to specifically explain that the Base64decode this
Func Base64decode (str string) []byte {var b []bytevar err Errorx: = Len (str) * 3 4switch {case x = = 2:str + = "=" Case X = = 1:str + = "="}if B, err = base64. Stdencoding.decodestring (str); Err! = Nil {return B}return B}
Because after the encryption to Base64Encode, replaced = empty string, so here need to deal with, PHP inside the Base64_decode function can be processed directly, here himself stuck for a few hours to study the next BASE64 principle, Base64 is a method that says 3 characters are converted to 4 characters. Since the 3-character binary is just 24 bits, divided into 4, then each only 6-bit binary, counted just 64, so base64 all the characters are 64, from a. Za.. Z0. 9+/just 64. If the conversion, if less, followed by the = number, so there will be the last = number and two = number of cases. This is the principle that complements the equals sign back, because the decoding function of go itself does not handle this.
Full file package, here, Https://github.com/last911/utils/blob/master/tools.go
It's weekend, go home. The company seems to have a lot of people.
Sunday to play, ease ... Go home