当前位置: 首页>>代码示例>>Golang>>正文


Golang crypto.CalculateTruncatedHash函数代码示例

本文整理汇总了Golang中common/crypto.CalculateTruncatedHash函数的典型用法代码示例。如果您正苦于以下问题:Golang CalculateTruncatedHash函数的具体用法?Golang CalculateTruncatedHash怎么用?Golang CalculateTruncatedHash使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了CalculateTruncatedHash函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: signHeartbeat

// take new heartbeat (our own), sign it, and package it into a signedHearteat
// I'm pretty sure this only follows a newHeartbeat() call; they can be merged
func (s *State) signHeartbeat(hb *heartbeat) (sh *SignedHeartbeat, err error) {
	sh = new(SignedHeartbeat)

	// confirm heartbeat and hash
	sh.heartbeat = hb
	gobHb, err := hb.GobEncode()
	if err != nil {
		return
	}
	sh.heartbeatHash, err = crypto.CalculateTruncatedHash(gobHb)
	if err != nil {
		return
	}

	// fill out signatures
	sh.signatures = make([]crypto.Signature, 1)
	signedHb, err := s.secretKey.Sign(sh.heartbeatHash[:])
	if err != nil {
		return
	}
	sh.signatures[0] = signedHb.Signature
	sh.signatories = make([]byte, 1)
	sh.signatories[0] = s.self.index
	return
}
开发者ID:Jonbeek,项目名称:Sia,代码行数:27,代码来源:consensus.go

示例2: TestCreateState

// Create a state, check the defaults
func TestCreateState(t *testing.T) {
	// does a state create without errors?
	s, err := CreateState(common.NewZeroNetwork())
	if err != nil {
		t.Fatal(err)
	}

	// check that previousEntropyStage1 is initialized correctly
	var emptyEntropy common.Entropy
	emptyHash, err := crypto.CalculateTruncatedHash(emptyEntropy[:])
	if err != nil {
		t.Fatal(err)
	}
	for i := range s.previousEntropyStage1 {
		if s.previousEntropyStage1[i] != emptyHash {
			t.Error("previousEntropyStage1 initialized incorrectly at index ", i)
		}
	}

	// sanity check the default values
	if s.participantIndex != 255 {
		t.Error("s.participantIndex initialized to ", s.participantIndex)
	}
	if s.currentStep != 1 {
		t.Error("s.currentStep should be initialized to 1!")
	}
	if s.wallets == nil {
		t.Error("s.wallets was not initialized")
	}
}
开发者ID:Radzell,项目名称:Sia,代码行数:31,代码来源:state_test.go

示例3: CreateState

// Create and initialize a state object.
func CreateState(messageRouter common.MessageRouter) (s *State, err error) {
	s = new(State)
	// check that we have a non-nil messageSender
	if messageRouter == nil {
		err = fmt.Errorf("Cannot initialize with a nil messageRouter")
		return
	}

	// create a signature keypair for this state
	pubKey, secKey, err := crypto.CreateKeyPair()
	if err != nil {
		return
	}

	// calculate the value of an empty hash (default for storedEntropyStage2 on all hosts is a blank array)
	emptyHash, err := crypto.CalculateTruncatedHash(s.storedEntropyStage2[:])
	if err != nil {
		return
	}

	// set state variables to their defaults
	s.messageRouter = messageRouter
	s.self = new(participant)
	s.self.address = messageRouter.AddMessageHandler(s)
	s.self.publicKey = pubKey
	s.secretKey = secKey
	for i := range s.previousEntropyStage1 {
		s.previousEntropyStage1[i] = emptyHash
	}
	s.participantIndex = 255
	s.currentStep = 1
	s.wallets = make(map[string]uint64)

	return
}
开发者ID:Radzell,项目名称:Sia,代码行数:36,代码来源:state.go

示例4: compile

