NSMutableData's bytes to UnsafeMutableRawPointer not updating the value of mutableData - ios

I am having a struct object. And a method, whose input is payload. Now I am creating a mutableData named packet, and it's mutable bytes are referring to ICMPHeader struct.
struct ICMPHeader {
var type:UInt8
var code:UInt8
var checksum:UInt16
var identifier:UInt16
var sequenceNumber:UInt16
};
func createPacket(payload:NSData) -> NSData(){
var packet:NSMutableData?
var icmpPtr:ICMPHeader = ICMPHeader(type: 0, code: 0, checksum: 0, identifier: 0, sequenceNumber: 0)
packet = NSMutableData(length: Int(MemoryLayout<ICMPHeader>.size + payload.length))
if packet != nil {
icmpPtr = packet!.mutableBytes.assumingMemoryBound(to: ICMPHeader.self).pointee
icmpPtr.type = type
icmpPtr.code = 0
icmpPtr.checksum = 0
icmpPtr.identifier = CFSwapInt16BigToHost(identifier)
icmpPtr.sequenceNumber = CFSwapInt16HostToBig(identifier)
memcpy(&icmpPtr + 1, payload.bytes, payload.length)
if (requiresChecksum) {
icmpPtr.checksum = in_cksum(packet!.bytes, bufferLen: packet!.length);
}
}
return packet
}
Mutable bytes are successfully getting binded to struct, and values are also getting updated in struct ICMPHeader.
The issue is changing the values in struct is not changing the value of mutable data packet.
And if, I am trying to recreate the packet after creating struct, then it is crashing.
package = NSMutableData(bytes: unsafeBitCast(icmpPtr, to: UnsafeMutableRawPointer.self), length: Int(MemoryLayout<ICMPHeader>.size + payload.length))

I have found the answer in XCode 8.1 Swift release notes.
After making the changes in struct object icmpPtr I have changed it back to buffer pointer.
var byteBuffer = [UInt8]()
withUnsafeBytes(of: &icmpPtr) {
(bytes: UnsafeRawBufferPointer) in byteBuffer += bytes
}
package.replaceBytes(in: NSMakeRange(0, byteBuffer.count), withBytes: byteBuffer)
So instead of creating the new Data object, I have replaced the bytes and it worked like a charm.
As per documentation:
XCode 8.1 Release Notes:
https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Introduction.html
Swift Section:
A new withUnsafeBytes(of:) function exposes a value's in-memory
representation as an UnsafeRawBufferPointer. This example copies a
heterogenous struct into a homogeneous array of bytes:
struct Header {
var x: Int
var y: Float
}
var header = Header(x: 0, y: 0.0)
var byteBuffer = [UInt8]()
withUnsafeBytes(of: &header) {
(bytes: UnsafeRawBufferPointer) in byteBuffer += bytes
}
A new Array.withUnsafeBytes method exposes an array's underlying
buffer as an UnsafeRawBufferPointer. This example copies an array of
integers into an array of bytes:
let intArray = [1, 2, 3]
var byteBuffer = [UInt8]()
intArray.withUnsafeBytes {
(bytes: UnsafeRawBufferPointer) in byteBuffer += bytes
}
So the final implementation is
struct ICMPHeader {
var type:UInt8
var code:UInt8
var checksum:UInt16
var identifier:UInt16
var sequenceNumber:UInt16
};
func createPacket(payload:NSData) -> NSData(){
var packet:NSMutableData?
var icmpPtr:ICMPHeader = ICMPHeader(type: 0, code: 0, checksum: 0, identifier: 0, sequenceNumber: 0)
packet = NSMutableData(length: Int(MemoryLayout<ICMPHeader>.size + payload.length))
if packet != nil {
icmpPtr = packet!.mutableBytes.assumingMemoryBound(to: ICMPHeader.self).pointee
icmpPtr.type = type
icmpPtr.code = 0
icmpPtr.checksum = 0
icmpPtr.identifier = CFSwapInt16BigToHost(identifier)
icmpPtr.sequenceNumber = CFSwapInt16HostToBig(identifier)
memcpy(&icmpPtr + 1, payload.bytes, payload.length)
if (requiresChecksum) {
icmpPtr.checksum = in_cksum(packet!.bytes, bufferLen: packet!.length);
}
var byteBuffer = [UInt8]()
withUnsafeBytes(of: &icmpPtr) {
(bytes: UnsafeRawBufferPointer) in byteBuffer += bytes
}
packet.replaceBytes(in: NSMakeRange(0, byteBuffer.count), withBytes: byteBuffer)
}
return packet
}

Related

iOS and Android generating different hash values in SHA512

