UnixTimePrefixedRandomNonce takes an int for the nonce size and returns a byte slice of length size. A byte slice is created for the nonce and filled with random data from `crypto/rand`, then the first 4 bytes of the nonce are overwritten with LittleEndian encoding of `time.Now().Unix()` The purpose
(size int)
| 15 | // The purpose of this function is to avoid an unlikely collision in randomly generating nonces |
| 16 | // by prefixing the nonce with time series data. |
| 17 | func UnixTimePrefixedRandomNonce(size int) []byte { |
| 18 | nonce := make([]byte, size) |
| 19 | rand.Read(nonce) |
| 20 | timeBytes := make([]byte, 8) |
| 21 | binary.LittleEndian.PutUint64(timeBytes, uint64(time.Now().Unix())) |
| 22 | copy(nonce, timeBytes[:4]) |
| 23 | return nonce |
| 24 | } |
| 25 | |
| 26 | func encrypt(data []byte, key []byte) []byte { |
| 27 | block, _ := aes.NewCipher(key) |