在php和android中,同一文件的SHA1校验和变得不同
我正在PHP和Android中生成SHA1密钥以验证该文件。但是我得到了PHP和Android的不同密钥 安卓:在php和android中,同一文件的SHA1校验和变得不同,php,android,Php,Android,我正在PHP和Android中生成SHA1密钥以验证该文件。但是我得到了PHP和Android的不同密钥 安卓: try { MessageDigest digest = MessageDigest.getInstance("SHA-1"); byte[] buffer = new byte[65536]; InputStream fis = new FileInputStream(downloadFile.getPath());
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());
}
PHP:
校验和输出:
PHP SHA1校验和:E7A91CD4127149A230F3DCB5AE8160565D3E1BE
Android SHA1校验和:19BCBD9D18A3880D2375BDB9181D75DA3F32DA0
<> P>可以从这个答案中考虑:使用这个BytRay2HEX函数:
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);
}
final protected static char[]hexArray=“0123456789ABCDEF.tocharray();
公共静态字符串byteArray2Hex(字节[]字节){
char[]hexChars=新字符[bytes.length*2];
对于(int j=0;j>>4];
hexChars[j*2+1]=hexArray[v&0x0F];
}
返回新字符串(hexChars);
}
我已经在Java1.7、PHP7和用SDK23编译的Android5.0上对其进行了测试。
希望这有帮助。您能为我们提供这两个版本的密码输出吗?我非常确定Java/Android会输出原始散列。我已经更新了Android和phpAre的校验和输出。我们绝对确定输入数据是相同的?是的,我只是在移动设备中复制并粘贴相同的文件,以便checking@Kamal我已经在本地环境中测试了您的代码,并用java 1.7和php 7编译和运行,我得到了相同的sha1输出,请参见:。您可以为
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);
}