简体   繁体   中英

How to create ByteBuffer in iOS?

I'm creating CSRF token at iOS end, in server side they have done using md5 hash, while creating this md5 hash in server side they have used ByteBuffer of 16 size and set random data, in same way how we can do in iOS with allocation of size 16 and set random data as mentioned in code (0, Math.random()) and (8, Math.random()) ? Here is the sample code of server side.

ByteBuffer bb = ByteBuffer.allocate(16); bb.putDouble(0, Math.random()); bb.putDouble(8, Math.random()); String input = Hex.encodeHexString(bb.array());

Byte in iOS Swift is an UInt8 object. So, your byte array is

var byteArray [UInt8] = []
// and you can append like
let byte : UInt8 = 10
byteArray.append(byte)

Hope it helps!

EDIT This way, you can provide the size

var byteArray: [UInt8] = [UInt8](repeatElement(0, count: 2))
// and you can append like
let byte1 : UInt8 = 10
byteArray[0] = byte1
let byte2 : UInt8 = 10
byteArray[1] = byte2
print(byteArray)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM