本文整理汇总了Golang中github.com/skycoin/skycoin/src/cipher.SumSHA256函数的典型用法代码示例。如果您正苦于以下问题:Golang SumSHA256函数的具体用法?Golang SumSHA256怎么用?Golang SumSHA256使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SumSHA256函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestBlockchainTail_03
func TestBlockchainTail_03(t *testing.T) {
bq := BlockchainTail{}
bq.Init()
h1 := cipher.SumSHA256(secp256k1.RandByte(888))
b1 := BlockBase{Hash: h1, Seqno: 1} // OK to leave '.sig' empty
r1 := bq.try_append_to_BlockchainTail(&b1)
if r1 != 0 {
t.Log("BlockchainTail::try_append_to_BlockchainTail(): initial insert failed.")
t.Fail()
}
if bq.GetNextSeqNo() != b1.Seqno+1 {
t.Log("BlockchainTail::GetNextSeqNo() failed.")
t.Fail()
}
r1dup := bq.try_append_to_BlockchainTail(&b1)
if r1dup != 1 {
t.Log("BlockchainTail::try_append_to_BlockchainTail(): duplicate hash not detected.")
t.Fail()
}
h2 := cipher.SumSHA256(secp256k1.RandByte(888))
b2 := BlockBase{Hash: h2, Seqno: 2} // OK to leave '.sig' empty
r2 := bq.try_append_to_BlockchainTail(&b2)
if r2 != 0 {
t.Log("BlockchainTail::try_append_to_BlockchainTail(): next insert failed.")
t.Fail()
}
if bq.GetNextSeqNo() != b2.Seqno+1 {
t.Log("BlockchainTail::GetNextSeqNo() failed.")
t.Fail()
}
h3 := cipher.SumSHA256(secp256k1.RandByte(888))
b3 := BlockBase{Hash: h3, Seqno: 0} // OK to leave '.sig' empty
r3 := bq.try_append_to_BlockchainTail(&b3)
if r3 != 2 {
t.Log("BlockchainTail::try_append_to_BlockchainTail(): low seqno not detected. ret=", r3)
t.Fail()
}
b3.Seqno = 4
r4 := bq.try_append_to_BlockchainTail(&b3)
if r4 != 3 {
t.Log("BlockchainTail::try_append_to_BlockchainTail(): high seqno not detected.")
t.Fail()
}
}
示例2: ApplyBlock
//applies block against the current head
func (bc *BlockChain) ApplyBlock(block Block) error {
//do time check
//do prevhash check
//check body hash
//check BkSeq
if block.Head.BkSeq != bc.Head().Head.BkSeq+1 {
return errors.New("block sequence is out of order")
}
if block.Head.PrevHash != bc.Head().Head.Hash() {
return errors.New("block PrevHash does not match current head")
}
if block.Head.Time < bc.Head().Head.Time {
return errors.New("block time invalid")
}
if block.Head.BodyHash != cipher.SumSHA256(block.Body) {
return errors.New("block body hash is wrong")
}
if err := bc.VerifyBlockSignature(block); err != nil {
return errors.New("block signature check failed")
}
//block is valid, apply
bc.Blocks = append(bc.Blocks, block)
return nil
}
示例3: TestCrypto2
//test signatures
func TestCrypto2(t *testing.T) {
a := "5a42c0643bdb465d90bf673b99c14f5fa02db71513249d904573d2b8b63d353d"
b, err := hex.DecodeString(a)
if err != nil {
t.Fatal(err)
}
if len(b) != 32 {
t.Fatal()
}
seckey := cipher.NewSecKey(b)
pubkey := cipher.PubKeyFromSecKey(seckey)
addr := cipher.AddressFromPubKey(pubkey)
_ = addr
test := []byte("test message")
hash := cipher.SumSHA256(test)
err = cipher.TestSecKeyHash(seckey, hash)
if err != nil {
t.Fatal()
}
}
示例4: TestBlockchainTail_02
func TestBlockchainTail_02(t *testing.T) {
bq := BlockchainTail{}
bq.Init()
// Use more than configured length to ensure some elements are
// removed:
n := Cfg_blockchain_tail_length * 2
for i := 0; i < n; i++ {
x := secp256k1.RandByte(888) // Random data.
h := cipher.SumSHA256(x) // Its hash.
b := BlockBase{Hash: h, Seqno: uint64(i)} // OK to leave '.sig' empty
bq.append_nocheck(&b)
}
if len(bq.blockPtr_slice) != Cfg_blockchain_tail_length {
t.Log("BlockchainTail::append_nocheck() incorrect append or remove.")
t.Fail()
}
if !bq.is_consistent() {
t.Log("BlockchainTail::is_consistent()")
t.Fail()
}
}
示例5: TestBlockStat_01
func TestBlockStat_01(t *testing.T) {
bs := BlockStat{}
bs.Init()
_, seckey := cipher.GenerateKeyPair()
hash := cipher.SumSHA256(secp256k1.RandByte(888))
sig := cipher.SignHash(hash, seckey)
var r int = -1
r = bs.try_add_hash_and_sig(hash, cipher.Sig{})
if r != 4 {
t.Log("BlockStat::try_add_hash_and_sig() failed to detect invalid signature.")
t.Fail()
}
r = bs.try_add_hash_and_sig(cipher.SHA256{}, sig)
if r != 4 {
t.Log("BlockStat::try_add_hash_and_sig() failed to detect invalid hash and signature.")
t.Fail()
}
r = bs.try_add_hash_and_sig(cipher.SHA256{}, cipher.Sig{})
if r != 4 {
t.Log("BlockStat::try_add_hash_and_sig() failed to detect invalid hash and signature.")
t.Fail()
}
//signer_pubkey, err := cipher.PubKeyFromSig(cipher.Sig{}, cipher.SHA256{})
//if err != nil {
//fmt.Printf("Got pubkey='%s' from all-zero sig and all-zero hash.\n", signer_pubkey.Hex())
//}
bs.frozen = true
r2 := bs.try_add_hash_and_sig(hash, sig)
if r2 != 3 {
t.Log("BlockStat::try_add_hash_and_sig() failed to detect frozen.")
t.Fail()
}
bs.frozen = false
r3 := bs.try_add_hash_and_sig(hash, sig)
if r3 != 0 {
t.Log("BlockStat::try_add_hash_and_sig() failed to add.")
t.Fail()
}
sig2 := cipher.SignHash(hash, seckey) // Redo signing.
r4 := bs.try_add_hash_and_sig(hash, sig2)
if r4 != 1 {
t.Log("BlockStat::try_add_hash_and_sig() failed to detect duplicate (hash,pubkey).")
t.Fail()
}
r5 := bs.try_add_hash_and_sig(hash, sig)
if r5 != 1 {
t.Log("BlockStat::try_add_hash_and_sig() failed to detect duplicate (hash,sig).")
t.Fail()
}
}
示例6: makeUxBodyWithSecret
func makeUxBodyWithSecret(t *testing.T) (coin.UxBody, cipher.SecKey) {
p, s := cipher.GenerateKeyPair()
return coin.UxBody{
SrcTransaction: cipher.SumSHA256(randBytes(t, 128)),
Address: cipher.AddressFromPubKey(p),
Coins: 10e6,
Hours: 100,
}, s
}
示例7: NewEmptySimpleWallet
func NewEmptySimpleWallet() Wallet {
idHash := cipher.SumSHA256(secp256k1.RandByte(256))
id := WalletID(hex.EncodeToString(idHash[:16]))
return &SimpleWallet{
Filename: NewWalletFilename(id),
Entries: WalletEntries{},
ID: id,
}
}
示例8: NewBlock
//creates new block
func (bc *BlockChain) NewBlock(seckey cipher.SecKey, blockTime uint64, data []byte) Block {
var b Block
b.Head.Time = blockTime
b.Head.BkSeq = bc.Head().Head.BkSeq + 1
b.Head.PrevHash = bc.Head().Head.Hash()
b.Head.BodyHash = cipher.SumSHA256(data)
b.Body = data
bc.SignBlock(seckey, &b)
return b
}
示例9: createUnconfirmedTxn
func createUnconfirmedTxn() visor.UnconfirmedTxn {
now := util.Now()
return visor.UnconfirmedTxn{
Txn: coin.Transaction{
Head: coin.TransactionHeader{
Hash: cipher.SumSHA256([]byte("cascas")),
},
},
Received: now,
Checked: now,
Announced: util.ZeroTime(),
}
}
示例10: TestBlockStat_02
func TestBlockStat_02(t *testing.T) {
bs := BlockStat{}
bs.Init()
hash1 := cipher.SumSHA256(secp256k1.RandByte(888))
n1 := 3
for i := 0; i < n1; i++ {
_, seckey := cipher.GenerateKeyPair()
sig := cipher.SignHash(hash1, seckey)
bs.try_add_hash_and_sig(hash1, sig)
}
hash2 := cipher.SumSHA256(secp256k1.RandByte(888))
n2 := 2
for i := 0; i < n2; i++ {
_, seckey := cipher.GenerateKeyPair()
sig := cipher.SignHash(hash2, seckey)
bs.try_add_hash_and_sig(hash2, sig)
}
hash3 := cipher.SumSHA256(secp256k1.RandByte(888))
n3 := 1
for i := 0; i < n3; i++ {
_, seckey := cipher.GenerateKeyPair()
sig := cipher.SignHash(hash3, seckey)
bs.try_add_hash_and_sig(hash3, sig)
}
best_hash, _, _ := bs.GetBestHashPubkeySig()
if best_hash != hash1 {
t.Log("BlockStat::try_add_hash_and_sig() or BlockStat::GetBestHashPubkeySig() issue.")
t.Fail()
}
}
示例11: NewWallet
//Generate Deterministic Wallet
//generates a random seed if seed is ""
func NewWallet(seed string) Wallet {
//if seed is blank, generate a new seed
if seed == "" {
seed_raw := cipher.SumSHA256(secp256k1.RandByte(64))
seed = hex.EncodeToString(seed_raw[:])
}
pub, sec := cipher.GenerateDeterministicKeyPair([]byte(seed[:]))
return Wallet{
Meta: map[string]string{
"filename": NewWalletFilename(),
"seed": seed,
"type": "deterministic",
"coin": "sky"},
Entry: NewWalletEntryFromKeypair(pub, sec),
}
}
示例12: TestGetTxnsMessageProcess
func TestGetTxnsMessageProcess(t *testing.T) {
v, _ := setupVisor()
d, _ := newVisorDaemon(v)
defer shutdown(d)
gc := setupExistingPool(d.Pool)
p := d.Pool
go p.Pool.ConnectionWriteLoop(gc)
tx := createUnconfirmedTxn()
tx.Txn.Head.Hash = cipher.SumSHA256([]byte("asdadwadwada"))
txns := []cipher.SHA256{tx.Txn.Hash()}
m := NewGetTxnsMessage(txns)
m.c = messageContext(addr)
go p.Pool.ConnectionWriteLoop(m.c.Conn)
defer m.c.Conn.Close()
// We don't have any to reply with
assert.NotPanics(t, func() { m.Process(d) })
assert.True(t, m.c.Conn.LastSent.IsZero())
// Disabled, nothing should happen
d.Visor.Visor.Unconfirmed.Txns[tx.Txn.Hash()] = tx
d.Visor.Config.Disabled = true
assert.NotPanics(t, func() { m.Process(d) })
assert.True(t, m.c.Conn.LastSent.IsZero())
// We have some to reply with
d.Visor.Config.Disabled = false
assert.NotPanics(t, func() { m.Process(d) })
wait()
assert.Equal(t, len(p.Pool.SendResults), 1)
if len(p.Pool.SendResults) == 0 {
t.Fatal("SendResults empty, would block")
}
sr := <-p.Pool.SendResults
assert.Equal(t, sr.Connection, m.c.Conn)
assert.Nil(t, sr.Error)
_, ok := sr.Message.(*GiveTxnsMessage)
assert.True(t, ok)
assert.False(t, m.c.Conn.LastSent.IsZero())
// Should not be broadcast to others
assert.True(t, gc.LastSent.IsZero())
}
示例13: NewWallet
// NewWallet generates Deterministic Wallet
// generates a random seed if seed is ""
func NewWallet(wltName string, opts ...Option) Wallet {
seedRaw := cipher.SumSHA256(secp256k1.RandByte(64))
seed := hex.EncodeToString(seedRaw[:])
w := Wallet{
Meta: map[string]string{
"filename": wltName,
"version": version,
"label": "",
"seed": seed,
"lastSeed": seed,
"tm": fmt.Sprintf("%v", time.Now().Unix()),
"type": "deterministic",
"coin": "sky"},
}
for _, opt := range opts {
opt(&w)
}
return w
}
示例14: main
func main() {
registerFlags()
parseFlags()
w := Wallet{
Meta: make(map[string]string), //map[string]string
Entries: make([]KeyEntry, genCount),
}
if BitcoinAddress == false {
w.Meta = map[string]string{"coin": "skycoin"}
} else {
w.Meta = map[string]string{"coin": "bitcoin"}
}
if seed == "" { //generate a new seed, as hex string
seed = cipher.SumSHA256(cipher.RandByte(1024)).Hex()
}
w.Meta["seed"] = seed
seckeys := cipher.GenerateDeterministicKeyPairs([]byte(seed), genCount)
for i, sec := range seckeys {
pub := cipher.PubKeyFromSecKey(sec)
w.Entries[i] = getKeyEntry(pub, sec)
}
output, err := json.MarshalIndent(w, "", " ")
if err != nil {
fmt.Printf("Error formating wallet to JSON. Error : %s\n", err.Error())
return
}
fmt.Printf("%s\n", string(output))
}
示例15: HashInner
// Hashes only the Transaction Inputs & Outputs
// This is what is signed
// Client hashes the inner hash with hash of output being spent and signs it with private key
func (self *Transaction) HashInner() cipher.SHA256 {
b1 := encoder.Serialize(self.In)
b2 := encoder.Serialize(self.Out)
b3 := append(b1, b2...)
return cipher.SumSHA256(b3)
}