简体   繁体   English

如何在iOS中创建ByteBuffer?

[英]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()) ? 我在iOS端创建CSRF令牌,在服务器端使用md5哈希完成此操作,而在服务器端创建此md5哈希则使用16字节大小的ByteBuffer并设置随机数据,方法与在iOS中使用如代码(0,Math.random())和(8,Math.random())所述分配大小16和设置随机数据? 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. iOS Swift中的字节是UInt8对象。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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