Golang encryption, decryption, and small guid Method

Source: Internet
Author: User
Tags md5 encryption sha1

Golang's MD5 encryption. base64 decrypts the guid code:

/*** Used for encryption and decryption (including MD5 encryption and base64 encryption/Decryption) and guid generation time: * zhifieya */package safeimport ("crypto/MD5" "crypto/Rand" "crypto/sha1" "encoding/base64" "encoding/Hex" "Io" "strings ") const (// base64 partition table, do not have repeated base64table = "<> :;',. /[email protected] # $ cdvwx % ^ & * abyzabcghijklmnopqrstuvwxyz01efghijklmnopqrstu2345678 (DEF) _ + | {} [] 9/"hashfunctionheader =" Zh. ife. iya "hashfunctionfooter =" 09. o25.o000078 ")/*** Character string for MD5 encryption, cannot decrypt */func getmd5string (s string) string {H: = md5.new () H. write ([] Byte (s) // use zhifeiya name for hash value. Do not change return hex after setting. encodetostring (H. sum (NiL)}/* Get sha1 string */func getsha1string (s string) string {T: = sha1.new () T. write ([] Byte (s) return hex. encodetostring (T. sum (NiL)}/*** get a guid value */func getguid () string {B: = make ([] Byte, 48) If _, err: = Io. readfull (Rand. reader, B); Err! = Nil {return ""} return getmd5string (base64.urlencoding. encodetostring (B)} var coder = base64.newencoding (base64table)/*** base64 encryption */func base64encode (STR string) string {var SRC [] Byte = [] Byte (hashfunctionheader + STR + hashfunctionfooter) return string ([] Byte (coder. encodetostring (SRC)}/*** base64 decryption */func base64decode (STR string) (string, error) {var SRC [] Byte = [] Byte (STR) by, err: = coder. decodestring (string (SRC) return strings. replace (strings. replace (string (by), hashfunctionheader, "",-1), hashfunctionfooter, "",-1), err}

 

Golang encryption, decryption, and small guid Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.