Java to Swift 5: Code Conversion for Device UID Verification
以下是将Java代码转换为Swift 5代码的结果:
import Foundation
func checkDeviceUidByKey(_ deviceUid: String, _ key: String) -> Int {
let sha1Length = 20
let checkString = ConfigManager.ProgramName + deviceUid + 'DES3'
let checkBytes = Array(checkString.utf8)
let keyBytes = Array(key.utf8)
var decodeKey = [UInt8](repeating: 0, count: key.count / 2)
var crcString = [UInt8](repeating: 0, count: checkBytes.count + sha1Length)
var i, j, k: Int
var crcValue: UInt32
// decode key.
j = 0
for i in 0..<key.count {
k = i % (26 - 16)
decodeKey[j] = keyBytes[i] - UInt8(UnicodeScalar('A').value) - UInt8(k)
i += 1
k = i % (26 - 16)
decodeKey[j] = (decodeKey[j] << 4) + (keyBytes[i] - UInt8(UnicodeScalar('A').value) - UInt8(k))
j += 1
}
// build string
crcString[0..<checkBytes.count] = checkBytes
crcString[checkBytes.count..<(checkBytes.count + sha1Length)] = decodeKey[0..<sha1Length]
var crc32 = CRC32()
crc32.update(data: crcString)
crcValue = crc32.value
var bBuf = ByteBuffer.allocate(capacity: 8)
bBuf.putLong(value: crcValue)
let crcResult = bBuf.array()
for i in 0..<4 {
if decodeKey[sha1Length + i] != crcResult[4 + i] {
return 0
}
}
return 1
}
请注意,此转换代码是基于你提供的Java代码,但是缺少ConfigManager类和CRC32类的定义。你需要根据Swift代码库中可用的适当类来替换这些引用。
原文地址: https://www.cveoy.top/t/topic/bPCX 著作权归作者所有。请勿转载和采集!