import sys
import json
import bcrypt
import binascii

# Read input JSON from stdin
input_data = json.load(sys.stdin)
password = input_data.get("password")
cost = int(input_data.get("cost", "10")) # Default cost 10

if not password:
    print(json.dumps({"error": "Password is required"}), file=sys.stderr)
    sys.exit(1)

# Generate bcrypt hash
hashed_bytes = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt(rounds=cost))
hashed_string = hashed_bytes.decode('utf-8')

# Hex encode for Glauth
hex_encoded_hash = binascii.hexlify(hashed_string.encode('utf-8')).decode('utf-8')

# Output JSON to stdout
output_data = {
    "bcrypt_hash": hashed_string,
    "hex_encoded_bcrypt_hash": hex_encoded_hash
}
print(json.dumps(output_data))