From b278230cdb1719f5bc123966de19b6e659857149 Mon Sep 17 00:00:00 2001 From: cj-chua <43709958+cj-chua@users.noreply.github.com> Date: Wed, 22 Feb 2023 17:35:39 -0600 Subject: [PATCH] Nip04 decrypt (#65) Co-authored-by: cj-ibex --- nip04/nip04.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/nip04/nip04.go b/nip04/nip04.go index fc4cca5..0361cee 100644 --- a/nip04/nip04.go +++ b/nip04/nip04.go @@ -101,8 +101,17 @@ func Decrypt(content string, key []byte) (string, error) { mode.CryptBlocks(plaintext, ciphertext) // remove padding - padding := int(plaintext[len(plaintext)-1]) // the padding amount is encoded in the padding bytes themselves - message := string(plaintext[0 : len(plaintext)-padding]) + var ( + message = string(plaintext) + plaintextLen = len(plaintext) + ) + if plaintextLen > 0 { + padding := int(plaintext[plaintextLen-1]) // the padding amount is encoded in the padding bytes themselves + if padding > plaintextLen { + return "", fmt.Errorf("Invalid padding amount: %d. \n", padding) + } + message = string(plaintext[0 : plaintextLen-padding]) + } return message, nil }