// compile() takes the list of heartbeats and uses them to advance the state.
func (s *State) compile() {
	// fetch a participant ordering
	participantOrdering := s.participantOrdering()

	// Lock down s.participants and s.heartbeats for editing
	s.participantsLock.Lock()
	s.heartbeatsLock.Lock()

	// Read heartbeats, process them, then archive them.
	for _, participant := range participantOrdering {
		if s.participants[participant] == nil {
			continue
		}

		// each participant must submit exactly 1 heartbeat
		if len(s.heartbeats[participant]) != 1 {
			s.tossParticipant(participant)
			continue
		}

		// this is the only way I know to access the only element of a map;
		// the key is unknown
		for _, hb := range s.heartbeats[participant] {
			s.processHeartbeat(hb, participant)
		}

		// archive heartbeats (unimplemented)

		// clear heartbeat list for next block
		s.heartbeats[participant] = make(map[crypto.TruncatedHash]*heartbeat)
	}

	// move UpcomingEntropy to CurrentEntropy
	s.currentEntropy = s.upcomingEntropy

	// generate a new heartbeat and add it to s.Heartbeats
	hb, err := s.newHeartbeat()
	if err != nil {
		log.Fatalln(err)
	}
	hash, err := crypto.CalculateTruncatedHash(hb.marshal())
	if err != nil {
		log.Fatalln(err)
	}
	s.heartbeats[s.participantIndex][hash] = hb

	// sign and annouce the heartbeat
	shb, err := s.signHeartbeat(hb)
	if err != nil {
		log.Fatalln(err)
	}
	err = s.announceSignedHeartbeat(shb)
	if err != nil {
		log.Fatalln(err)
	}
}
开发者ID:Radzell,项目名称:Sia,代码行数:57,代码来源:consensus.go

示例5: processHeartbeat

// Update the state according to the information presented in the heartbeat
// processHeartbeat uses return codes for testing purposes
func (s *State) processHeartbeat(hb *heartbeat, i byte) (err error) {
	print("Confirming Participant ")
	println(i)

	// Add the entropy to UpcomingEntropy
	th, err := crypto.CalculateTruncatedHash(append(s.upcomingEntropy[:], hb.entropy[:]...))
	s.upcomingEntropy = common.Entropy(th)

	return
}
开发者ID:Jonbeek,项目名称:Sia,代码行数:12,代码来源:consensus.go

示例6: processHeartbeat

// Update the state according to the information presented in the heartbeat
// processHeartbeat uses return codes for testing purposes
func (s *State) processHeartbeat(hb *heartbeat, i participantIndex) int {
	// compare EntropyStage2 to the hash from the previous heartbeat
	expectedHash, err := crypto.CalculateTruncatedHash(hb.entropyStage2[:])
	if err != nil {
		log.Fatalln(err)
	}
	if expectedHash != s.previousEntropyStage1[i] {
		s.tossParticipant(i)
		return 1
	}

	// Add the EntropyStage2 to UpcomingEntropy
	th, err := crypto.CalculateTruncatedHash(append(s.upcomingEntropy[:], hb.entropyStage2[:]...))
	s.upcomingEntropy = common.Entropy(th)

	// store entropyStage1 to compare with next heartbeat from this participant
	s.previousEntropyStage1[i] = hb.entropyStage1

	return 0
}
开发者ID:Radzell,项目名称:Sia,代码行数:22,代码来源:consensus.go

示例7: tossParticipant

// Removes all traces of a participant from the State
func (s *State) tossParticipant(pi participantIndex) {
	// remove from s.Participants
	s.participants[pi] = nil

	// remove from s.PreviousEntropyStage1
	var emptyEntropy common.Entropy
	zeroHash, err := crypto.CalculateTruncatedHash(emptyEntropy[:])
	if err != nil {
		log.Fatal(err)
	}
	s.previousEntropyStage1[pi] = zeroHash

	// nil map in s.Heartbeats
	s.heartbeats[pi] = nil
}
开发者ID:Radzell,项目名称:Sia,代码行数:16,代码来源:consensus.go

示例8: compile

// compile() takes the list of heartbeats and uses them to advance the state.
func (s *State) compile() {
	participantOrdering := s.participantOrdering()

	// Read read heartbeats, process them, then archive them. Other functions
	// concurrently access the heartbeats, so mutexes are needed.
	for _, participant := range participantOrdering {
		if s.participants[participant] == nil {
			continue
		}

		// each participant must submit exactly 1 heartbeat
		if len(s.heartbeats[participant]) != 1 {
			s.tossParticipant(participant)
			continue
		}

		for _, hb := range s.heartbeats[participant] {
			s.processHeartbeat(hb, participant)
		}

		// archive heartbeats
		// currently, archives are sent to /dev/null
		s.heartbeats[participant] = make(map[crypto.TruncatedHash]*heartbeat)
	}

	// move UpcomingEntropy to CurrentEntropy
	s.currentEntropy = s.upcomingEntropy

	// generate a new heartbeat and add it to s.Heartbeats
	hb, err := s.newHeartbeat()
	if err != nil {
		log.Fatalln(err)
	}
	hash, err := crypto.CalculateTruncatedHash(hb.marshal())
	if err != nil {
		log.Fatalln(err)
	}
	s.heartbeats[s.participantIndex][hash] = hb

	// sign and annouce the heartbeat
	shb, err := s.signHeartbeat(hb)
	if err != nil {
		log.Fatalln(err)
	}
	s.announceSignedHeartbeat(shb)
}
开发者ID:jaked122,项目名称:Sia,代码行数:47,代码来源:consensus.go

示例9: unmarshalSignedHeartbeat

// convert string to signedHeartbeat
func unmarshalSignedHeartbeat(msh []byte) (sh *signedHeartbeat, err error) {
	// we reference the nth element in the []byte, make sure there is an nth element
	if len(msh) <= marshalledHeartbeatLen() {
		err = fmt.Errorf("input for unmarshalSignedHeartbeat is too short")
		return
	}
	numSignatures := int(msh[marshalledHeartbeatLen()]) // the nth element

	// verify that the total length of msh is what is expected
	signatureSectionLen := numSignatures * (crypto.SignatureSize + 1)
	totalLen := marshalledHeartbeatLen() + 1 + signatureSectionLen
	if len(msh) != totalLen {
		err = fmt.Errorf("input for UnmarshalSignedHeartbeat is incorrect length, expecting ", totalLen, " bytes")
		return
	}

	// get sh.Heartbeat and sh.HeartbeatHash
	sh = new(signedHeartbeat)
	index := 0
	heartbeat, err := unmarshalHeartbeat(msh[index:marshalledHeartbeatLen()])
	if err != nil {
		return
	}
	heartbeatHash, err := crypto.CalculateTruncatedHash(msh[index:marshalledHeartbeatLen()])
	if err != nil {
		return
	}
	sh.heartbeat = heartbeat
	sh.heartbeatHash = heartbeatHash

	// get sh.Signatures and sh.Signatories
	index += marshalledHeartbeatLen()
	index += 1 // skip the numSignatures byte
	sh.signatures = make([]crypto.Signature, numSignatures)
	sh.signatories = make([]participantIndex, numSignatures)
	for i := 0; i < numSignatures; i++ {
		copy(sh.signatures[i][:], msh[index:])
		index += crypto.SignatureSize
		sh.signatories[i] = participantIndex(msh[index])
		index += 1
	}

	return
}
开发者ID:Radzell,项目名称:Sia,代码行数:45,代码来源:consensus.go

示例10: CreateState

// Create and initialize a state object
func CreateState(messageSender common.MessageSender, participantIndex participantIndex) (s State, err error) {
	// check that we have a non-nil messageSender
	if messageSender == nil {
		err = fmt.Errorf("Cannot initialize with a nil messageSender")
		return
	}

	// check that participantIndex is legal
	if int(participantIndex) >= common.QuorumSize {
		err = fmt.Errorf("Invalid participant index!")
		return
	}

	// initialize crypto keys
	pubKey, secKey, err := crypto.CreateKeyPair()
	if err != nil {
		return
	}

	// create and fill out the participant object
	self := new(Participant)
	self.Address = messageSender.Address()
	self.Address.Id = common.Identifier(participantIndex)
	self.PublicKey = pubKey

	// calculate the value of an empty hash (default for storedEntropyStage2 on all hosts is a blank array)
	emptyHash, err := crypto.CalculateTruncatedHash(s.storedEntropyStage2[:])
	if err != nil {
		return
	}

	// set state variables to their defaults
	s.messageSender = messageSender
	s.AddParticipant(self, participantIndex)
	s.secretKey = secKey
	for i := range s.previousEntropyStage1 {
		s.previousEntropyStage1[i] = emptyHash
	}
	s.participantIndex = participantIndex
	s.currentStep = 1
	s.wallets = make(map[string]uint64)

	return
}
开发者ID:jaked122,项目名称:Sia,代码行数:45,代码来源:state.go

示例11: TestnewHeartbeat

// Verify that newHeartbeat() produces valid heartbeats
func TestnewHeartbeat(t *testing.T) {
	// create a state, and then a heartbeat
	s, err := CreateState(common.NewZeroNetwork(), 0)
	if err != nil {
		t.Fatal(err)
	}
	hb, err := s.newHeartbeat()
	if err != nil {
		t.Fatal(err)
	}

	// verify that entropy is being properly generated when making the heartbeat
	storedEntropyHash, err := crypto.CalculateTruncatedHash(s.storedEntropyStage2[:])
	if err != nil {
		t.Fatal(err)
	} else if hb.entropyStage1 != storedEntropyHash {
		t.Fatal("newHeartbeat() incorrectly producing EntropyStage1 from s.StoredEntropyStage2")
	}
}
开发者ID:jaked122,项目名称:Sia,代码行数:20,代码来源:consensus_test.go

示例12: Start

// Take an unstarted State and begin the consensus algorithm cycle
func (s *State) Start() {
	// start the ticker to progress the state
	go s.tick()

	s.lock.Lock()
	// create first heartbeat and add it to heartbeat map, then announce it
	hb, err := s.newHeartbeat()
	if err != nil {
		return
	}
	heartbeatHash, err := crypto.CalculateTruncatedHash([]byte(hb.marshal()))
	s.heartbeats[s.participantIndex][heartbeatHash] = hb
	shb, err := s.signHeartbeat(hb)
	if err != nil {
		return
	}
	s.announceSignedHeartbeat(shb)
	s.lock.Unlock()
}
开发者ID:jaked122,项目名称:Sia,代码行数:20,代码来源:state.go

示例13: randInt

// Use the entropy stored in the state to generate a random integer [low, high)
// randInt only runs during compile(), when the mutexes are already locked
//
// needs to be converted to return uint64
func (s *State) randInt(low int, high int) (randInt int, err error) {
	// verify there's a gap between the numbers
	if low == high {
		err = fmt.Errorf("low and high cannot be the same number")
		return
	}

	// Convert CurrentEntropy into an int
	rollingInt := 0
	for i := 0; i < 4; i++ {
		rollingInt = rollingInt << 8
		rollingInt += int(s.currentEntropy[i])
	}

	randInt = (rollingInt % (high - low)) + low

	// Convert random number seed to next value
	truncatedHash, err := crypto.CalculateTruncatedHash(s.currentEntropy[:])
	s.currentEntropy = common.Entropy(truncatedHash)
	return
}
开发者ID:Radzell,项目名称:Sia,代码行数:25,代码来源:state.go

示例14: signHeartbeat

// take new heartbeat (our own), sign it, and package it into a signedHearteat
// I'm pretty sure this only follows a newHeartbeat() call; they can be merged
func (s *State) signHeartbeat(hb *heartbeat) (sh *signedHeartbeat, err error) {
	sh = new(signedHeartbeat)

	// confirm heartbeat and hash
	sh.heartbeat = hb
	marshalledHb := hb.marshal()
	sh.heartbeatHash, err = crypto.CalculateTruncatedHash([]byte(marshalledHb))
	if err != nil {
		return
	}

	// fill out sigantures
	sh.signatures = make([]crypto.Signature, 1)
	signedHb, err := crypto.Sign(s.secretKey, string(sh.heartbeatHash[:]))
	if err != nil {
		return
	}
	sh.signatures[0] = signedHb.Signature
	sh.signatories = make([]participantIndex, 1)
	sh.signatories[0] = s.participantIndex
	return
}
开发者ID:Radzell,项目名称:Sia,代码行数:24,代码来源:consensus.go

示例15: addNewParticipant

// Add a participant to the state, tell the participant about ourselves
func (s *State) addNewParticipant(payload []byte) {
	// extract index and participant object from payload
	participantIndex := payload[0]
	p, err := unmarshalParticipant(payload[1:])
	if err != nil {
		return
	}

	// for this participant, make the heartbeat map and add the default heartbeat
	hb := new(heartbeat)
	emptyHash, err := crypto.CalculateTruncatedHash(hb.entropyStage2[:])
	hb.entropyStage1 = emptyHash
	s.heartbeatsLock.Lock()
	s.participantsLock.Lock()
	s.heartbeats[participantIndex] = make(map[crypto.TruncatedHash]*heartbeat)
	s.heartbeats[participantIndex][emptyHash] = hb
	s.heartbeatsLock.Unlock()

	if *p == *s.self {
		// add our self object to the correct index in participants
		s.participants[participantIndex] = s.self
		s.tickingLock.Lock()
		s.ticking = true
		s.tickingLock.Unlock()

		go s.tick()
	} else {
		// add the participant to participants
		s.participants[participantIndex] = p

		// tell the new guy about ourselves
		m := new(common.Message)
		m.Destination = p.address
		m.Payload = append([]byte(string(newParticipant)), s.self.marshal()...)
		s.messageRouter.SendMessage(m)
	}
	s.participantsLock.Unlock()
}
开发者ID:Radzell,项目名称:Sia,代码行数:39,代码来源:state.go


注:本文中的common/crypto.CalculateTruncatedHash函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。