本文整理汇总了Golang中crypto/sha256.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
}
开发者ID:rawtaz,项目名称:restic,代码行数:34,代码来源:chunker_test.go
示例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
}
开发者ID:kisom,项目名称:cryptutils,代码行数:28,代码来源:fcrypt.go
示例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)
}
开发者ID:21JD21,项目名称:Bitcoin-HD-Wallet-Key-Derivation,代码行数:29,代码来源:HDWallet.go
示例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)
}
开发者ID:n1rvana,项目名称:go-hdwallet,代码行数:7,代码来源:util.go
示例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
}
开发者ID:cinode,项目名称:go,代码行数:33,代码来源:keygenerator_contents.go
示例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
}
开发者ID:philsong,项目名称:go-bitcoin-multisig,代码行数:34,代码来源:btcutils.go
示例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()
}
}
开发者ID:jonathanmarvens,项目名称:gocrypto,代码行数:31,代码来源:hybrid_test.go
示例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
}
开发者ID:gildo,项目名称:bitcoin-identity,代码行数:32,代码来源:btc-identity.go
示例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)
}
开发者ID:Bochenski,项目名称:mashups,代码行数:30,代码来源:SHA.go
示例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()
}
}
开发者ID:daycationapp,项目名称:infrastructure,代码行数:25,代码来源:tool.go
示例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])
}
开发者ID:FuzzyHobbit,项目名称:btcplex,代码行数:12,代码来源:addresscheck.go
示例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
}
开发者ID:kljensen,项目名称:aws,代码行数:9,代码来源:treehash.go
示例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)
}
开发者ID:ThePiachu,项目名称:Split-Vanity-Miner-Golang,代码行数:10,代码来源:hashing.go
示例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
}
开发者ID:cardi,项目名称:timefind,代码行数:24,代码来源:dec_stream.go
示例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
}
开发者ID:dvln,项目名称:vcs,代码行数:64,代码来源:git.go
示例16: Fountain
// Fountain takes an io.Reader from a source file and implements a Luby
// Transform fountain code encoding algorithm over the file. It uses degree
// of distribution, chunk size, and a percent of chunks to generate into
// blocks.
func Fountain(src io.Reader, dist int, size int, perc float32, signature string) []Block {
// Still requires signature for signing blocks
// Convert io.Reader to string
buf := new(bytes.Buffer)
buf.ReadFrom(src)
s := buf.String()
// Setting hash seed
id := s
// Chunking source string
chunks := make([]string, len(s)/size)
for i := 0; i < len(s)/size; i++ {
chunks[i] = s[i*size : (i*size)+size]
}
// Padding last chunk
remainder := s[(len(s)-1)-(len(s)%size):]
remainder = remainder + string([]byte{1})
for (size - len(remainder)) > 0 {
remainder = remainder + string([]byte{0})
}
chunks = append(chunks, remainder)
// Combining chunks into blocks
blocks := []Block{}
for i := 0; i < int(float32(len(chunks))*perc); i++ {
// Generate Id hash
h := sha256.New()
io.WriteString(h, id)
id = string(h.Sum(nil))
// Randomly selects first block element
index := random(0, len(chunks)-1)
parents := []string{string(index)}
data := chunks[index]
// Randomly selects subsequent elements
for j := 0; j < random(0, dist-1); j++ {
index := random(0, len(chunks)-1)
parents = append(parents, string(index))
data = xor(data, chunks[index])
}
// Hash content and prepend hash to content
h = sha256.New()
io.WriteString(h, data+signature)
shash := string(h.Sum(nil))
// Append block to resulting []Block
blocks = append(blocks, Block{id, parents, data, shash})
}
return blocks
}
开发者ID:Hermes,项目名称:hermes,代码行数:61,代码来源:lt.go
示例17: Hash
// Hash takes a salt and provided string and returns their corresponding
// combined sha256 string
func Hash(salt, provided string) string {
hasher := sha256.New()
hasher.Write([]byte(provided))
first := hex.EncodeToString(hasher.Sum(nil))
hasher = sha256.New()
hasher.Write(append([]byte(first), salt...))
return hex.EncodeToString(hasher.Sum(nil))
}
开发者ID:Tiger66639,项目名称:gohst,代码行数:12,代码来源:auth.go
示例18: 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, header *Header) *xzDec {
s := new(xzDec)
s.crc32 = crc32.NewIEEE()
s.Header = header
s.block.hash.sha256 = sha256.New()
s.index.hash.sha256 = sha256.New()
s.lzma2 = xzDecLZMA2Create(dictMax)
xzDecReset(s)
return s
}
开发者ID:kinvolk,项目名称:acbuild,代码行数:26,代码来源:dec_stream.go
示例19: main
func main() {
flag.Parse()
// Read the input file
in, err := ioutil.ReadFile(*inFile)
if err != nil {
log.Fatalf("input file: %s", err)
}
fmt.Println(len(in), in)
// Read the private key
pemData, err := ioutil.ReadFile(*keyFile)
if err != nil {
log.Fatalf("read key file: %s", err)
}
// Extract the PEM-encoded data block
block, _ := pem.Decode(pemData)
if block == nil {
log.Fatalf("bad key data: %s", "not PEM-encoded")
}
if got, want := block.Type, "RSA PRIVATE KEY"; got != want {
log.Fatalf("unknown key type %q, want %q", got, want)
}
// Decode the RSA private key
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
log.Fatalf("bad private key: %s", err)
}
label := []byte("orders")
var out []byte
if *decrypt {
// Decrypt the data
out, err = rsa.DecryptOAEP(sha256.New(), rand.Reader, priv, in, label)
if err != nil {
log.Fatalf("decrypt: %s", err)
}
} else {
out, err = rsa.EncryptOAEP(sha256.New(), rand.Reader, &priv.PublicKey, in, label)
if err != nil {
log.Fatalf("encrypt: %s", err)
}
//fmt.Println(priv.PublicKey)
//fmt.Println(priv)
}
// Write data to output file
if err := ioutil.WriteFile(*outFile, out, 0600); err != nil {
log.Fatalf("write output: %s", err)
}
}
开发者ID:wujunjian,项目名称:go,代码行数:53,代码来源:rsaexample.go
示例20: GenHash
// GenHash generate SHA-256 hash for given password and salt
func GenHash(password, salt string) string {
hasher := sha256.New()
hasher.Write([]byte(password + salt))
prehash := fmt.Sprintf("%064x", hasher.Sum(nil))
hasher2 := sha256.New()
hasher2.Write([]byte(salt + prehash))
return fmt.Sprintf("%x", hasher.Sum(nil))
}
开发者ID:essentialkaos,项目名称:ek,代码行数:13,代码来源:passwd.go
注:本文中的crypto/sha256.New函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论