本文整理匯總了Golang中github.com/FactomProject/factomd/common/primitives.NewHash函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewHash函數的具體用法?Golang NewHash怎麽用?Golang NewHash使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewHash函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: addIdentity
func addIdentity(entry interfaces.IEBEntry, height uint32, st *State) error {
extIDs := entry.ExternalIDs()
// This check is here to prevent possible index out of bounds with extIDs[:6]
if len(extIDs) != 7 {
return errors.New("Identity Error Create Management: Invalid external ID length")
}
if bytes.Compare([]byte{0x00}, extIDs[0]) != 0 || // Version
!CheckExternalIDsLength(extIDs[:6], []int{1, 14, 32, 32, 32, 32}) { // Nonce
return errors.New("Identity Error Create Identity Chain: Invalid external ID length")
}
chainID := entry.GetChainID()
IdentityIndex := st.isIdentityChain(chainID)
if IdentityIndex == -1 {
IdentityIndex = st.CreateBlankFactomIdentity(chainID)
}
h := primitives.NewHash(extIDs[2])
st.Identities[IdentityIndex].Key1 = h
h = primitives.NewHash(extIDs[3])
st.Identities[IdentityIndex].Key2 = h
h = primitives.NewHash(extIDs[4])
st.Identities[IdentityIndex].Key3 = h
h = primitives.NewHash(extIDs[5])
st.Identities[IdentityIndex].Key4 = h
st.Identities[IdentityIndex].IdentityCreated = height
return nil
}
示例2: TestMarshalUnmarshalServerFault
func TestMarshalUnmarshalServerFault(t *testing.T) {
ts := primitives.NewTimestampNow()
vmIndex := int(*ts) % 10
sf := NewServerFault(primitives.NewHash([]byte("a test")), primitives.NewHash([]byte("a test2")), vmIndex, 10, 100, 0, ts)
hex, err := sf.MarshalBinary()
if err != nil {
t.Error(err)
}
t.Logf("Marshalled - %x", hex)
sf2, err := UnmarshalMessage(hex)
if err != nil {
t.Error(err)
}
str := sf2.String()
t.Logf("str - %v", str)
if sf2.Type() != constants.FED_SERVER_FAULT_MSG {
t.Errorf("Invalid message type unmarshalled - got %v, expected %v", sf2.Type(), constants.FED_SERVER_FAULT_MSG)
}
hex2, err := sf2.(*ServerFault).MarshalBinary()
if err != nil {
t.Error(err)
}
if len(hex) != len(hex2) {
t.Error("Hexes aren't of identical length")
}
for i := range hex {
if hex[i] != hex2[i] {
t.Error("Hexes do not match")
}
}
}
示例3: NextECBlock
func NextECBlock(prev interfaces.IEntryCreditBlock) (interfaces.IEntryCreditBlock, error) {
e := NewECBlock()
// Handle the really unusual case of the first block.
if prev == nil {
e.GetHeader().SetPrevHeaderHash(primitives.NewHash(constants.ZERO_HASH))
e.GetHeader().SetPrevFullHash(primitives.NewHash(constants.ZERO_HASH))
e.GetHeader().SetDBHeight(0)
} else {
v, err := prev.HeaderHash()
if err != nil {
return nil, err
}
e.GetHeader().SetPrevHeaderHash(v)
v, err = prev.GetFullHash()
if err != nil {
return nil, err
}
e.GetHeader().SetPrevFullHash(v)
e.GetHeader().SetDBHeight(prev.GetHeader().GetDBHeight() + 1)
}
if err := e.(*ECBlock).BuildHeader(); err != nil {
return nil, err
}
return e, nil
}
示例4: UnmarshalBinaryData
func (m *EOM) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Error unmarshalling EOM message: %v", r)
}
}()
newData = data
if newData[0] != m.Type() {
return nil, fmt.Errorf("Invalid Message type")
}
newData = newData[1:]
m.Timestamp = new(primitives.Timestamp)
newData, err = m.Timestamp.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.ChainID = primitives.NewHash(constants.ZERO_HASH)
newData, err = m.ChainID.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.Minute, newData = newData[0], newData[1:]
if m.Minute < 0 || m.Minute >= 10 {
return nil, fmt.Errorf("Minute number is out of range")
}
m.VMIndex = int(newData[0])
newData = newData[1:]
m.FactoidVM = uint8(newData[0]) == 1
newData = newData[1:]
m.DBHeight, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
m.SysHeight, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
m.SysHash = primitives.NewHash(constants.ZERO_HASH)
newData, err = m.SysHash.UnmarshalBinaryData(newData)
if len(newData) > 0 {
sig := new(primitives.Signature)
newData, err = sig.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.Signature = sig
}
return data, nil
}
示例5: TestThatFullAndFaultCoreHashesMatch
func TestThatFullAndFaultCoreHashesMatch(t *testing.T) {
ts := primitives.NewTimestampNow()
vmIndex := int(*ts) % 10
sf := NewServerFault(primitives.NewHash([]byte("a test")), primitives.NewHash([]byte("a test2")), vmIndex, 10, 100, 0, ts)
sl := coupleOfSigs(t)
fsf := NewFullServerFault(nil, sf, sl, 0)
if !sf.GetCoreHash().IsSameAs(fsf.GetCoreHash()) {
t.Error("CoreHashes do not match between FullServerFault and ServerFault")
}
}
示例6: MarshalBinary
func (m *MissingMsg) MarshalBinary() ([]byte, error) {
var buf primitives.Buffer
binary.Write(&buf, binary.BigEndian, m.Type())
t := m.GetTimestamp()
data, err := t.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(data)
if m.Asking == nil {
m.Asking = primitives.NewHash(constants.ZERO_HASH)
}
data, err = m.Asking.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(data)
buf.WriteByte(uint8(m.VMIndex))
binary.Write(&buf, binary.BigEndian, m.DBHeight)
binary.Write(&buf, binary.BigEndian, m.SystemHeight)
binary.Write(&buf, binary.BigEndian, uint32(len(m.ProcessListHeight)))
for _, h := range m.ProcessListHeight {
binary.Write(&buf, binary.BigEndian, h)
}
bb := buf.DeepCopyBytes()
return bb, nil
}
示例7: MarshalBinary
func (m *EOM) MarshalBinary() (data []byte, err error) {
var buf primitives.Buffer
resp, err := m.MarshalForSignature()
if err != nil {
return nil, err
}
buf.Write(resp)
binary.Write(&buf, binary.BigEndian, m.DBHeight)
binary.Write(&buf, binary.BigEndian, m.SysHeight)
if m.SysHash == nil {
m.SysHash = primitives.NewHash(constants.ZERO_HASH)
}
if d, err := m.SysHash.MarshalBinary(); err != nil {
return nil, err
} else {
buf.Write(d)
}
sig := m.GetSignature()
if sig != nil {
sigBytes, err := sig.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(sigBytes)
}
return buf.DeepCopyBytes(), nil
}
示例8: NewRepeatingHash
func NewRepeatingHash(b byte) interfaces.IHash {
tmp := make([]byte, constants.HASH_LENGTH)
for i := range tmp {
tmp[i] = b
}
return primitives.NewHash(tmp)
}
示例9: BtcDecode
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
// This is part of the MsgAck interface implementation.
func (msg *MsgAck) BtcDecode(r io.Reader, pver uint32) error {
newData, err := ioutil.ReadAll(r)
if err != nil {
return fmt.Errorf("MsgAck.BtcDecode reader is invalid")
}
if len(newData) != 169 {
return fmt.Errorf("MsgAck.BtcDecode reader does not have right length: ", len(newData))
}
msg.Height, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
msg.ChainID = new(primitives.Hash)
newData, _ = msg.ChainID.UnmarshalBinaryData(newData)
msg.Index, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
msg.Typ, newData = newData[0], newData[1:]
msg.Affirmation = primitives.NewHash(newData[0:32])
newData = newData[32:]
copy(msg.SerialHash[:], newData[0:32])
newData = newData[32:]
copy(msg.Signature[:], newData[0:63])
return nil
}
示例10: UnmarshalBinaryData
func (m *MissingData) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Error unmarshalling: %v", r)
}
}()
newData = data
if newData[0] != m.Type() {
return nil, fmt.Errorf("Invalid Message type")
}
newData = newData[1:]
m.Timestamp = new(primitives.Timestamp)
newData, err = m.Timestamp.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.RequestHash = primitives.NewHash(constants.ZERO_HASH)
newData, err = m.RequestHash.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.Peer2Peer = true // Always a peer2peer request.
return data, nil
}
示例11: registerIdentityAsServer
func registerIdentityAsServer(entry interfaces.IEBEntry, height uint32, st *State) error {
extIDs := entry.ExternalIDs()
if len(extIDs) == 0 {
return errors.New("Identity Error Register Identity: Invalid external ID length")
}
if bytes.Compare([]byte{0x00}, extIDs[0]) != 0 || // Version
!CheckExternalIDsLength(extIDs, []int{1, 26, 32, 33, 64}) { // Signiture
return errors.New("Identity Error Register Identity: Invalid external ID length")
}
chainID := entry.GetChainID()
IdentityIndex := st.isIdentityChain(chainID)
if IdentityIndex == -1 {
IdentityIndex = st.CreateBlankFactomIdentity(chainID)
}
sigmsg, err := AppendExtIDs(extIDs, 0, 2)
if err != nil {
return err
} else {
// Verify Signature
idKey := st.Identities[IdentityIndex].Key1
if CheckSig(idKey, extIDs[3][1:33], sigmsg, extIDs[4]) {
st.Identities[IdentityIndex].ManagementRegistered = height
st.Identities[IdentityIndex].ManagementChainID = primitives.NewHash(extIDs[2][:32])
} else {
return errors.New("New Management Chain Register for identity [" + chainID.String()[:10] + "] is invalid. Bad signiture")
}
}
return nil
}
示例12: registerFactomIdentity
func registerFactomIdentity(entry interfaces.IEBEntry, chainID interfaces.IHash, height uint32, st *State) error {
extIDs := entry.ExternalIDs()
if len(extIDs) == 0 {
return errors.New("Identity Error Register Identity: Invalid external ID length")
}
if bytes.Compare([]byte{0x00}, extIDs[0]) != 0 || // Version
!CheckExternalIDsLength(extIDs, []int{1, 24, 32, 33, 64}) { // Signiture
return errors.New("Identity Error Register Identity: Invalid external ID length")
}
// find the Identity index from the chain id in the external id. add this chainID as the management id
idChain := primitives.NewHash(extIDs[2])
IdentityIndex := st.isIdentityChain(idChain)
if IdentityIndex == -1 {
IdentityIndex = st.CreateBlankFactomIdentity(idChain)
}
sigmsg, err := AppendExtIDs(extIDs, 0, 2)
if err != nil {
return err
} else {
// Verify Signature
idKey := st.Identities[IdentityIndex].Key1
if CheckSig(idKey, extIDs[3][1:33], sigmsg, extIDs[4]) {
st.Identities[IdentityIndex].ManagementRegistered = height
} else {
return errors.New("New Management Chain Register for identity [" + chainID.String()[:10] + "] is invalid. Bad signiture")
}
}
st.Identities[IdentityIndex].IdentityRegistered = height
return nil
}
示例13: getEntry
func getEntry(hash string) *EntryHolder {
entryHash, err := primitives.HexToHash(hash)
if err != nil {
return nil
}
dbase := StatePointer.GetAndLockDB()
entry, err := dbase.FetchEntry(entryHash)
StatePointer.UnlockDB()
if err != nil {
return nil
}
if entry == nil {
return nil
}
holder := new(EntryHolder)
holder.Hash = hash
holder.ChainID = entry.GetChainID().String()
max := byte(0x80)
for _, data := range entry.ExternalIDs() {
hexString := false
for _, bytes := range data {
if bytes > max {
hexString = true
break
}
}
if hexString {
str := hex.EncodeToString(data)
holder.ExtIDs = append(holder.ExtIDs[:], "<span id='encoding'><a>Hex : </a></span><span id='data'>"+htemp.HTMLEscaper(str)+"</span>")
} else {
str := string(data)
holder.ExtIDs = append(holder.ExtIDs[:], "<span id='encoding'><a>Ascii: </a></span><span id='data'>"+htemp.HTMLEscaper(str)+"</span>")
}
}
holder.Version = 0
holder.Height = fmt.Sprintf("%d", entry.GetDatabaseHeight())
holder.ContentLength = len(entry.GetContent())
data := sha256.Sum256(entry.GetContent())
content := string(entry.GetContent())
holder.Content = htemp.HTMLEscaper(content)
if bytes, err := entry.MarshalBinary(); err != nil {
holder.ECCost = "Error"
} else {
if eccost, err := util.EntryCost(bytes); err != nil {
holder.ECCost = "Error"
} else {
holder.ECCost = fmt.Sprintf("%d", eccost)
}
}
//holder.Content = string(entry.GetContent())
holder.ContentHash = primitives.NewHash(data[:]).String()
return holder
}
示例14: CreateDBlock
func (s *State) CreateDBlock() (b interfaces.IDirectoryBlock, err error) {
prev := s.GetCurrentDirectoryBlock()
b = new(directoryBlock.DirectoryBlock)
b.SetHeader(new(directoryBlock.DBlockHeader))
b.GetHeader().SetVersion(constants.VERSION_0)
if prev == nil {
b.GetHeader().SetPrevLedgerKeyMR(primitives.NewZeroHash())
b.GetHeader().SetPrevKeyMR(primitives.NewZeroHash())
b.GetHeader().SetDBHeight(0)
eb, _ := entryCreditBlock.NextECBlock(nil)
s.EntryCreditBlock = eb
} else {
bodyMR, err := prev.BuildBodyMR()
if err != nil {
return nil, err
}
prev.GetHeader().SetBodyMR(bodyMR)
prevLedgerKeyMR := prev.GetHash()
if prevLedgerKeyMR == nil {
return nil, errors.New("prevLedgerKeyMR is nil")
}
b.GetHeader().SetPrevLedgerKeyMR(prevLedgerKeyMR)
b.GetHeader().SetPrevKeyMR(prev.GetKeyMR())
b.GetHeader().SetDBHeight(prev.GetHeader().GetDBHeight() + 1)
eb, _ := entryCreditBlock.NextECBlock(s.EntryCreditBlock)
s.EntryCreditBlock = eb
}
b.SetDBEntries(make([]interfaces.IDBEntry, 0))
s.CurrentAdminBlock = s.NewAdminBlock()
b.AddEntry(primitives.NewHash(constants.ADMIN_CHAINID), primitives.NewZeroHash())
b.AddEntry(primitives.NewHash(constants.EC_CHAINID), primitives.NewZeroHash())
b.AddEntry(primitives.NewHash(constants.FACTOID_CHAINID), primitives.NewZeroHash())
return b, err
}
示例15: FetchECBlockHead
func (db *Overlay) FetchECBlockHead() (interfaces.IEntryCreditBlock, error) {
blk := entryCreditBlock.NewECBlock()
block, err := db.FetchChainHeadByChainID([]byte{byte(ENTRYCREDITBLOCK)}, primitives.NewHash(blk.GetChainID()), blk)
if err != nil {
return nil, err
}
if block == nil {
return nil, nil
}
return block.(interfaces.IEntryCreditBlock), nil
}