SHA1 Checksum在php和android中对同一个文件有所不同
发布时间:2020-12-13 22:25:25 所属栏目:PHP教程 来源:网络整理
导读:我在 PHP和 Android中生成SHA1密钥以验证文件.但是我为PHP和Android获得了不同的密钥. Android: try { MessageDigest digest = MessageDigest.getInstance("SHA-1"); byte[] buffer = new byte[65536]; InputStream fis = new FileInputStream(downloadFile
我在
PHP和
Android中生成SHA1密钥以验证文件.但是我为PHP和Android获得了不同的密钥.
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,n); } } fis.close(); byte[] digestResult = digest.digest(); log("CheckSum : " + byteArray2Hex(digestResult)); } catch (Exception e) { log("Exception : " + e.getLocalizedMessage()); } PHP: echo ' nSHA1 File hash of '. $filePath . ': ' . sha1_file($filePath); 校验和输出: PHP SHA1 CheckSum:e7a91cd4127149a230f3dcb5ae81605615d3e1be 任何人都可以帮助如何处理这个. 解决方法
从这个SO答案:
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); } 我已经在java 1.7和PHP 7以及使用SDK 23编译的Android 5.0上进行了测试.希望这可以帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |