def test_gen_password_too_long(self): username = "******" secret = b"FFF" file_password = b"A" * 67 with self.assertRaises(ValueError): crypto.gen_user_password(username, secret, file_password)
def test_gen_user_password_invalid_secret_len(self): username = "******" secret = b"FFFFF" scrambled_password = b"A" * 5 with self.assertRaises(ValueError): crypto.gen_user_password(username, secret, scrambled_password)
def gen_password_main(): parser = argparse.ArgumentParser( description='Generate admin tokens for a given username') parser.add_argument('--secret-len', type=int, help='number of bytes in the password secret', default=SECRET_LEN) parser.add_argument('--username-len', type=int, help='username length', default=USERNAME_LEN) parser.add_argument('zip_password', help='hexified zip password') parser.add_argument('username', help='username (read by stdin by default)', nargs='?', default='-') args = parser.parse_args() if args.username == '-': user = sys.stdin.readline().strip() else: user = args.username if len(user) != args.username_len: raise ValueError("Invalid username length") secret = os.urandom(args.secret_len) print(gen_user_password(user, secret, bytes.fromhex(args.zip_password)))
def gen_password_main(): parser = argparse.ArgumentParser( description="Generate admin tokens for a given username" ) parser.add_argument( "--secret-len", type=int, help="number of bytes in the password secret", default=SECRET_LEN, ) parser.add_argument( "--username-len", type=int, help="username length", default=USERNAME_LEN ) parser.add_argument("zip_password", help="hexified zip password") parser.add_argument( "username", help="username (read by stdin by default)", nargs="?", default="-" ) args = parser.parse_args() if args.username == "-": user = sys.stdin.readline().strip() else: user = args.username if len(user) != args.username_len: raise ValueError("Invalid username length") secret = os.urandom(args.secret_len) print(gen_user_password(user, secret, bytes.fromhex(args.zip_password)))
def test_recover_file_password_from_token(self): username = "******" secret = b"FFF" file_password = b"A" * 7 token = crypto.gen_user_password(username, secret, file_password) self.assertEqual(crypto.recover_file_password_from_token(token), file_password)
def test_extract_bad_zip(self): self.tempdir = Utils.new_tmp_dir() enc_path = os.path.join(self.tempdir, "pack.zip.enc") dec_path = os.path.join(self.tempdir, "pack.zip") Config.encrypted_file = enc_path Config.decrypted_file = dec_path with open(enc_path, "wb") as f: invalid_zip = b"this is not a zip" encrypted = crypto.encode(b"fooobar", invalid_zip, b"metadata") f.write(encrypted) password = crypto.gen_user_password("XXXXXX", b"YYY", b"fooobar") with self.assertRaises(Forbidden) as e: ContestManager.extract_contest(password)
def test_gen_user_password(self): username = "******" secret = b"SEC" file_password = b"PASSWOR" token = crypto.gen_user_password(username, secret, file_password) new_secret, encoded_password = crypto.decode_data( token[len(username) + 1:], len(secret)) self.assertEqual( file_password, crypto.recover_file_password(username, new_secret, encoded_password))
def test_gen_password_max_len(self): username = "******" secret = b"FFF" file_password = b"A" * 62 crypto.gen_user_password(username, secret, file_password)