Golang, Python MD5, SHA512, base64 encoding, etc.

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


It is more convenient to handle it in go.


Func main() {

    fmt.Println(md5Str("woGo"))
    fmt.Println(sha512Str("woGo"))
    fmt.Println(base64DecodeStr(base64EncodeStr("fd")))
}

//md5 verification
Func md5Str(src string) string {

    h := md5.New()
    h.Write([]byte(src)) // The string to be encrypted is
    //fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // Output the encrypted result
    Return hex.EncodeToString(h.Sum(nil))
}

//sha512 verification
Func sha512Str(src string) string {
    h := sha512.New()
    h.Write([]byte(src)) // The string to be encrypted is
    //fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // Output the encrypted result
    Return hex.EncodeToString(h.Sum(nil))
}

//base encoding
Func base64EncodeStr(src string) string {
    Return string(base64.StdEncoding.EncodeToString([]byte(src)))
}

//base decoding
Func base64DecodeStr(src string) string {
    a, err := (base64.StdEncoding.DecodeString(src))
    If err != nil {
       Return "error"
    }
    Return string(a)
}


And in Python, that's just a few lines of code.


Import hashlib, base64
Src =b"woGo"

m= hashlib.md5()
M.update(src)
Print (m.hexdigest())

Print(hashlib.sha512(src).hexdigest())

In fact, whether it is GO language or Python, when decoding, you need to add an exception.
Print(base64.b64encode(b"fd"))
Print(base64.b64decode(base64.b64encode(b"fd")); 


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.