sha512

package
v1.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package sha512 provides functions to export the middle state of internal sha512.digest and set the state. It also provides functions to serialize and deserialize between text and sha512.digest.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrDigestType = errors.New("digest must be type of *sha512.digest")

ErrDigestType hash.Hash has many implementation types, but, here, we only reflect sha512.digest type.

Functions

func GetHashStateText

func GetHashStateText(digest hash.Hash) (string, error)

GetHashStateText is also a helper method, it will return text representation of hash or unexpected error.

Example
// When you need to save the middle state of hash to use sometime in
// the future, just call GetHashStateText() function simply.
// When you need this hash with the state, you can call NewHashWithState()
// function, see the next example. It seems like serialize and deserialize.

hash := sha512.New()
if _, err := hash.Write([]byte("hello world")); err != nil {
	// you should handle the unexpected error
	return
}
fmt.Println(hex.EncodeToString(hash.Sum(nil)))
hashStateText, err := GetHashStateText(hash)
if err == nil {
	fmt.Println(hashStateText)
}
Output:

309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
MP+BAwEBBVN0YXRlAf+CAAEEAQFIAf+EAAEBWAH/hgABAk54AQQAAQNMZW4BBgAAABn/gwEBAQlbOF11aW50NjQB/4QAAQYBEAAAHP+FAQEBClsxMjhddWludDgB/4YAAQYB/gEAAAD/1P+CAQj4agnmZ/O8yQj4u2euhYTKpzv4PG7zcv6U+Cv4pU/1Ol8dNvH4UQ5Sf63mgtH4mwVojCs+bB/4H4PZq/tBvWv4W+DNGRN+IXkB/4BoZWxsbyB3b3JsZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEWAQsA

func NewHashWithStateText

func NewHashWithStateText(stateCipherText string) (digest hash.Hash, err error)

NewHashWithStateText is a helper method, directly generate *sha512.digest by stateCipherText. Maybe, it will raise an error, return value err will represent it. If successfully, digest returned will be available.

Example
// When you saved the middle state of hash, you can recover it at
// any time.

stateText := "MP+BAwEBBVN0YXRlAf+CAAEEAQFIAf+EAAEBWAH/hgABAk54AQQAAQNMZW4BBgAAABn/gwEBAQlbOF11aW50NjQB/4QAAQYBEAAAHP+FAQEBClsxMjhddWludDgB/4YAAQYB/gEAAAD/1P+CAQj4agnmZ/O8yQj4u2euhYTKpzv4PG7zcv6U+Cv4pU/1Ol8dNvH4UQ5Sf63mgtH4mwVojCs+bB/4H4PZq/tBvWv4W+DNGRN+IXkB/4BoZWxsbyB3b3JsZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEWAQsA"
hash, _ := NewHashWithStateText(stateText)
fmt.Println(hex.EncodeToString(hash.Sum(nil)))
Output:

309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f

func SetHashState

func SetHashState(digest hash.Hash, state *State) error

SetHashState will be used to set sha512.digest state. This method will help us implement continuous hash.

Types

type State

type State struct {

	// H is corresponding to sha512.digest.h
	H [8]uint64

	// X is corresponding to sha512.digest.x
	X [128]byte

	// Nx is corresponding to sha512.digest.nx
	Nx int

	// Len is corresponding to sha512.digest.len
	Len uint64
}

State is a representation of *sha512.digest

func DecodeStringToState

func DecodeStringToState(cipherText string) (*State, error)

DecodeStringToState decodes string that is encoded by base64, and then decodes it to a *state. When something goes wrong, it will return an error, otherwise err is nil.

func GetHashState

func GetHashState(digest hash.Hash) (*State, error)

GetHashState will return sha512.digest internal state. This is an unsafe method, so you should use it with caution. If reflect successfully, it will return a *State.

func (*State) EncodeToString

func (s *State) EncodeToString() (string, error)

EncodeToString encodes state to string by base64 encode. If there are anything wrong, it will return an error to represent it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL