转成php public static void main(String[] args) throws NoSuchAlgorithmException { String transId = "6FEB23ACB0374985A2A52D282EDD5361u6643"; String mKey = "2426177315863edd099aa9ba13e6a996"; String checkSignRaw = mKey + ":" + transId; MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(checkSignRaw.getBytes(StandardCharsets.UTF_8)); String sign = bytesToHex(hash); System.out.println(sign); } private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray(); private static String bytesToHex(byte[] bytes) { char[] hexChars = new char[bytes.length * 2]; for (int i = 0; i < bytes.length; i++) { int v = bytes[i] & 0xff; hexChars[i * 2] = HEX_ARRAY[v >>> 4]; hexChars[i * 2 + 1] = HEX_ARRAY[v & 0x0f]; } return new String(hexChars); }
转成php public static void main(String[] args) throws NoSuchAlgorithmException { String transId = "6FEB23ACB0374985A2A52D282EDD5361u6643"; String mKey = "2426177315863edd099aa9ba13e6a996"; String checkSignRaw = mKey + ":" + transId; MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(checkSignRaw.getBytes(StandardCharsets.UTF_8)); String sign = bytesToHex(hash); System.out.println(sign); } private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray(); private static String bytesToHex(byte[] bytes) { char[] hexChars = new char[bytes.length * 2]; for (int i = 0; i < bytes.length; i++) { int v = bytes[i] & 0xff; hexChars[i * 2] = HEX_ARRAY[v >>> 4]; hexChars[i * 2 + 1] = HEX_ARRAY[v & 0x0f]; } return new String(hexChars); }
转成php public static void main(String[] args) throws NoSuchAlgorithmException {
String transId = "6FEB23ACB0374985A2A52D282EDD5361u6643";
String mKey = "2426177315863edd099aa9ba13e6a996";
String checkSignRaw = mKey + ":" + transId;
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(checkSignRaw.getBytes(StandardCharsets.UTF_8));
String sign = bytesToHex(hash);
System.out.println(sign);
}
private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray();
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int i = 0; i < bytes.length; i++) {
int v = bytes[i] & 0xff;
hexChars[i * 2] = HEX_ARRAY[v >>> 4];
hexChars[i * 2 + 1] = HEX_ARRAY[v & 0x0f];
}
return new String(hexChars);
}

用户500297581
2023年10月21日
|
浏览 168
这段代码是用 Java 编写的。如果您想将其转换为 PHP 代码,可以按照以下方式进行转换:
复制代码
<?php
function main() {
$transId = "6FEB23ACB0374985A2A52D282EDD5361u6643";
$mKey = "2426177315863edd099aa9ba13e6a996";
$checkSignRaw = $mKey . ":" . $transId;
$hash = hash('sha256', $checkSignRaw);
$sign = bytesToHex($hash);
echo $sign;
}
function bytesToHex($bytes) {
$hexChars = "";
foreach (str_split($bytes) as $byte) {
$v = ord($byte);
$hexChars .= HEX_ARRAY[$v >> 4] . HEX_ARRAY[$v & 0x0f];
}
return $hexChars;
}
main();
?>
请注意,上述代码只是一个示例,并可能需要根据您的实际需求进行调整。
我要提问
复制内容
分享给好友
AI编程问答网 免责声明:
以上内容除特别注明外均来源于网友提问,AI编程问答网回答,权益归原著者所有;
下一篇:如何部署源码
