Documentation ¶
Overview ¶
Package sha256 provides functions to export the middle state of internal sha256.digest and set the state. It also provides functions to serialize and deserialize between text and sha256.digest.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrDigestType = errors.New("digest must be type of *sha256.digest")
ErrDigestType hash.Hash has many implementation types, but, here, we only reflect sha256.digest type.
Functions ¶
func GetHashStateText ¶
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 := sha256.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: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 MP+BAwEBBVN0YXRlAf+CAAEEAQFIAf+EAAEBWAH/hgABAk54AQQAAQNMZW4BBgAAABn/gwEBAQlbOF11aW50MzIB/4QAAQYBEAAAGv+FAQEBCVs2NF11aW50OAH/hgABBgH/gAAAc/+CAQj8agnmZ/y7Z66F/Dxu83L8pU/1OvxRDlJ//JsFaIz8H4PZq/xb4M0ZAUBoZWxsbyB3b3JsZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARYBCwA=
func NewHashWithStateText ¶
NewHashWithStateText is a helper method, directly generate *sha256.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+BAwEBBXN0YXRlAf+CAAEEAQFIAf+EAAEBWAH/hgABAk54AQQAAQNMZW4BBgAAABn/gwEBAQlbOF11aW50MzIB/4QAAQYBEAAAGv+FAQEBCVs2NF11aW50OAH/hgABBgH/gAAAc/+CAQj8agnmZ/y7Z66F/Dxu83L8pU/1OvxRDlJ//JsFaIz8H4PZq/xb4M0ZAUBzb21ldGhpbmcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARIBCQA=" hash, _ := NewHashWithStateText(stateText) fmt.Println(hex.EncodeToString(hash.Sum(nil)))
Output: 3fc9b689459d738f8c88a3a48aa9e33542016b7a4052e001aaa536fca74813cb
Types ¶
type State ¶
type State struct { // H is corresponding to sha256.digest.h H [8]uint32 // X is corresponding to sha256.digest.x X [64]byte // Nx is corresponding to sha256.digest.nx Nx int // Len is corresponding to sha256.digest.len Len uint64 }
State is a representation of *sha256.digest
func DecodeStringToState ¶
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 ¶
GetHashState will return sha256.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 ¶
EncodeToString encodes state to string by base64 encode. If there are anything wrong, it will return an error to represent it.