public static String hash(String s) { try { return new String(toHex(md5(s)).getBytes("UTF-8"), "UTF-8"); } catch (Exception e) { log.error("not supported charset...{}", e); return s; } }
时间: 2024-02-14 11:31:04 浏览: 25
这个`hash`函数用于计算给定字符串的哈希值,并返回对应的哈希字符串。
函数首先调用之前定义的`md5`函数来计算给定字符串的MD5哈希值。然后,它将MD5哈希值转换为十六进制字符串,通过调用`toHex(md5(s))`来实现。
接下来,函数调用`toHex(md5(s)).getBytes("UTF-8")`将十六进制字符串转换为字节数组,并使用UTF-8编码。
最后,函数通过调用`new String(...)`将字节数组转换回字符串,并使用UTF-8编码。如果在转换过程中出现异常,函数会捕获异常,并记录错误日志。
如果一切顺利,函数将返回计算得到的哈希字符串。如果出现异常,则返回原始输入字符串。
希望能解答您的问题!如果还有其他问题,请随时提问。
相关问题
public static String hash(String s) { try { String a = toHex(md5(s)); if (a != null) { return new String(a.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8); } else { return ""; } } catch (Exception e) { log.error("not supported charset...{}", e.getMessage()); return s; } } 方法md5可以为null吗
方法`md5`返回值可以为`null`,如果在`try`块中出现异常,或者`catch`块中的错误处理程序返回`null`,则该方法返回值为`null`。在`try`块中,如果`MessageDigest.getInstance("MD5")`或`algorithm.update(s.getBytes("UTF-8"))`抛出异常,那么`algorithm.digest()`将不会被调用,`messageDigest`将为`null`。在`catch`块中,如果出现异常,那么`md5`方法将返回`null`,因为`return null;`语句在`catch`块中。因此,需要在调用`md5`方法的地方进行必要的空值检查,以避免`NullPointerException`异常。
public static String hmacsha256(String plainStr, String key) { SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), "HmacSHA256"); Mac mac=null; try { mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); } catch (NoSuchAlgorithmException | InvalidKeyException e) { e.printStackTrace(); } byte digest[] = mac.doFinal(plainStr.getBytes(Charset.forName("UTF- 8"))); return new StringBuilder().append(byte2HexStr(digest)).toString(); } public static String byte2HexStr(byte array[]) { return array != null ? new String(Hex.encodeHex(array)) : null; } java to PHP
Here's the PHP equivalent of the Java code for computing HMAC-SHA256:
```
function hmacsha256($plainStr, $key) {
$secretKey = utf8_encode($key);
$digest = hash_hmac('sha256', utf8_encode($plainStr), $secretKey, true);
return bin2hex($digest);
}
```
Note that in PHP, the `hash_hmac` function is used to compute the HMAC with SHA256. The third parameter of `hash_hmac` is set to `true` to get the output as raw binary data. The `bin2hex` function is then used to convert the binary data to a hexadecimal string, which is the same format as the output of the `byte2HexStr` function in the Java code.
Also note that in PHP, strings are already represented as Unicode, so there's no need to convert the key and plain string to UTF-8 encoding using the `Charset.forName("UTF-8")` method.
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)