def stdin_mode(args): salt = args.salt needs_salt = True if salt is None else False data = [] try: while True: content = input() if content == '': break plaintext = content.strip().encode('utf8') if needs_salt: salt = gen_salt(HASH_LEN) iterations = args.iterations hash_res = SCRAMSHA1(plaintext, salt, iterations) final = hash_format(hash_res, salt, iterations, mode=args.format) if args.output_file is None: output_data([final]) else: data.append(final) except EOFError: pass except KeyboardInterrupt: pass except Exception as e: print(e, file=sys.stderr) finally: if args.output_file: with open(args.output_file, 'w') as file: output_data(data, file=file)
def single_mode(args): # gen salt if not given if args.salt is None: salt = gen_salt(HASH_LEN) else: salt = args.salt plaintext = args.plaintext.encode('utf8') iterations = args.iterations hash_res = SCRAMSHA1(plaintext, salt, iterations) data = [hash_format(hash_res, salt, iterations, mode=args.format)] output_data(data, file=args.output_file)
def file_mode(args): with open(args.input_file, 'r') as input_file: data = [] for line in input_file: if args.salt is None: salt = gen_salt(HASH_LEN) else: salt = args.salt plaintext = line.strip().encode('utf8') iterations = args.iterations hash_res = SCRAMSHA1(plaintext, salt, iterations) data.append( hash_format(hash_res, salt, iterations, mode=args.format)) output_data(data, file=args.output_file)
def test_non_bytes_plaintext(): with pytest.raises(TypeError): SCRAMSHA1('not bytes', salt, iterations)
def test_consecutive_hashes(): result = SCRAMSHA1(plaintext, salt, iterations) for i in range(10): result2 = SCRAMSHA1(plaintext, salt, iterations) assert result == result2
def test_valid_input(): result = SCRAMSHA1(plaintext, salt, iterations) assert result.hex() == 'e9d94660c39d65c38fbad91c358f14da0eef2bd6'
def test_non_int_iterations(): with pytest.raises(TypeError): SCRAMSHA1(plaintext, salt, .1)
def test_zero_iterations(): with pytest.raises(ValueError): SCRAMSHA1(plaintext, salt, 0)
def test_negative_iterations(): with pytest.raises(ValueError): SCRAMSHA1(plaintext, salt, -1)
def test_non_b64_salt(): with pytest.raises(ValueError): SCRAMSHA1(plaintext, 'im a bad salt', iterations)