freematics-traccar-encrypted/server/encryption/decrypt.go

26 lines
680 B
Go
Raw Normal View History

2024-06-27 17:16:47 -06:00
package encryption
import (
"errors"
"golang.org/x/crypto/chacha20poly1305"
)
func Decrypt(key, ciphertextMsg []byte) ([]byte, error) {
aead, err := chacha20poly1305.New(key)
if err != nil {
return nil, err
}
nonceSize := aead.NonceSize()
tagSize := aead.Overhead()
if len(ciphertextMsg) < nonceSize+tagSize {
return nil, errors.New("ciphertext too short")
}
2024-06-27 22:35:28 -06:00
// Split the message apart.
// The order is nonce, ciphertext, and tag. The last two aren't used.
nonce, _, _ := ciphertextMsg[:nonceSize], ciphertextMsg[nonceSize:len(ciphertextMsg)-tagSize], ciphertextMsg[len(ciphertextMsg)-tagSize:]
2024-06-27 17:16:47 -06:00
2024-06-27 22:35:28 -06:00
return aead.Open(nil, nonce, ciphertextMsg[nonceSize:], nil)
2024-06-27 17:16:47 -06:00
}