We need to verify hash value coming from the server. Android part is working. On iOS side we use CryptoKit to generate hash value.
Passing as paramater "jsonString" from which the hash key needs to be generated and "hasValue" which needs to be matched. While both functions use the same algorythm the outcome is always different.
let digest = SHA512.hash(data: jsonStringWithSaltBytes)
let hasBytes = digest.bytes
In computeHas function when we try to get the let hasBytes = digest.bytes it always give different HashBytes after SHA512.hash bytes conversion.
import CryptoKit
extension Digest {
var bytes: [UInt8] { Array(makeIterator()) }
var data: Data { Data(bytes) }
}
func generateHasKey(jsonString: String, hasValue: String) -> Bool {
let data = Data(base64Encoded: hasValue)
let hashWithSaltByte: [UInt8] = Array(data!)
let hashSizeByts: Int = 512 / 8
if hashWithSaltByte.count < hashSizeByts {
return false
}
var saltBytesArr: [UInt8] = Array(repeating: 0, count: (hasWithSaltByte.count - hashSizeInByts))
for index in 0..<saltBytes.count {
saltBytesArr[index] = hasWithSaltByte[hashSizeInByts + index]
}
let expectedHasString = computeHas(jsonString: jsonString, saltBytes: saltBytesArr)
if expectedHashString == hasValue {
print("Hash value match..")
return true
}else {
print("Hash value did not match..")
return false
}
}
func computeHash(jsonString: String, saltBytes: [UInt8]) -> String {
let saltBytes = saltBytes
let jsonStringBytes: [UInt8] = Array(jsonString.utf8)
var jsonStringWithSaltBytes: [UInt8] = Array(repeating: 0, count: (jsonStringBytes.count + saltBytes.count))
for index in 0..<jsonStringBytes.count {
jsonStringWithSaltBytes[index] = jsonStringBytes[index]
}
for index in 0..<saltBytes.count {
jsonStringWithSaltBytes[jsonStringBytes.count + index] = saltBytes[index]
}
let digest = SHA512.hash(data: jsonStringWithSaltBytes)
let hashBytes = digest.bytes
var hashWithSaltBytes: [UInt8] = Array(repeating: 0, count: (hashBytes.count + saltBytes.count))
for index in 0..<hasBytes.count {
hashWithSaltBytes[index] = hashBytes[index]
}
for index in 0..<saltBytes.count {
hashWithSaltBytes[hashBytes.count + index] = saltBytes[index]
}
let FinalsaltData = Data(hasWithSaltBytes)
let finalHashString = FinalsaltData.base64EncodedString()
return finalHashString ?? ""
}

How to pass key in HMAC as HEX Swift iOS

So I have this code to generate for HMAC-SHA1
let key = "foo".toSHA1()
let data = "bar"
var results = [CUnsignedChar](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1), key, key.count, data, data.count, &results)
let hmacData:NSData = NSData(bytes: results, length: (Int(CC_SHA1_DIGEST_LENGTH)))
var bytes = [UInt8](repeating: 0, count: hmacData.length)
hmacData.getBytes(&bytes, length: hmacData.length)
var hexString = ""
for byte in bytes {
hexString += String(format:"%02hhx", UInt8(byte))
}
print(hexString)
and this code for converting key string to SHA1
func toSHA1() -> String {
let data = self.data(using: String.Encoding.utf8)!
var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA1($0, CC_LONG(data.count), &digest)
}
let hexBytes = digest.map { String(format: "%02x", $0) }
return hexBytes.joined()
}
and the result is
faa3c04b058d38cecf1243421a596742a6cf1188
so using this onlineHMAC Generator outputs the same result. But my desired output should be
38b24d28d64f2459d42d1ecd1c9fa375ffeb369f
and I can achieve this by changing the Key type to HEX in the page that I provided.
So my problem now is how do I get the same output in my code? Do I need to convert key to hex?
Fixed it by passing digest as key instead of converting it to string.
Here's the updated code
let key = "foo".toSHA1()
let data = "bar"
var results = [CUnsignedChar](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1), key, key.count, data, data.count, &results)
let hmacData:NSData = NSData(bytes: results, length: (Int(CC_SHA1_DIGEST_LENGTH)))
var bytes = [UInt8](repeating: 0, count: hmacData.length)
hmacData.getBytes(&bytes, length: hmacData.length)
var hexString = ""
for byte in bytes {
hexString += String(format:"%02hhx", UInt8(byte))
}
print(hexString)
func toSHA1() -> [UInt8] {
let data = self.data(using: String.Encoding.utf8)!
var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA1($0, CC_LONG(data.count), &digest)
}
return digest
}

Writing structure to an outputStream in Swift 3

