commit 8e81ae4dcfc23c2bc623a268c42b83eef0c6fe30
parent 5e8732cb33eb4cb54246b6a3c397f618fed7fcfd
Author: Oscar Benedito <oscar@oscarbenedito.com>
Date:   Mon, 17 Aug 2020 00:10:30 +0200

Add main function to hex-base64

Diffstat:
Mhex-base64-translator/hex-base64-translator.py | 47++++++++++++++++++++++++++---------------------
1 file changed, 26 insertions(+), 21 deletions(-)

diff --git a/hex-base64-translator/hex-base64-translator.py b/hex-base64-translator/hex-base64-translator.py @@ -17,6 +17,7 @@ import sys from getpass import getpass + def char_to_bits(c): n = ord(c) if n >= 65 and n <= 90: @@ -32,6 +33,7 @@ def char_to_bits(c): else: sys.exit('Error, ' + c + ' is not a Base64 character.', file=sys.stderr) + def bits_to_char(s): n = int(s, 2) if n < 26: @@ -47,6 +49,7 @@ def bits_to_char(s): else: sys.exit('Error, ' + s + ' (' + str(n) + ') is not a binary number lower than 64.', file=sys.stderr) + def base64_to_hex(s): if len(s) % 2: print('WARNING: Number of Base64 characters is not multiple of 2. Adding zeros to string.', file=sys.stderr) @@ -62,6 +65,7 @@ def base64_to_hex(s): return ret + def hex_to_base64(s): if len(s) % 3: print('WARNING: Number of hexadecimal values is not a multiple of 3. Adding zeros to string.', file=sys.stderr) @@ -77,26 +81,27 @@ def hex_to_base64(s): return ret -if len(sys.argv) != 2 or (sys.argv[1] != 'base64-to-hex' and sys.argv[1] != 'hex-to-base64'): - sys.exit('Usage: ' + sys.argv[0] + ' base64-to-hex | hex-to-base64.') - -if sys.argv[1] == 'base64-to-hex': - inp = getpass(prompt = 'Base64 secret: ') - print('') - out = base64_to_hex(inp) - print('-'*80) - print('Secret in hexadecimal:', out) - print('-'*80) - -elif sys.argv[1] == 'hex-to-base64': - inp = getpass(prompt = 'Hexadecimal secret: ') - print('') - out = hex_to_base64(inp) - print('-'*80) - print('Secret in Base64:', out) - print('-'*80) - if inp[0] == '0' and len(inp) % 2 == 0: - out = hex_to_base64(inp[1:]) +if __name__ == '__main__': + if len(sys.argv) != 2 or (sys.argv[1] != 'base64-to-hex' and sys.argv[1] != 'hex-to-base64'): + sys.exit('Usage: ' + sys.argv[0] + ' base64-to-hex | hex-to-base64.') + + if sys.argv[1] == 'base64-to-hex': + inp = getpass(prompt = 'Base64 secret: ') + print('') + out = base64_to_hex(inp) + print('-'*80) + print('Secret in hexadecimal:', out) + print('-'*80) + + elif sys.argv[1] == 'hex-to-base64': + inp = getpass(prompt = 'Hexadecimal secret: ') + print('') + out = hex_to_base64(inp) print('-'*80) - print('Due to SSSS having an output with an even number of characters, your secret could be:', out) + print('Secret in Base64:', out) print('-'*80) + if inp[0] == '0' and len(inp) % 2 == 0: + out = hex_to_base64(inp[1:]) + print('-'*80) + print('Due to SSSS having an output with an even number of characters, your secret could be:', out) + print('-'*80)