Golang sha1,base64

Source: Internet
Author: User
Tags sha1 sha1 encryption
This is a creation in Article, where the information may have evolved or changed.

1.BASE64 encoding

Package Main//This syntax introduces the ENCODING/BASE64 package and uses the name b64 instead of the default base64. This saves you a bit of space. Import b64"encoding/base64"Import"FMT"Func Main () {//This is the string that will be encoded. Data: ="abc123!? $*& () '-=@~"    //Go supports both standard and URL-compatible base64 formats. The encoding needs to use []byte type parameters, so the string will be converted to this type. SENC: = b64. Stdencoding.encodetostring ([]byte(data)) FMT. Println (SENC)//decoding may return an error, and if you are unsure whether the input information is properly formatted, then you will need to do a bug check. Sdec, _: =b64. Stdencoding.decodestring (SENC) fmt. Println (string(SDEC)) fmt. Println ()//codecs are encoded using the URL-compatible base64 format. UENC: = b64. Urlencoding.encodetostring ([]byte(data)) FMT. Println (UENC) UDec, _:=b64. Urlencoding.decodestring (UENC) fmt. Println (string(UDEC))//standard base64 encoding and URL-compatible base64 encoded strings are slightly different (suffix + and-), but both can be correctly decoded to the original string. }/*The results are as follows: $ go run base64-encoding.goywjjmtizit8kkiyoksctpub+abc123!? $*& () '-=@~ywjjmtizit8kkiyoksctpub-abc123!? $*& () '-=@~*/

2.SHA1 encryption

Package Main//Go implements a series of hash functions in multiple crypto/* packages. Import"CRYPTO/SHA1"Import"FMT"Func Main () {s:="SHA1 This string"    //the way to produce a hash is SHA1. New (), SHA1. Write (bytes), and then SHA1. Sum ([]byte{}). Here we start with a new hash. H: =SHA1. New ()//writes the bytes to be processed. If it is a string, you need to use []byte (s) to force the conversion to a byte array. H.write ([]byte(s))//This is used to get the final hash value of the character slice. The Sum parameter can be used to append additional byte slices to the existing character slices: generally not required. BS: =h.sum (Nil)//SHA1 values are often output in 16, for example in Git commit. Use%x to format the hash result as a 16 binary string. FMT. Println (s) fmt. Printf ("%x\n", BS)}/*The Run program computes the hash value and outputs it in a readable 16-in format. $ go Run SHA1-HASHES.GOSHA1 This stringcf23df2207d99a74fbe169e3eba035e633b65d94 you can use the similar method above to calculate other forms of hash values. For example, calculate MD5 hashes, introduce CRYPTO/MD5, and use MD5. New () method. Note that if you need a secure hash of cryptography, you need to look carefully at the hash strength. */

Related Article

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.