Skip to content

amazon-ion/ion-hash-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

** This package is considered beta. While the API is relatively stable it is still subject to change. **

Amazon Ion Hash Go

An implementation of Amazon Ion Hash in Go.

build license docs

Getting Started

You can start using ion-hash-go by simply importing it, e.g.,

import (
	ionhash "github.com/amzn/ion-hash-go"
)

Generating a hash while reading

// Create a hasher provider, using MD5
hasherProvider := ionhash.NewCryptoHasherProvider("MD5")

// Create an Ion reader over the input [1,2,3]
ionReader := ion.NewReaderString("[1,2,3]")

// Create a hash reader
hashReader, err := ionhash.NewHashReader(ionReader, hasherProvider)
if err != nil {
	panic(err)
}

// Read over the top level value and calculate its hash
hashReader.Next()
hashReader.Next()


// Get the hash value
res, err := hashReader.Sum(nil)
if err != nil {
	panic(err)
}

// Print out the hash in Hex
fmt.Printf("Digest = %x\n", res) // prints: Digest = 8f3bf4b1935cf469c9c10c31524b2625

Generating a hash while writing

// Create a hasher provider, using MD5
hasherProvider := ionhash.NewCryptoHasherProvider("MD5")

// Create an Ion writer
ionWriter := ion.NewTextWriter(new(bytes.Buffer))

// Create a hash writer
hashWriter, err := ionhash.NewHashWriter(ionWriter, hasherProvider)
if err != nil {
	panic(err)
}

// Write the list [1,2,3]
hashWriter.BeginList()
hashWriter.WriteInt(1)
hashWriter.WriteInt(2)
hashWriter.WriteInt(3)
hashWriter.EndList()

// Get the hash value
res, err := hashWriter.Sum(nil)
if err != nil {
	panic(err)
}

// Print out the hash in Hex
fmt.Printf("Digest = %x\n", res) // prints: Digest = 8f3bf4b1935cf469c9c10c31524b2625/

Development

This package uses Go Modules to model its dependencies.

Assuming the go command is in your path, building the module can be done as:

$ go build -v ./...

Running all the tests can be executed with:

$ go test -v ./...

We use goimports to format our imports and files in general. Running this before commit is advised:

$ goimports -w .

It is recommended that you hook this in your favorite IDE (Tools > File Watchers in Goland, for example).

Known Issues

< No current known issues >

License

This library is licensed under the Apache-2.0 License.