goprocrypt

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2019 License: MIT Imports: 8 Imported by: 0

README

goprocrypt

Simple Go RSA Asymmetric Key Encryption for Protocol Buffers
dependency
dep ensure -add "github.com/Yomiji/goprocrypt"
protocol

Provided EncryptedMessage protocol buffer is returned from an encrypted message. The EncryptedMessage protocol buffer has the following configuration:

syntax = "proto3";
package goprocrypt;
option go_package = "goprocrypt";

message EncryptedMessage {
    bytes id = 1; //hash of type
    bytes signature = 3;
    bytes digest = 5;
}

The idea is that an application would encrypt, send and then decrypt an EncryptedMessage protocol buffer. An application could use the id parameter to hash the protocol buffer's specific type.

useage
import (
	"github.com/Yomiji/genrsa"
	"github.com/Yomiji/goprocrypt"
	"github.com/golang/protobuf/proto"
	"testing"
	"time"
)

var party1private, party1public = genrsa.MakeKeys(2048)
var party2private, party2public = genrsa.MakeKeys(2048)

var testMsg = &TestMsg{
  Time:time.Now().Unix(),
  TrialNo:0,
  Msgtext:"Some Text",
}

func TestEncrypt(t *testing.T) {
	// private key necessary for signature application from first party, encrypting for second party
	encMsg, err := goprocrypt.Encrypt([]byte("test"), testMsg, party2public, party1private)
	
	// do something with encrypted message
}

func TestDecrypt(t *testing.T) {
	decMsg := &TestMsg{}

	encMsg, err := goprocrypt.Encrypt([]byte("test"), testMsg, party2public, party1private)
	
	// first party's public key for verification of identity and second party key for decryption
	// directly modifies decMsg to fill it with the decrypted data, the type must match or an error
	// occurs
	err = goprocrypt.Decrypt([]byte("test"), encMsg, party2private, party1public, decMsg)
	
	// do something with decMsg, which now contains testMsg unencrypted
}

To change the encryption algorithm:

goprocrypt.Hash = crypto.SHA256

To change the verification algorithm:

goprocrypt.Sign = crypto.SHA512_256

Documentation

Index

Constants

This section is empty.

Variables

Allow the developer to change the hash function

View Source
var Logger = log.New(os.Stdout, "[GOPROTOCRYPT] ", log.Ldate|log.Ltime)

Allow the developer to take logging

Allow the developer to change the signing function

Functions

func Decrypt

func Decrypt(label []byte, encryptedMsg *EncryptedMessage, privateKey *rsa.PrivateKey, publicKeyForSig *rsa.PublicKey, message proto.Message) (err error)

Decrypt the encrypted message to the given protocol buffer

Types

type EncryptedMessage

type EncryptedMessage struct {
	Signature            []byte   `protobuf:"bytes,1,opt,name=signature,proto3" json:"signature,omitempty"`
	Digest               []byte   `protobuf:"bytes,3,opt,name=digest,proto3" json:"digest,omitempty"`
	VerificationKeyId    []byte   `protobuf:"bytes,8,opt,name=verificationKeyId,proto3" json:"verificationKeyId,omitempty"`
	DecryptionKeyId      []byte   `protobuf:"bytes,9,opt,name=decryptionKeyId,proto3" json:"decryptionKeyId,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func Encrypt

func Encrypt(label []byte, message proto.Message, publicKey *rsa.PublicKey, privateKeyForSig *rsa.PrivateKey) (encMsg *EncryptedMessage, err error)

Encrypt a protocol buffer 'message' with the given label using the given public key

func (*EncryptedMessage) Descriptor

func (*EncryptedMessage) Descriptor() ([]byte, []int)

func (*EncryptedMessage) GetDecryptionKeyId

func (m *EncryptedMessage) GetDecryptionKeyId() []byte

func (*EncryptedMessage) GetDigest

func (m *EncryptedMessage) GetDigest() []byte

func (*EncryptedMessage) GetSignature

func (m *EncryptedMessage) GetSignature() []byte

func (*EncryptedMessage) GetVerificationKeyId

func (m *EncryptedMessage) GetVerificationKeyId() []byte

func (*EncryptedMessage) ProtoMessage

func (*EncryptedMessage) ProtoMessage()

func (*EncryptedMessage) Reset

func (m *EncryptedMessage) Reset()

func (*EncryptedMessage) String

func (m *EncryptedMessage) String() string

func (*EncryptedMessage) XXX_DiscardUnknown

func (m *EncryptedMessage) XXX_DiscardUnknown()

func (*EncryptedMessage) XXX_Marshal

func (m *EncryptedMessage) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*EncryptedMessage) XXX_Merge

func (m *EncryptedMessage) XXX_Merge(src proto.Message)

func (*EncryptedMessage) XXX_Size

func (m *EncryptedMessage) XXX_Size() int

func (*EncryptedMessage) XXX_Unmarshal

func (m *EncryptedMessage) XXX_Unmarshal(b []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL