|
| 1 | +package crypt |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/base64" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + |
| 11 | + "github.com/pkg/errors" |
| 12 | + "golang.org/x/crypto/nacl/secretbox" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + KeySize = 32 |
| 17 | + NonceSize = 24 |
| 18 | +) |
| 19 | + |
| 20 | +type RandEnvelope struct { |
| 21 | + Nonce *[NonceSize]byte |
| 22 | + CipherText []byte |
| 23 | +} |
| 24 | + |
| 25 | +func OneWay(str string) string { |
| 26 | + enc := sha256.Sum256([]byte(str)) |
| 27 | + return base64.StdEncoding.EncodeToString(enc[:]) |
| 28 | +} |
| 29 | + |
| 30 | +func Encrypt(ekey string, data []byte) (string, error) { |
| 31 | + key, err := decodeKey(ekey) |
| 32 | + if err != nil { |
| 33 | + return "", errors.WithStack(err) |
| 34 | + } |
| 35 | + |
| 36 | + nonce, err := generateNonce() |
| 37 | + if err != nil { |
| 38 | + return "", errors.WithStack(err) |
| 39 | + } |
| 40 | + |
| 41 | + var cipherText []byte |
| 42 | + cipherText = secretbox.Seal(cipherText, data, nonce, key) |
| 43 | + |
| 44 | + envelope := RandEnvelope{ |
| 45 | + Nonce: nonce, |
| 46 | + CipherText: cipherText, |
| 47 | + } |
| 48 | + |
| 49 | + envelopeJson, err := json.Marshal(envelope) |
| 50 | + if err != nil { |
| 51 | + return "", errors.WithStack(err) |
| 52 | + } |
| 53 | + |
| 54 | + return base64.StdEncoding.EncodeToString(envelopeJson), nil |
| 55 | +} |
| 56 | + |
| 57 | +func Decrypt(ekey string, sealed string) ([]byte, error) { |
| 58 | + decoded, err := base64.StdEncoding.DecodeString(sealed) |
| 59 | + if err != nil { |
| 60 | + return nil, errors.WithStack(err) |
| 61 | + } |
| 62 | + |
| 63 | + var envelope RandEnvelope |
| 64 | + |
| 65 | + if err := json.Unmarshal(decoded, &envelope); err != nil { |
| 66 | + return nil, errors.WithStack(err) |
| 67 | + } |
| 68 | + |
| 69 | + key, err := decodeKey(ekey) |
| 70 | + if err != nil { |
| 71 | + return nil, errors.WithStack(err) |
| 72 | + } |
| 73 | + |
| 74 | + data := []byte{} |
| 75 | + |
| 76 | + data, ok := secretbox.Open(nil, envelope.CipherText, envelope.Nonce, key) |
| 77 | + if !ok { |
| 78 | + return nil, errors.WithStack(fmt.Errorf("could not decrypt data")) |
| 79 | + } |
| 80 | + |
| 81 | + return data, nil |
| 82 | +} |
| 83 | + |
| 84 | +func decodeKey(ekey string) (*[KeySize]byte, error) { |
| 85 | + var key [KeySize]byte |
| 86 | + |
| 87 | + data, err := base64.StdEncoding.DecodeString(ekey) |
| 88 | + if err != nil { |
| 89 | + return nil, errors.WithStack(err) |
| 90 | + } |
| 91 | + |
| 92 | + copy(key[:], data[0:KeySize]) |
| 93 | + |
| 94 | + return &key, nil |
| 95 | +} |
| 96 | + |
| 97 | +func generateNonce() (*[NonceSize]byte, error) { |
| 98 | + nonce := new([NonceSize]byte) |
| 99 | + _, err := io.ReadFull(rand.Reader, nonce[:]) |
| 100 | + if err != nil { |
| 101 | + return nil, errors.WithStack(err) |
| 102 | + } |
| 103 | + |
| 104 | + return nonce, nil |
| 105 | +} |
0 commit comments