Go language Note: use of BASE64/SHA1

Source: Internet
Author: User
Tags cryptographically secure md5 hash 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 a bit of space.
import b64 "encoding / base64"
import "fmt"

func main () {
    // This is the string to be encoded / decoded.
    data: = "abc123!? $ * & () '-= @ ~"
    // Go also supports standard and URL-compatible base64 formats. Encoding requires a parameter of type [] byte, so convert a string to this type.
    sEnc: = b64.StdEncoding.EncodeToString ([] byte (data))
    fmt.Println (sEnc)
    // Decoding may return an error. If you are not sure whether the input information format is correct, then you need to perform error checking.
    sDec, _: = b64.StdEncoding.DecodeString (sEnc)
    fmt.Println (string (sDec))
    fmt.Println ()
    // Use URL-compatible base64 format for encoding and decoding.
    uEnc: = b64.URLEncoding.EncodeToString ([] byte (data))
    fmt.Println (uEnc)
    uDec, _: = b64.URLEncoding.DecodeString (uEnc)
    fmt.Println (string (uDec))
    // The standard base64-encoded and URL-compatible base64-encoded strings are slightly different (the suffixes are + and-), but both can be correctly decoded into the original string.
}

/ *
The results are as follows:
$ go run base64-encoding.go
YWJjMTIzIT8kKiYoKSctPUB +
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 generate a hash is worth sha1.New (), sha1.Write (bytes), and then sha1.Sum ([] byte {}). Here we start with a new hash.
    h: = sha1.New ()
    // Write the bytes to be processed. If it is a string, you need to use [] byte (s) to cast it to a byte array.
    h.Write ([] byte (s))
    // This is the character slice used to get the final hash value. Sum parameters can be used to append additional byte slices to existing character slices: generally not needed.
    bs: = h.Sum (nil)
    // SHA1 values are often output in hexadecimal, as in git commit. Use% x to format the hash result as a hexadecimal string.
    fmt.Println (s)
    fmt.Printf ("% x \ n", bs)
}

/ *
The running program calculates the hash value and outputs it in a readable hexadecimal format.
$ go run sha1-hashes.go
sha1 this string
cf23df2207d99a74fbe169e3eba035e633b65d94
You can use other similar methods to calculate other forms of hash values. For example, calculate the MD5 hash, introduce crypto / md5 and use the md5.New () method.
Note that if you need cryptographically secure hashing, you need to study the hash strength carefully.
* / 
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.