Skip to content

Commit

Permalink
Make only Marshal and Unmarshal public
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshatM committed Oct 29, 2020
1 parent d3f5886 commit c60b800
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
10 changes: 5 additions & 5 deletions crypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func init() {
}

// can only support strings.
func Encrypt(stringToEncrypt string) (string, error) {
func encrypt(stringToEncrypt string) (string, error) {
// validate we aren't passing in an already encrypted string!
if IsFernetToken([]byte(stringToEncrypt)) {
if isFernetToken([]byte(stringToEncrypt)) {
return stringToEncrypt, nil
}

Expand All @@ -35,17 +35,17 @@ func Encrypt(stringToEncrypt string) (string, error) {
}

// can only support strings
func Decrypt(encryptedString string) string {
func decrypt(encryptedString string) string {

// validate we aren't passing in an already decrypted string!
if !IsFernetToken([]byte(encryptedString)) {
if !isFernetToken([]byte(encryptedString)) {
return encryptedString
}

return string(fernet.VerifyAndDecrypt([]byte(encryptedString), 0, fernet.MustDecodeKeys(encoded_key)))
}

func IsFernetToken(token []byte) bool {
func isFernetToken(token []byte) bool {
msg := fernet.VerifyAndDecrypt(token, 0, fernet.MustDecodeKeys(encoded_key))
return msg != nil
}
21 changes: 8 additions & 13 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package privacy

import (
"fmt"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)

type Mode int

const (
EncryptMode Mode = iota
DecryptMode
encryptMode Mode = iota
decryptMode
)

// `recursiveEncrypt` examines all the field values of a message, and encrypts / decrypts them.
// `recursiveCrypt` examines all the field values of a message, and encrypts / decrypts them.
func recursiveCrypt(raw_message proto.Message, mode Mode) {

protomessage := raw_message.ProtoReflect()
Expand All @@ -31,18 +30,14 @@ func recursiveCrypt(raw_message proto.Message, mode Mode) {
} else {
switch fd.Kind() {
case protoreflect.StringKind:

// TODO: Replace with a call to privacy.Encrypt
if mode == EncryptMode {
new_value, err := Encrypt(value.String())
if mode == encryptMode {
new_value, err := encrypt(value.String())
if err != nil {
panic(err)
}
fmt.Println(fmt.Sprintf("Encrypted new_value is %+v", new_value))
protomessage.Set(fd, protoreflect.ValueOf(new_value))
} else {
new_value := Decrypt(value.String())
fmt.Println(fmt.Sprintf("Decrypted new_value is %+v", new_value))
new_value := decrypt(value.String())
protomessage.Set(fd, protoreflect.ValueOf(new_value))
}
case protoreflect.MessageKind:
Expand All @@ -61,13 +56,13 @@ func recursiveCrypt(raw_message proto.Message, mode Mode) {
func Unmarshal(b []byte, m proto.Message) error {
err := proto.Unmarshal(b, m)
if err == nil {
recursiveCrypt(m, EncryptMode)
recursiveCrypt(m, encryptMode)
}
return err
}

// Marshal returns the wire-format encoding of m.
func Marshal(m proto.Message) ([]byte, error) {
recursiveCrypt(m, DecryptMode)
recursiveCrypt(m, decryptMode)
return proto.Marshal(m)
}

0 comments on commit c60b800

Please sign in to comment.