Please help me to translate this Java code into a working Python code -> import…
- User
Please help me to translate this Java code into a working Python code -> import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException;
public static String POSTFIX = "20240227"; public static boolean succeed = true;
/* JADX INFO: Access modifiers changed from: private */ public String calcHash(String str) { String str2 = ""; if (!succeed) { return ""; } try { JSONObject parseObject = JSON.parseObject(str.replace("":", POSTFIX + "":")); if (parseObject.keySet().size() != 2) { return ""; } for (Object obj : parseObject.keySet().stream().sorted().toArray()) { String str3 = (String) obj; str2 = str2 + str3 + parseObject.get(str3).toString(); } try { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); messageDigest.update(str2.toLowerCase().getBytes(), 0, str2.length()); String bigInteger = new BigInteger(1, messageDigest.digest()).toString(16); while (bigInteger.length() < 32) { bigInteger = "0" + bigInteger; } return "HTB{" + bigInteger + "}"; } catch (NoSuchAlgorithmException unused) { return "Something is wrong, contact admin"; } } catch (JSONException unused2) { return ""; } }
- Assistant
Sure, I can help you translate the given Java code into Python code. Here's the equivalent Python code:
import hashlib import json POSTFIX = "20240227" succeed = True def calc_hash(input_str): result_str = "" if not succeed: return "" try: json_obj = json.loads(input_str.replace('":', POSTFIX + '":')) if len(json_obj.keys()) != 2: return "" sorted_keys = sorted(json_obj.keys()) for key in sorted_keys: result_str += key + str(json_obj[key]) md5_hash = hashlib.md5() md5_hash.update(result_str.lower().encode('utf-8')) big_integer = md5_hash.hexdigest() while len(big_integer) < 32: big_integer = "0" + big_integer return f"HTB{{{big_integer}}}" except json.JSONDecodeError: return "" # Example usage # input_str = '{"example_key1":"example_value1","example_key2":"example_value2"}' # print(calc_hash(input_str)) - User
i want 'input_str' to be a file name which is on my disk kotlin-tooling-metadata.json . so code needs to be loading this file instead please.
- Assistant
Sure, let's modify the
calc_hashfunction so that it reads from a file rather than directly taking the JSON string as input. This will allow us to read the contents ofkotlin-tooling-metadata.jsonand process it.Here is the complete modified code:
import hashlib import json POSTFIX = "20240227" succeed = True def calc_hash(file_name): result_str = "" if not succeed: return "" try: with open(file_name, 'r') as file: input_str = file.read() json_obj = json.loads(input_str.replace('":', POSTFIX + '":')) if len(json_obj.keys()) != 2: return "" sorted_keys = sorted(json_obj.keys()) for key in sorted_keys: result_str += key + str(json_obj[key]) md5_hash = hashlib.md5() md5_hash.update(result_str.lower().encode('utf-8')) big_integer = md5_hash.hexdigest() while len(big_integer) < 32: big_integer = "0" + big_integer return f"HTB{{{big_integer}}}" except FileNotFoundError: return "File not found" except json.JSONDecodeError: return "" # Example usage file_name = 'kotlin-tooling-metadata.json' print(calc_hash(file_name))