【问题标题】:How to convert MD5 with BigInt MD5 Hash to String format in NodeJs with equivalent code from Java如何使用 Java 中的等效代码将具有 BigInt MD5 Hash 的 MD5 转换为 NodeJs 中的字符串格式
【发布时间】:2021-07-22 01:41:58
【问题描述】:

我在 Java 中有以下代码用于 MD5 哈希。我想用 Typescript 转换成等效的 NodeJ。

Java 代码

String secretKey = "MyID~Denmark";
final MessageDigest md = MessageDigest.getInstance("MD5")       final byte[] messageDigest = md.digest(secretKey.getBytes());
final BigInteger number = new BigInteger(messageDigest);
String value = String.format("%032x", number);
System.out.println("Value: "+value);

我也尝试过下面的 Typescript 代码,Java 和 NodeJs 的结果不相等。

public checkMd51(): void {
        const country: string = "Denmark";
        const projectId: string = "MyID";
        const secretKey: string = projectId + "~" + country;
        // const key: string = crypto.createHash("md5").update(String(secretKey)).digest();

        const md5 = crypto.createHash('md5').update(secretKey).digest('hex');

        const bigInt = BigInt(`0x${md5.substring(0, 32)}`);
        // const bigInt = BigInt(`0x${md5}`);
        // const val1 = util.format('%s:%s:%s', bigInt);

        console.log("MD5 value: ", val1.toString());

        
    }

请帮帮我。

【问题讨论】:

    标签: javascript java node.js typescript


    【解决方案1】:

    给你。以下代码相当于您在 java 中为 MD5 摘要编写的代码。

    const country: string = "Denmark";
    const projectId: string = "MyID";
    const secretKey: string = projectId + "~" + country;
    const md5 = crypto.createHash('md5').update(secretKey).digest('hex');
    console.log("MD5: ", md5);
    

    用 Typescript 和 NodeJs 运行 java 程序和上面的代码,你可以验证两者是否相等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-29
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      相关资源
      最近更新 更多