↓ Skip to main content
Security

GoLang: Hash

·2 mins
Table of Contents

Previously I talked about files, encoding in golang. In this LTD (learn to do) I will talk about hash package in general.

https://blog.samarthya.me/wps/2021/01/30/golang-encoding/

Help
#

Package crypto collects common cryptographic constants. — Official golang documentation
const (
    MD4         Hash = 1 + iota // import golang.org/x/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

)

Let’s try out a simple MD5 hasher.

Package md5 implements the MD5 hash algorithm as defined in RFC 1321 — packagemd5
fmt.Printf(" Is BLAKE2b_256 available: %t\n", crypto.BLAKE2b_256.Available())
fmt.Printf(" Is MD5 available: %t\n", crypto.MD5.Available())

var hasher hash.Hash
// Creates a new MD5 hasher
hasher = md5.New()

io.WriteString(hasher, "And Leon's getting laaarger!")
fmt.Printf("%x\n", hasher.Sum(nil))

New()
#

New returns a new hash.Hash computing the MD5 checksum.

Sum()
#

Sum returns the MD5 checksum of the data.

fmt.Printf(" Is MD5 available: %t\n", crypto.MD5.Available())
	fmt.Printf(" Is SHA1 available: %t\n", crypto.SHA1.Available())
	fmt.Printf(" Is SHA256 available: %t\n", crypto.SHA256.Available())
var hasher hash.Hash

// Creates a new MD5 hasher
hasher = md5.New()

// Version 1
io.WriteString(hasher, "And Leon's getting laaarger!")
fmt.Printf("%x\n", hasher.Sum(nil))
fmt.Printf("Size: %d\n", hasher.Size())

// Version 2
var data = []byte("Data for which the md5 hash will be computed")

fmt.Printf("%x\n", hasher.Sum(data))
fmt.Printf("Size: %d\n", hasher.Size())

sha1 := sha1.New()
fmt.Printf("%x\n", sha1.Sum(nil))
fmt.Printf("Size: %d\n", sha1.Size())

sha256 := sha256.New()
fmt.Printf("%x\n", sha256.Sum(nil))
fmt.Printf("Size: %d\n", sha256.Size())
Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

GoLang: encoding

Package encoding provide capabilities to convert data to and from byte-level and textual representations. encoding/gob encoding/json, and encoding/xml The gob is Go’s format for serializing and deserializing. The gob was tailored for communicating between two servers written in Go. Only exported fields are encoded; zero values are not taken into account Package gob manages streams of gobs – binary values exchanged between an Encoder (transmitter) and a Decoder (receiver). Format GoB # Let’s create a simple Type Blog and we will encode and decode it using the gob format.

GoLang: Hex Output

·3 mins
In last few blogs I have written about reading commandline parameters reading files and using the os package. In this blog, I will combine some these items to read a file byte by byte using the bufio package func’s.

GoLang: Program Banner

·2 mins
In the last blog I talked about command line arguments os.Args[]. In this I will try and create a sample banner program that shall Take 3 arguments n - Number of times a message has to be repeated. m - The message that needs to be shown. d - If debug message needs to be shown. I am going to use the flags package.