本文整理匯總了Golang中crypto/sha256.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestChunker
func TestChunker(t *testing.T) {
// setup data source
buf := getRandom(23, 32*1024*1024)
ch := chunker.New(bytes.NewReader(buf), testPol, sha256.New())
chunks := testWithData(t, ch, chunks1)
// test reader
for i, c := range chunks {
rd := c.Reader(bytes.NewReader(buf))
h := sha256.New()
n, err := io.Copy(h, rd)
if err != nil {
t.Fatalf("io.Copy(): %v", err)
}
if uint(n) != chunks1[i].Length {
t.Fatalf("reader returned wrong number of bytes: expected %d, got %d",
chunks1[i].Length, n)
}
d := h.Sum(nil)
if !bytes.Equal(d, chunks1[i].Digest) {
t.Fatalf("wrong hash returned: expected %02x, got %02x",
chunks1[i].Digest, d)
}
}
// setup nullbyte data source
buf = bytes.Repeat([]byte{0}, len(chunks2)*chunker.MinSize)
ch = chunker.New(bytes.NewReader(buf), testPol, sha256.New())
testWithData(t, ch, chunks2)
}
示例2: keyList
func keyList(ks *store.KeyStore, cfg *config) error {
updated := time.Unix(ks.Timestamp, 0).Format(timeFormat)
fmt.Println("Key store was last updated", updated)
fmt.Printf("%d keys stored\n", len(ks.Keys))
fmt.Println("Owner public key:")
h := sha256.New()
h.Write(ks.PublicKey)
fmt.Printf("\tFingerprint: %x\n", h.Sum(nil))
if len(ks.Keys) > 0 {
fmt.Println("Key store:")
for k, v := range ks.Keys {
fmt.Printf("\t%s\n", k)
ut := time.Unix(v.Timestamp, 0)
st := time.Unix(v.SignatureTime, 0)
signer, ok := ks.FindPublic(v.KeySigner)
if !ok {
signer = "<unknown>"
}
h = sha256.New()
h.Write(v.Keys)
fmt.Printf("\t\tLast update: %s\n", ut.Format(timeFormat))
fmt.Printf("\t\t Signed at: %s\n", st.Format(timeFormat))
fmt.Printf("\t\t Signed by: %s\n", signer)
fmt.Printf("\t\tFingerprint: %x\n", h.Sum(nil))
}
}
return nil
}
示例3: Serialize
func (ek *ExtendedPublicKey) Serialize() {
serialized := make([]byte, 78)
//4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private; testnet: 0x043587CF public, 0x04358394 private)
copy(serialized[0:], ek.Version)
//1 byte: depth: 0x00 for master nodes, 0x01 for level-1 derived keys, ....
serialized[4] = ek.Depth
//4 bytes: the fingerprint of the parent's key (0x00000000 if master key)
copy(serialized[5:], ek.ParentFingerPrint)
//4 bytes: child number. This is ser32(i) for i in xi = xpar/i, with xi the key being serialized. (0x00000000 if master key)
binary.BigEndian.PutUint32(serialized[9:], ek.ChildNumber)
//32 bytes: the chain code
copy(serialized[13:], ek.Chaincode)
//33 bytes: serP(K) for public keys
copy(serialized[45:], ek.PublicKey)
//add 32 checksum bits (derived from the double SHA-256 checksum)
firstSha256 := sha256.New()
firstSha256.Write(serialized)
one := firstSha256.Sum(nil)
secondSha256 := sha256.New()
secondSha256.Write(one)
checksum := secondSha256.Sum(nil)[:4]
serializedChecksum := make([]byte, 82)
copy(serializedChecksum[0:], serialized)
copy(serializedChecksum[78:], checksum)
//convert to the Base58 representation
buf := base58.Encode(serializedChecksum)
fmt.Println(buf)
}
示例4: dblSha256
func dblSha256(data []byte) []byte {
sha1 := sha256.New()
sha2 := sha256.New()
sha1.Write(data)
sha2.Write(sha1.Sum(nil))
return sha2.Sum(nil)
}
示例5: GenerateKeyData
func (k *keyDataGeneratorContents) GenerateKeyData(stream io.ReadCloser, keyData []byte) (
equalStream io.ReadCloser, err error) {
// Ensure the stream is closed once we're out of here
defer stream.Close()
if len(keyData) > sha256.Size {
// This should not happen when called from within this package. We
// don't use ciphers that need more than 32 bytes of key data.
return nil, errKeyDataToLarge
}
// We need a temporary buffer to save the contents of the stream
tempBuffer, err := newSecureTempBuffer()
if err != nil {
return nil, err
}
defer tempBuffer.Close()
// Store in temp buffer and calculate sha in the meantime
hasher := sha256.New()
_, err = io.Copy(tempBuffer, io.TeeReader(stream, hasher))
if err != nil {
return nil, err
}
hash := hasher.Sum(nil)
// Use hash to fill the keyData array
copy(keyData, hash)
// Ensure we read the same contents by requiring same hash on the data
return newReaderHashValidator(tempBuffer.Reader(), sha256.New(), hash), nil
}
示例6: NewSignature
// NewSignature generates a ECDSA signature given the raw transaction and privateKey to sign with
func NewSignature(rawTransaction []byte, privateKey []byte) ([]byte, error) {
//Start secp256k1
secp256k1.Start()
var privateKey32 [32]byte
for i := 0; i < 32; i++ {
privateKey32[i] = privateKey[i]
}
//Get the raw public key
publicKey, success := secp256k1.Pubkey_create(privateKey32, false)
if !success {
return nil, errors.New("Failed to create public key from provided private key.")
}
//Hash the raw transaction twice with SHA256 before the signing
shaHash := sha256.New()
shaHash.Write(rawTransaction)
var hash []byte = shaHash.Sum(nil)
shaHash2 := sha256.New()
shaHash2.Write(hash)
rawTransactionHashed := shaHash2.Sum(nil)
//Sign the raw transaction
signedTransaction, success := secp256k1.Sign(rawTransactionHashed, privateKey32, newNonce())
if !success {
return nil, errors.New("Failed to sign transaction")
}
//Verify that it worked.
verified := secp256k1.Verify(rawTransactionHashed, signedTransaction, publicKey)
if !verified {
return nil, errors.New("Failed to verify signed transaction")
}
//Stop secp256k1 and return signature
secp256k1.Stop()
return signedTransaction, nil
}
示例7: TestGenerateKeys
func TestGenerateKeys(t *testing.T) {
var err error
if testSender, err = dhkam.GenerateKey(rand.Reader); err != nil {
fmt.Println(err.Error())
t.FailNow()
}
if testReceiver, err = dhkam.GenerateKey(rand.Reader); err != nil {
fmt.Println(err.Error())
t.FailNow()
}
testSenderKEK = testSender.InitializeKEK(rand.Reader,
&testReceiver.PublicKey, dhkam.KEKAES128CBCHMACSHA256, nil, sha256.New())
if testSenderKEK == nil {
fmt.Println(ErrInvalidKEKParams.Error())
t.FailNow()
}
testReceiverKEK = testReceiver.InitializeKEK(rand.Reader,
&testSender.PublicKey, dhkam.KEKAES128CBCHMACSHA256, nil, sha256.New())
if testReceiverKEK == nil {
fmt.Println(ErrInvalidKEKParams.Error())
t.FailNow()
}
if testmsg, err = ioutil.ReadFile("README"); err != nil {
fmt.Println(err.Error())
t.FailNow()
}
}
示例8: GenerateSin
func GenerateSin(pubkey []byte) string {
// FIRST STEP - COMPLETE
sha_256 := sha256.New()
sha_256.Write(pubkey)
// SECOND STEP - COMPLETE
rip := ripemd160.New()
rip.Write(sha_256.Sum(nil))
// THIRD STEP - COMPLETE
sin_version, _ := hex.DecodeString("0f02")
pubPrefixed := append(sin_version, rip.Sum(nil)...)
// FOURTH STEP - COMPLETE
sha2nd := sha256.New()
sha2nd.Write([]byte(pubPrefixed))
sha3rd := sha256.New()
sha3rd.Write([]byte(sha2nd.Sum(nil)))
// // FIFTH STEP - COMPLETE
checksum := sha3rd.Sum(nil)[0:4]
// SIXTH STEP - COMPLETE
pubWithChecksum := append(pubPrefixed, checksum...)
// SIN
sin := base58.Encode(pubWithChecksum)
return sin
}
示例9: main
func main() {
// read whole the file
b, err := ioutil.ReadFile("file")
if err != nil {
panic(err)
}
blocks := (len(b) / 1024)
if len(b)%1024 == 0 {
blocks--
}
firstBlock := true
currentHash := sha256.New()
for i := blocks; i >= 0; i-- {
startIndex := i * 1024
endIndex := startIndex + 1024
block := b[startIndex:endIndex]
if firstBlock {
block = b[startIndex:]
} else {
//append current hash to block
block = currentHash.Sum(block)
}
firstBlock = false
//hash current block
currentHash = sha256.New()
currentHash.Write(block)
}
resultingHash := hex.EncodeToString(currentHash.Sum(make([]byte, 0)))
fmt.Println(resultingHash)
}
示例10: main
func main() {
if os.Args[1] == "-h" {
toHash := os.Args[2]
hasher := sha256.New()
hasher.Write([]byte(toHash))
fmt.Println(base64.URLEncoding.EncodeToString(hasher.Sum(nil)))
return
} else if os.Args[1] == "-i" {
toHash := os.Args[3]
hasher := sha256.New()
hasher.Write([]byte(toHash))
p := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
u := os.Args[2]
h := os.Args[4]
if db.Exists(u) == 0 {
did := db.InsertUser(u, p, h, false)
fmt.Println(did)
} else {
fmt.Println("Failed due to existence/failure")
}
} else if os.Args[1] == "-f" {
db.Fix()
}
}
示例11: doubleSHA256
// DoubleSHA256 computes a double sha256 hash of the first 21 bytes of the
// address. This is the one function shared with the other bitcoin RC task.
// Returned is the full 32 byte sha256 hash. (The bitcoin checksum will be
// the first four bytes of the slice.)
func (a *A25) doubleSHA256() []byte {
h := sha256.New()
h.Write(a[:21])
d := h.Sum([]byte{})
h = sha256.New()
h.Write(d)
return h.Sum(d[:0])
}
示例12: NewTreeHash
// Creates a new tree hasher.
func NewTreeHash() *TreeHash {
var result TreeHash
result.whole = sha256.New()
result.part = sha256.New()
result.hashers = io.MultiWriter(result.whole, result.part)
result.nodes = make([]treeHashNode, 0)
return &result
}
示例13: DoubleSHA
//TODO: test and add to tests
//double SHA-256 hashing of a single byte array
func DoubleSHA(b []byte) []byte {
var h hash.Hash = sha256.New()
h.Write(b)
var h2 hash.Hash = sha256.New()
h2.Write(h.Sum(nil))
return h2.Sum(nil)
}
示例14: xzDecInit
/**
* xzDecInit - Allocate and initialize a XZ decoder state
* @dictMax: Maximum size of the LZMA2 dictionary (history buffer) for
* decoding. LZMA2 dictionary is always 2^n bytes
* or 2^n + 2^(n-1) bytes (the latter sizes are less common
* in practice), so other values for dictMax don't make sense.
*
* dictMax specifies the maximum allowed dictionary size that xzDecRun
* may allocate once it has parsed the dictionary size from the stream
* headers. This way excessive allocations can be avoided while still
* limiting the maximum memory usage to a sane value to prevent running the
* system out of memory when decompressing streams from untrusted sources.
*
* xzDecInit returns a pointer to an xzDec, which is ready to be used with
* xzDecRun.
*/
func xzDecInit(dictMax uint32) *xzDec {
s := new(xzDec)
s.lzma2 = xzDecLZMA2Create(dictMax)
s.block.hash.sha256 = sha256.New()
s.index.hash.sha256 = sha256.New()
xzDecReset(s)
return s
}
示例15: GitHookInstalled
// GitHookInstalled is used to check if a given hook is installed as
// specified, it does nothing more, Params:
// h (*GitHookMgr): the hook mgr structure (find location of repo/etc)
// path (string): where is the hook we wish to install?
// name (string): what is the "git name" for the hook?
// link (bool): is hook a symlink to hookPath, or full copy/install?
// Returns boolean, true if hook is installed as specified, false otherwise
func GitHookInstalled(h *GitHookMgr, path, name string, link bool) bool {
cachedPathHashes := make(map[string]string)
repoPath, _, err := h.Exists(LocalPath)
hookInstalled := false
hookInstallPath := ""
if err == nil && repoPath != "" { // if the local path exists...
hookInstallPath = filepath.Join(repoPath, ".git", "hooks", name)
if isBareRepo(repoPath) {
hookInstallPath = filepath.Join(repoPath, "hooks", name)
}
if link { // if client wants a link, see if link is there already...
fileInfo, err := os.Lstat(hookInstallPath)
if err != nil {
return false // if not there then installed is false
}
if fileInfo.Mode()&os.ModeSymlink == 0 {
return false // if not a symlink then installed is false
}
originFile, err := os.Readlink(hookInstallPath)
if err != nil {
return false // if cannot read link, installed is false
}
if originFile != path {
return false // target is not what we wanted, installed is false
}
hookInstalled = true
} else { // user wants copy of file, see if there and sha matches..
if there, err := file.Exists(hookInstallPath); err != nil || !there {
return false // err checking existence|not there, not installed
}
installed, err := ioutil.ReadFile(hookInstallPath)
hasher := sha256.New()
hasher.Write(installed)
installedFileHash := hex.EncodeToString(hasher.Sum(nil))
if err != nil {
return false // failed to read file, assume not installed
}
wantedFileHash := ""
if cachedHash, ok := cachedPathHashes[path]; ok {
wantedFileHash = cachedHash // only gen the hash of the target file once per pass
} else {
wanted, err2 := ioutil.ReadFile(path)
if err2 != nil {
return false // failed to read file, assume not installed
}
hasher = sha256.New()
hasher.Write(wanted)
wantedFileHash = hex.EncodeToString(hasher.Sum(nil))
}
if installedFileHash != wantedFileHash {
return false // sha's differ, assume rev we want not installed
}
hookInstalled = true
}
}
return hookInstalled
}