我想要在Swift中使用数据值的十六进制表示.
最终我想要像这样使用它:
let data = Data(base64Encoded: "aGVsbG8gd29ybGQ=")!
print(data.hexString)
Run Code Online (Sandbox Code Playgroud) 我对Swift相对较新,而且我使用HMAC和SHA1进行加密.我找到了以下答案/sf/answers/1708806571/但我无法弄清楚如何正确实现这一点.任何帮助都会很棒.
我正在尝试使用Java来散列Java中的String ripemd160来模拟以下php的输出:
$string = 'string';
$key = 'test';
hash_hmac('ripemd160', $string, $key);
// outputs: 37241f2513c60ae4d9b3b8d0d30517445f451fa5
Run Code Online (Sandbox Code Playgroud)
尝试1
最初我尝试使用以下方法模拟它...但是我不相信它可以ripemd160用作getInstance`算法吗?
或许它是,我只是没有在本地启用它?
public String signRequest(String uri, String secret) {
try {
byte[] keyBytes = secret.getBytes();
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
Mac mac = Mac.getInstance("ripemd160");
mac.init(signingKey);
// Compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(uri.getBytes());
// Convert raw bytes to Hex
byte[] hexBytes = new Hex().encode(rawHmac);
// Covert array of Hex bytes to a String
return new String(hexBytes, "UTF-8"); …Run Code Online (Sandbox Code Playgroud)