我在PHP和Android中都生成SHA1密钥以验证文件。但是我为PHP和Android获得了不同的密钥。
安卓:
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] buffer = new byte[65536];
InputStream fis = new FileInputStream(downloadFile.getPath());
int n = 0;
while (n != -1) {
n = fis.read(buffer);
if (n > 0) {
digest.update(buffer, 0, n);
}
}
fis.close();
byte[] digestResult = digest.digest();
log("CheckSum : " + byteArray2Hex(digestResult));
} catch (Exception e) {
log("Exception : " + e.getLocalizedMessage());
}
Run Code Online (Sandbox Code Playgroud)
PHP:
echo ' \nSHA1 File hash of '. $filePath . ': ' . sha1_file($filePath);
Run Code Online (Sandbox Code Playgroud)
校验和输出:
PHP SHA1校验和:e7a91cd4127149a230f3dcb5ae81605615d3e1be Android SHA1校验和:19bcbd9d18a3880d2375bddb9181d75da3f32da0
谁能帮忙解决这个问题。
从这个答案: https: //stackoverflow.com/a/9855338/3393666考虑使用这个 byteArray2Hex 函数:
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String byteArray2Hex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
Run Code Online (Sandbox Code Playgroud)
我已经在 java 1.7 与 PHP 7 和使用 SDK 23 编译的 Android 5.0 上进行了测试。希望这会有所帮助。
| 归档时间: |
|
| 查看次数: |
242 次 |
| 最近记录: |