来自java的swift cryptokit sha512散列匹配问题

l0oc07j2  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(447)

java到swift哈希检查
我请求帮助理解如何比较javasha512散列和swiftsha512散列。
我从一个秘密字符串开始。使用aes.gcm对字符串进行加密,然后将生成的字节数组base64编码为字符串,以便在java中传输。
为了验证到达时的数据,我计算了加密字符串的散列,并将其包含在数据包中。
当它到达swift代码时,我得到加密字符串的数据表示。然后计算该字符串的哈希值。我想验证哈希值是否相同。
java代码:

// Represents a encryption function
byte[] encryptedData = someCrypt.doEncryption(dataToEncrypt);

// Encode the encrypted data for transport
String stringToTransport = Base64.toBase64String(encryptedData);

// Compute a hash of that data to verify
MessageDigest hash = MessageDigest.getInstance("SHA512", "BCFIPS");
byte[] hashBytes = hash.digest(encryptedData);
String stringRepresentationOfHash = Base64.toBase64String(hashBytes)

字符串输出示例:

"SRxWiA9S/TC4XrFumkUXp61okvoawTKTtD9Y8Pc+xJEANIYcrUnbaNWaK/R/Tr4ZoNXHXX1gpjRHnIsyLTFKIw=="

银行代码

// Get the string of the encrypted data from an import
let encryptedString: String = dataImported.encryptedDataStringRepresentation

// Convert the string to data object
if let encryptedStringData = Data(base64Encoded: encryptedString, options: NSData.Base64DecodingOptions(rawValue: 0)) {
    // Compute the hash
    let hash = SHA512.hash(data: encryptedStringData)
    // Get the Base64 string representation
    let stringRepresentationOfHash = hash.dataRepresentation.base64EncodedString()
}

字符串输出示例:

"AAAAICAAAAAKAAAAAAAAAAAAAAAAAAAA4OiP6v5/AAAAAAAgIAAAACgAAAAAAAAAlABeJq33UpRAAAAAAAAAAA=="

有没有人能向我解释我错了什么,我确信这是一个编码问题。我试过在swift中将字符串输入转换成字节数组。

if let encryptedStringData = Data(base64Encoded: encryptedString, options: NSData.Base64DecodingOptions(rawValue: 0)) {
    let dataArray: Array = (encryptedStringData.bytes)
    let hashOfDataBytesInt8 = dataArray.map { Int8(bitPattern: $0) }
    let keySaltBytesUInt8 = hashOfDataBytesInt8.map { UInt8(bitPattern: $0)}
    let dataFromArray = Data(keySaltBytesUInt8)
    let hash = SHA512.hash(data: dataFromArray)
}

但我还是得到了这样的结果:

"AAAAICAAAAAKAAAAAAAAAAAAAAAAAAAA4De76v5/AAAAAAAgIAAAACgAAAAAAAAAUwCn7rhnPQxAAAAAAAAAAA=="

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题