Skip to content

Commit

Permalink
test: proc
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Jun 4, 2024
1 parent f05e418 commit 11d67dd
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 78 deletions.
17 changes: 17 additions & 0 deletions kata/reflux/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package reflux

import "encoding/json"

var _ Codec = (*CodecJSON)(nil)

type CodecJSON struct{}

// Marshal implements Codec.
func (CodecJSON) Marshal(v any) ([]byte, error) {
return json.Marshal(v)
}

// Unmarshal implements Codec.
func (CodecJSON) Unmarshal(data []byte, v any) error {
return json.Unmarshal(data, v)
}
103 changes: 86 additions & 17 deletions kata/reflux/reflux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,22 @@ type CodecString interface {
DecodeString(string) ([]byte, error)
}

type Codec interface {
Marshal(any) ([]byte, error)
Unmarshal([]byte, any) error
}

type Option func(*Reflux)

func WithCodecString(c CodecString) Option {
return func(r *Reflux) {
if c != nil {
r.codecString = c
}
}
}

func WithCodec(c Codec) Option {
return func(r *Reflux) {
if c != nil {
r.codec = c
Expand All @@ -28,9 +41,10 @@ func WithCodecString(c CodecString) Option {
}

type Reflux struct {
priv *rsa.PrivateKey
pub *rsa.PublicKey
codec CodecString
priv *rsa.PrivateKey
pub *rsa.PublicKey
codec Codec
codecString CodecString
}

// New returns a new Reflux.
Expand All @@ -51,9 +65,10 @@ func New(privKey, pubKey string, opts ...Option) (*Reflux, error) {
}
}
r := &Reflux{
priv: priv,
pub: pub,
codec: base64.StdEncoding,
priv: priv,
pub: pub,
codec: CodecJSON{},
codecString: base64.StdEncoding,
}
for _, f := range opts {
f(r)
Expand All @@ -65,35 +80,35 @@ func (r *Reflux) PrivateKey() *rsa.PrivateKey { return r.priv }

func (r *Reflux) PublicKey() *rsa.PublicKey { return r.pub }

// Encrypt encode a protobuf message to token use PublicKey.
// Encrypt encode a message use PublicKey.
func (r *Reflux) Encrypt(message proto.Message) (string, error) {
plainText, err := proto.Marshal(message)
plainText, err := r.codec.Marshal(message)
if err != nil {
return "", err
}
cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, r.pub, plainText)
if err != nil {
return "", err
}
return r.codec.EncodeToString(cipherText), nil
return r.codecString.EncodeToString(cipherText), nil
}

// Decrypt decodes token to a protobuf message.
// Decrypt decodes to a message use PrivateKey.
func (r *Reflux) Decrypt(tk string, message proto.Message) error {
cipherText, err := r.codec.DecodeString(tk)
cipherText, err := r.codecString.DecodeString(tk)
if err != nil {
return err
}
plainText, err := rsa.DecryptPKCS1v15(rand.Reader, r.priv, cipherText)
if err != nil {
return err
}
return proto.Unmarshal(plainText, message)
return r.codec.Unmarshal(plainText, message)
}

// Sign sign a protobuf message.
// Sign sign a message use PrivateKey.
func (r *Reflux) Sign(message proto.Message) (string, error) {
plainText, err := proto.Marshal(message)
plainText, err := r.codec.Marshal(message)
if err != nil {
return "", err
}
Expand All @@ -102,16 +117,70 @@ func (r *Reflux) Sign(message proto.Message) (string, error) {
if err != nil {
return "", err
}
return r.codec.EncodeToString(sighText), nil
return r.codecString.EncodeToString(sighText), nil
}

// Verify token a protobuf message signature.
// Verify a message signature use PubicKey.
func (r *Reflux) Verify(tk string, message proto.Message) error {
plainText, err := r.codec.Marshal(message)
if err != nil {
return err
}
sighText, err := r.codecString.DecodeString(tk)
if err != nil {
return err
}
hashed := sha256.Sum256(plainText)
return rsa.VerifyPKCS1v15(r.pub, crypto.SHA256, hashed[:], sighText)
}

// EncryptProto encode a protobuf message use PublicKey.
func (r *Reflux) EncryptProto(message proto.Message) (string, error) {
plainText, err := proto.Marshal(message)
if err != nil {
return "", err
}
cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, r.pub, plainText)
if err != nil {
return "", err
}
return r.codecString.EncodeToString(cipherText), nil
}

// DecryptProto decodes to a protobuf message use PrivateKey.
func (r *Reflux) DecryptProto(tk string, message proto.Message) error {
cipherText, err := r.codecString.DecodeString(tk)
if err != nil {
return err
}
plainText, err := rsa.DecryptPKCS1v15(rand.Reader, r.priv, cipherText)
if err != nil {
return err
}
return proto.Unmarshal(plainText, message)
}

// SignProto sign a protobuf message use PrivateKey.
func (r *Reflux) SignProto(message proto.Message) (string, error) {
plainText, err := proto.Marshal(message)
if err != nil {
return "", err
}
hashed := sha256.Sum256(plainText)
sighText, err := rsa.SignPKCS1v15(rand.Reader, r.priv, crypto.SHA256, hashed[:])
if err != nil {
return "", err
}
return r.codecString.EncodeToString(sighText), nil
}

// VerifyProto a protobuf message signature use PublicKey.
func (r *Reflux) VerifyProto(tk string, message proto.Message) error {
plainText, err := proto.Marshal(message)
if err != nil {
return err
}
sighText, err := r.codec.DecodeString(tk)
sighText, err := r.codecString.DecodeString(tk)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 11d67dd

Please sign in to comment.