Go Lang Hashing Example

by Anish

Posted on Wednesday October 31 , 2018

This sample chapter extracted from the book, Go Lang Cryptography for Developers . The Book theme isCryptography is for EveryOne. Learn from Crypto Principle to Applied Cryptography With Practical Example


Hashing

A cryptographic hash function is a hash function which takes an input (or 'message') and returns a fixed-size alphanumeric string. The string is called the 'hash value', 'message digest', 'digital fingerprint', 'digest' or 'checksum'.

The ideal hash function has three main properties:

  1. It is extremely easy to calculate a hash for any given data.
  2. It is extremely computationally difficult to calculate an alphanumeric text that has a given hash.
  3. It is extremely unlikely that two slightly different messages will have the same hash.

Go provides built-in support for hash algorithms, supported Hashing Algorithms and their Digest Sizes

Hash Digest Size
MD4 16
MD5 16
SHA1 20
SHA224 28
SHA256 32
SHA384 48
SHA512 64
RIPEMD160 20
SHA3_224 28
SHA3_256 32
SHA3_384 48
SHA3_512 64
SHA512_224 28
SHA512_256 32
BLAKE2s_256 32
BLAKE2b_256 32
BLAKE2b_384 48
BLAKE2b_512 64

type Hash : Hash identifies a cryptographic hash function that is implemented in another package

        MD4                         // import crypto/md4
        MD5                         // import crypto/md5
        SHA1                        // import crypto/sha1
        SHA224                      // import crypto/sha256
        SHA256                      // import crypto/sha256
        SHA384                      // import crypto/sha512
        SHA512                      // import crypto/sha512
        MD5SHA1                     // no implementation; MD5+SHA1 used for TLS RSA
        RIPEMD160                   // import golang.org/x/crypto/ripemd160
        SHA3_224                    // import golang.org/x/crypto/sha3
        SHA3_256                    // import golang.org/x/crypto/sha3
        SHA3_384                    // import golang.org/x/crypto/sha3
        SHA3_512                    // import golang.org/x/crypto/sha3
        SHA512_224                  // import crypto/sha512
        SHA512_256                  // import crypto/sha512
        BLAKE2s_256                 // import golang.org/x/crypto/blake2s
        BLAKE2b_256                 // import golang.org/x/crypto/blake2b
        BLAKE2b_384                 // import golang.org/x/crypto/blake2b
        BLAKE2b_512                 // import golang.org/x/crypto/blake2b

SHA-1 Hashing Example

package main
import "crypto/sha1"
import "fmt"
func main() {
    s := "Hello 8gwifi.org"
    // The pattern for generating a hash is `sha1.New()`,
    // `sha1.Write(bytes)`, then `sha1.Sum([]byte{})`.
    // Here we start with a new hash.
    h := sha1.New()
    
    // `Write` expects bytes. If you have a string `s`,
    // use `[]byte(s)` to coerce it to bytes.
    h.Write([]byte(s))

    // This gets the finalized hash result as a byte
    // slice. The argument to `Sum` can be used to append
    // to an existing byte slice: it usually isn't needed.
    bs := h.Sum(nil)

    // SHA1 values are often printed in hex, for example
    // in git commits. Use the `%x` format verb to convert
    // a hash results to a hex string.
    fmt.Println(s)
    fmt.Printf("%x\n", bs)
}

The output

Hello 8gwifi.org
5523bdf845330363e738519c87f5f0dafe72c051

MD5 Hashing Example

package main
import "crypto/md5"
import "fmt"
func main() {
    s := "Hello 8gwifi.org"
    // The pattern for generating a hash is `sha1.New()`,
    // `sha1.Write(bytes)`, then `sha1.Sum([]byte{})`.
    // Here we start with a new hash.
    h := md5.New()
    
    // `Write` expects bytes. If you have a string `s`,
    // use `[]byte(s)` to coerce it to bytes.
    h.Write([]byte(s))

    // This gets the finalized hash result as a byte
    // slice. The argument to `Sum` can be used to append
    // to an existing byte slice: it usually isn't needed.
    bs := h.Sum(nil)

    // MD5 values are often printed in hex, for example
    // in git commits. Use the `%x` format verb to convert
    // a hash results to a hex string.
    fmt.Println(s)
    fmt.Printf("%x\n", bs)
}

The output

Hello 8gwifi.org
5d567716bc513834a40ecf179fcab1ba

Thanku for reading !!! Give a Share for Support


Your Support Matters!

Instead of directly asking for donations, I'm thrilled to offer you all nine of my books for just $9 on leanpub By grabbing this bundle you not only help cover my coffee, beer, and Amazon bills but also play a crucial role in advancing and refining this project. Your contribution is indispensable, and I'm genuinely grateful for your involvement in this journey!

Any private key value that you enter or we generate is not stored on this site, this tool is provided via an HTTPS URL to ensure that private keys cannot be stolen, for extra security run this software on your network, no cloud dependency




python Cryptography Topics
Topics
For Coffee/ Beer/ Amazon Bill and further development of the project Support by Purchasing, The Modern Cryptography CookBook for Just $9 Coupon Price

Kubernetes for DevOps

Hello Dockerfile

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers

Here