我在将纯java Curve25519函数转换为Python等效函数时遇到问题,具体问题与将哈希字符串转换为等效字节的摘要函数有关,java实现:
数据示例:
sP=“这是用于测试的密码短语示例”/**
* Calculate the SHA-256 hash of a string
*
* @param input Data to be hashed
* @return The hash digest
*/
public static byte[] singleDigest(String input) {
byte[] bytes;
try {
bytes = singleDigest(input.getBytes("UTF-8"));
System.out.println("bytes"+bytes);
System.out.println("before singleDigest"+input);
} catch (UnsupportedEncodingException exc) {
bytes = null;
}
return bytes;
}
/**
* Calculate the SHA-256 hash of a byte array
*
* @param input Data to be hashed
* @return The hash digest
*/
public static byte[] singleDigest(byte[] input) {
byte[] bytes;
synchronized (digest) {
digest.reset();
bytes = digest.digest(input);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
sb.append(String.format("%02x", bytes[i] & 0xFF));
}
System.out.println(sb.toString());
}
return bytes;
}
生成此字节输出:
字节输出:82、-57、124、58、-105、76、123、3、119、-21、121、71、-54、73、-75、54、31、-33、-49、-68、-16、-19、125、-61、-116、-82、96、50、-35、-119、-28、25
对于正数和负数,在python端,我使用此函数将散列字符串转换为字节:
^{pr2}$
我在这里使用列表而不是可变的bytearray,因为bytearray的内部表示(0-256),以及一些散列字符串的输出
不同且仅为正整数:
82、199、124、58、151、76、123、3、119、235、121、71、202、73、181、54、31、217、207、188、240、237、125、196、140、174、96、50、221、137、228、25
我注意到(256-JavaByteOutput[x]==PythonByteOtput[x])当JavaByteOutput为负数时,问题是,如何修改ParseHexString类,使其具有相等的正/负输出,并且我想要纯python代码而不需要库。在
谢谢你的回答。
当做
亚历克斯