How to apply PKCS5 to an NSData ?
There doesn't seem to be much info about this yet, I've found this Objective-C solution that would work but I'm looking for a "swiftier" approach if possible.
Here is my failed attempt :
func pkcs5(data: NSData, blocksize: Int = 16) -> NSMutableData {
var data = NSMutableData(data: data)
let count = blocksize - data.length % blocksize
for _ in 0...count {
// somehow append the equivalent of chr(count) to data
}
return data
}
If you know Python, here's the function I'm trying to copy :
def pkcs5_pad(data, blocksize=16):
pad_count = blocksize - len(data) % blocksize
return data + (chr(pad_count) * pad_count).encode('utf-8')
Thanks.
You can use
appendBytes():The "trick" is to declare
countas a variable of typeUInt8, so that you can pass a pointer with&count.Alternatively, you can create an array with the padding bytes first and then append the data in one step: