// Decrypt message with private key of receiver.
// No one else except the sender will be able to decrypt the message.
Decrypt message with private key of receiver.
No one else except the sender will be able to decrypt the message.
// Decrypt message with private key of receiver . // No one else except the sender will be able to decrypt the message .
func (client *Client) Decrypt(msg *Message) *Message {
	hash := msg.Body.Hash
	puzzle := crypto.NewPuzzle(msg.Head.Diff)
	if !puzzle.Verify(hash, msg.Body.Npow) {
		return nil
	}
	session := client.PrivKey().Decrypt(msg.Head.Session)
	if session == nil {
		return nil
	}
	cipher := crypto.NewCipher(session)
	publicBytes := cipher.Decrypt(msg.Head.Sender)
	if publicBytes == nil {
		return nil
	}
	public := crypto.LoadPubKey(publicBytes)
	if public == nil {
		return nil
	}
	if public.Size() != gopeer.Get("AKEY_SIZE").(uint) {
		return nil
	}
	sign := cipher.Decrypt(msg.Body.Sign)
	if sign == nil {
		return nil
	}
	if !public.Verify(hash, sign) {
		return nil
	}
	titleBytes := cipher.Decrypt(msg.Head.Title)
	if titleBytes == nil {
		return nil
	}
	dataBytes := cipher.Decrypt(msg.Body.Data)
	if dataBytes == nil {
		return nil
	}
	rand := cipher.Decrypt(msg.Head.Rand)
	if rand == nil {
		return nil
	}
	check := crypto.SumHash(bytes.Join(
		[][]byte{
			rand,
			publicBytes,
			client.PubKey().Bytes(),
			titleBytes,
			dataBytes,
		},
		[]byte{},
	))
	if !bytes.Equal(check, hash) {
		return nil
	}
	return &Message{
		Head: HeadMessage{
			Diff:    msg.Head.Diff,
			Title:   titleBytes,
			Rand:    rand,
			Sender:  publicBytes,
			Session: session,
		},
		Body: BodyMessage{
			Data: dataBytes,
			Hash: hash,
			Sign: sign,
			Npow: msg.Body.Npow,
		},
	}
}
func ( client * Client ) Decrypt ( msg * Message ) * Message { hash := msg . Body . Hash puzzle := crypto . NewPuzzle ( msg . Head . Diff ) if ! puzzle . Verify ( hash , msg . Body . Npow ) { return nil } session := client . PrivKey ( ) . Decrypt ( msg . Head . Session ) if session == nil { return nil } cipher := crypto . NewCipher ( session ) publicBytes := cipher . Decrypt ( msg . Head . Sender ) if publicBytes == nil { return nil } public := crypto . LoadPubKey ( publicBytes ) if public == nil { return nil } if public . Size ( ) != gopeer . Get ( " " ) . ( uint ) { return nil } sign := cipher . Decrypt ( msg . Body . Sign ) if sign == nil { return nil } if ! public . Verify ( hash , sign ) { return nil } titleBytes := cipher . Decrypt ( msg . Head . Title ) if titleBytes == nil { return nil } dataBytes := cipher . Decrypt ( msg . Body . Data ) if dataBytes == nil { return nil } rand := cipher . Decrypt ( msg . Head . Rand ) if rand == nil { return nil } check := crypto . SumHash ( bytes . Join ( [ ] [ ] byte { rand , publicBytes , client . PubKey ( ) . Bytes ( ) , titleBytes , dataBytes , } , [ ] byte { } , ) ) if ! bytes . Equal ( check , hash ) { return nil } return & Message { Head : HeadMessage { Diff : msg . Head . Diff , Title : titleBytes , Rand : rand , Sender : publicBytes , Session : session , } , Body : BodyMessage { Data : dataBytes , Hash : hash , Sign : sign , Npow : msg . Body . Npow , } , } }
Decrypt message with private key of receiver.
Decrypt message with private key of receiver .
// Proof of work. Prevent spam. // Decrypt session key by private key of receiver. // Decrypt public key of sender by decrypted session key. // Load public key and check standart size. // Decrypt sign of message and verify this // by public key of sender and hash of message. // Decrypt title of message by session key. // Decrypt main data of message by session key. // Decrypt random string by session key. // Check received hash and generated hash. // Return decrypted message.