I'm trying to pass a structure in a stream so that it is then sent over the socket to another device. The code works, but the wrong data is sent. And each time random data is sent - then I'm doing something wrong. Where am I wrong? Here is my code:
public struct PStypes {
var u: UInt32 //< [X_XXXXXX V]
var i: UInt32 //< [X_XXXXXX A]
}
func sendMessage(message: String) {
var task = PStypes(u: 7, i: 9)
var bufferData = NSData(bytes: &task, length: 8)
var data:Data = bufferData as Data
var bufferDataSize = data.count
let bytesWritten = withUnsafePointer(to: &data) {
$0.withMemoryRebound(to: UInt8.self, capacity: bufferDataSize) {
outputStream.write($0, maxLength: bufferDataSize)
}
}
}
The problem is with this code:
let bytesWritten = withUnsafePointer(to: &data) {
$0.withMemoryRebound(to: UInt8.self, capacity: bufferDataSize) {
outputStream.write($0, maxLength: bufferDataSize)
}
}
this ends up giving you a pointer to the Data data structure itself not the data it holds. You can fix this using:
let bytesWritten = data.withUnsafeBytes {
outputStream.write($0, maxLength: 8)
}
this also simplifies the code a little!
var task = PStypes(u: 7, i: 9)
I think you are passing Int value instead of UInt32

Convert UIImage to byte array in swift

How can I convert a UIimage into a Byte Array, so I can upload it into my web service?
You can actually use a couple of lines to do it
guard let image = UIImage(named: "someImage"),
let data = image.jpegData(compressionQuality: 1.0) else { return }
// OR
guard let image = UIImage(named: "someImage"),
let data = image.pngData() else { return }
The number should range from 0.0 to 1.0 and sets the jpeg quality. PNG is lossless so there is no need for compression quality identifier but be aware that the file size can be about 10 times higher
--- update ---
Updated for Swift 5.1
You can convert UIImage to NSData and pass it to this method
func getArrayOfBytesFromImage(imageData:NSData) -> NSMutableArray
{
// the number of elements:
let count = imageData.length / sizeof(UInt8)
// create array of appropriate length:
var bytes = [UInt8](count: count, repeatedValue: 0)
// copy bytes into array
imageData.getBytes(&bytes, length:count * sizeof(UInt8))
var byteArray:NSMutableArray = NSMutableArray()
for (var i = 0; i < count; i++) {
byteArray.addObject(NSNumber(unsignedChar: bytes[i]))
}
return byteArray
}
Swift 5, iOS 14 version based on toofani answer, minimal changes
func getArrayOfBytesFromImage(imageData:NSData) -> Array<UInt8>
{
// the number of elements:
let count = imageData.length / MemoryLayout<Int8>.size
// create array of appropriate length:
var bytes = [UInt8](repeating: 0, count: count)
// copy bytes into array
imageData.getBytes(&bytes, length:count * MemoryLayout<Int8>.size)
var byteArray:Array = Array<UInt8>()
for i in 0 ..< count {
byteArray.append(bytes[i])
}
return byteArray
}
So a complete sequence looks like this... assuming I got a UIImage I extract the data and then recombine it.
let data = imageX.pngData()
bytes = getArrayOfBytesFromImage(imageData: data! as NSData)
let datos: NSData = NSData(bytes: bytes, length: bytes.count)
newImage = UIImage(data: datos as Data) // Note it's optional. Don't force unwrap!!!

How do I send data from a GKPlayer to another in a match?

I would like to send NSData from a GKPlayer to another in a match. Therefore, the method sendDataToAllPlayers(_:withDataMode:error:) would be the most ideal.
func sendDataToAllPlayers(data: Int,
withDataMode Reliable: GKMatchSendDataMode,
error: NSErrorPointer) -> Bool {
return true
}
Though, how can I use the method above to send an Int variable?
var score:Int = 0
extension Int {
var data: NSData {
var source = self
return NSData(bytes: &source, length: sizeof(Int))
}
}
extension Double {
var data: NSData {
var source = self
return NSData(bytes: &source, length: sizeof(Double))
}
}
extension NSData {
var integerValue:Int {
var result = 0
getBytes(&result, length: sizeof(Int))
return result
}
var doubleValue:Double {
var result:Double = 0
getBytes(&result, length: sizeof(Double))
return result
}
}
let myIntegerData = 123.data // _NSInlineData
let backToInt = myIntegerData.integerValue // 123
let myDoubleData = 123.456789.data // _NSInlineData
let backToDouble = myDoubleData.doubleValue // 123.456789
This version for encapsulating an Int (or other type) into or extracting it fromNSData should work even between devices with different byte orders:
// at sender before sending data
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
archiver.encodeInteger(score, forKey: "score")
archiver.finishEncoding()
// at receiver after receiving data
let unarchiver = NSKeyedUnarchiver(forReadingWithData: data)
let score = unarchiver.decodeIntegerForKey("score")
unarchiver.finishDecoding()

Resources