extract 12 byte nonce from the data and deseal the data
(Key []byte, Data []byte)
| 42 | |
| 43 | // extract 12 byte nonce from the data and deseal the data |
| 44 | func DecryptWithKey(Key []byte, Data []byte) (result []byte, err error) { |
| 45 | |
| 46 | // make sure data is atleast 28 byte, 16 bytes of AEAD cipher and 12 bytes of nonce |
| 47 | if len(Data) < 28 { |
| 48 | err = fmt.Errorf("Invalid data") |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | data_without_nonce := Data[0 : len(Data)-chacha20poly1305.NonceSize] |
| 53 | |
| 54 | nonce := Data[len(Data)-chacha20poly1305.NonceSize:] |
| 55 | |
| 56 | cipher, err := chacha20poly1305.New(Key) |
| 57 | if err != nil { |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | return cipher.Open(result[:0], nonce, data_without_nonce, nil) // result buffer should be different |
| 62 | |
| 63 | } |
| 64 | |
| 65 | // use master keys, everytime required |
| 66 | func (w *Wallet_Memory) Encrypt(Data []byte) (result []byte, err error) { |