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

25 lines
624 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")
}
// Split nonce and ciphertext.
nonce, ciphertext, tag := ciphertextMsg[:nonceSize], ciphertextMsg[nonceSize:len(ciphertextMsg)-tagSize], ciphertextMsg[len(ciphertextMsg)-tagSize:]
return aead.Open(nil, nonce, append(ciphertext, tag...), nil)
}