Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java/Android中使用多个值生成SHA256哈希_Java_Android_Hash_Sha - Fatal编程技术网

在Java/Android中使用多个值生成SHA256哈希

在Java/Android中使用多个值生成SHA256哈希,java,android,hash,sha,Java,Android,Hash,Sha,我有两个对象,需要根据它们生成SHA256哈希 第一个值是一个JSONObject 第二个值是一个字符串变量 理想情况下,我需要的是 哈希=新哈希(JSONObject,String) 我找不到任何接受两个值的哈希生成方法 有人能帮我吗?SHA256使用字节数组作为输入。您需要将JSONObject和字符串转换为字节数组,然后在这些字节数组的串联上计算sha256哈希。使用键和值生成sha256哈希代码的正确方法 public static String hashMac(String te

我有两个对象,需要根据它们生成SHA256哈希

第一个值是一个JSONObject 第二个值是一个字符串变量

理想情况下,我需要的是

哈希=新哈希(JSONObject,String)

我找不到任何接受两个值的哈希生成方法


有人能帮我吗?

SHA256使用字节数组作为输入。您需要将JSONObject和字符串转换为字节数组,然后在这些字节数组的串联上计算sha256哈希。

使用键和值生成sha256哈希代码的正确方法

   public static String hashMac(String text, String secretKey)
                  throws SignatureException {

                 try {
                  Key sk = new SecretKeySpec(secretKey.getBytes(), HASH_ALGORITHM);
                  Mac mac = Mac.getInstance(sk.getAlgorithm());
                  mac.init(sk);
                  final byte[] hmac = mac.doFinal(text.getBytes());
                  return toHexString(hmac);
                 } catch (NoSuchAlgorithmException e1) {
                  // throw an exception or pick a different encryption method
                  throw new SignatureException(
                    "error building signature, no such algorithm in device "
                      + HASH_ALGORITHM);
                 } catch (InvalidKeyException e) {
                  throw new SignatureException(
                    "error building signature, invalid key " + HASH_ALGORITHM);
                 }
    }

    public static String toHexString(byte[] bytes) {  
            StringBuilder sb = new StringBuilder(bytes.length * 2);  

            Formatter formatter = new Formatter(sb);  
            for (byte b : bytes) {  
                formatter.format("%02x", b);  
            }  

            return sb.toString();  
        } 

这取决于您如何合并这两个值,也许可以使用StringBuilder。