mirror of
https://github.com/lnbits/lnbits.git
synced 2025-10-11 04:52:34 +02:00
feat: add urlsafe enc to lnbits-cli (#3173)
This commit is contained in:
@@ -518,12 +518,15 @@ def encrypt_macaroon():
|
|||||||
|
|
||||||
@encrypt.command("aes")
|
@encrypt.command("aes")
|
||||||
@click.option("-p", "--payload", required=True, help="Payload to encrypt.")
|
@click.option("-p", "--payload", required=True, help="Payload to encrypt.")
|
||||||
def encrypt_aes(payload: str):
|
@click.option(
|
||||||
|
"-u", "--urlsafe", is_flag=True, required=False, help="Urlsafe b64encode."
|
||||||
|
)
|
||||||
|
def encrypt_aes(payload: str, urlsafe: bool = False):
|
||||||
"""AES encrypts a payload"""
|
"""AES encrypts a payload"""
|
||||||
key = getpass("Enter encryption key: ")
|
key = getpass("Enter encryption key: ")
|
||||||
aes = AESCipher(key.encode())
|
aes = AESCipher(key.encode())
|
||||||
try:
|
try:
|
||||||
encrypted = aes.encrypt(payload.encode())
|
encrypted = aes.encrypt(payload.encode(), urlsafe=urlsafe)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
click.echo(f"Error encrypting payload: {ex}")
|
click.echo(f"Error encrypting payload: {ex}")
|
||||||
return
|
return
|
||||||
@@ -533,12 +536,15 @@ def encrypt_aes(payload: str):
|
|||||||
|
|
||||||
@decrypt.command("aes")
|
@decrypt.command("aes")
|
||||||
@click.option("-p", "--payload", required=True, help="Payload to decrypt.")
|
@click.option("-p", "--payload", required=True, help="Payload to decrypt.")
|
||||||
def decrypt_aes(payload: str):
|
@click.option(
|
||||||
|
"-u", "--urlsafe", is_flag=True, required=False, help="Urlsafe b64decode."
|
||||||
|
)
|
||||||
|
def decrypt_aes(payload: str, urlsafe: bool = False):
|
||||||
"""AES decrypts a payload"""
|
"""AES decrypts a payload"""
|
||||||
key = getpass("Enter encryption key: ")
|
key = getpass("Enter encryption key: ")
|
||||||
aes = AESCipher(key.encode())
|
aes = AESCipher(key.encode())
|
||||||
try:
|
try:
|
||||||
decrypted = aes.decrypt(payload)
|
decrypted = aes.decrypt(payload, urlsafe=urlsafe)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
click.echo(f"Error decrypting payload: {ex}")
|
click.echo(f"Error decrypting payload: {ex}")
|
||||||
return
|
return
|
||||||
|
@@ -93,9 +93,14 @@ class AESCipher:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
decrypted_bytes = aes.decrypt(encrypted_bytes)
|
decrypted_bytes = aes.decrypt(encrypted_bytes)
|
||||||
return self.unpad(decrypted_bytes).decode()
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
raise ValueError("Decryption error") from exc
|
raise ValueError("Decryption error: could not decrypt") from exc
|
||||||
|
|
||||||
|
unpadded = self.unpad(decrypted_bytes)
|
||||||
|
if len(unpadded) == 0:
|
||||||
|
raise ValueError("Decryption error: unpadding failed")
|
||||||
|
|
||||||
|
return unpadded.decode()
|
||||||
|
|
||||||
def encrypt(self, message: bytes, urlsafe: bool = False) -> str:
|
def encrypt(self, message: bytes, urlsafe: bool = False) -> str:
|
||||||
"""
|
"""
|
||||||
|
Reference in New Issue
Block a user