Best way to generate NSData object with random bytes of a specific length?

后端 未结 9 1314
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 11:37

If I create a new NSData object of a specific size using dataWithBytes:length:, what is the most efficient way to create the input bytes (20 Mb worth) of random characters, pref

相关标签:
9条回答
  • 2021-02-05 11:58

    Use arc4random_buf to fill the buffer with random bytes

    Obj-C

    + (nullable NSData *)radomDataOfSize:(size_t)sizeInBytes
    {
        void *buff = malloc(sizeInBytes);
        if (buff == NULL) {
            return nil;
        }
        arc4random_buf(buff, sizeInBytes);
    
        return [NSData dataWithBytesNoCopy:buff length:sizeInBytes freeWhenDone:YES];
    }
    
    0 讨论(0)
  • 2021-02-05 12:04

    Swift 3:

    import Security
    
    func randomBytes(length: Int) -> Data {
        var data = Data(capacity: length)
        data.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) -> Void in
            let _ = SecRandomCopyBytes(kSecRandomDefault, length, bytes)
        }
        return data
    }
    
    0 讨论(0)
  • 2021-02-05 12:10

    I've open sourced my JFRandom class over at github which can do exactly this. Here's a blog post demonstrating how to obtain/use it to achieve your goal...

    http://jayfuerstenberg.com/devblog/generating-random-numbers-strings-and-data-in-objective-c

    0 讨论(0)
  • 2021-02-05 12:11

    You might consider using CCRandomGenerateBytes function from CommonCrypto to generate random data. Like:

    func generateBytes(length : Int) throws -> NSData? {
        var bytes = [UInt8](count: length, repeatedValue: UInt8(0))
        let statusCode = CCRandomGenerateBytes(&bytes, bytes.count)
        if statusCode != CCRNGStatus(kCCSuccess) {
            return nil
        }
        return NSData(bytes: bytes, length: bytes.count)
    }
    
    0 讨论(0)
  • 2021-02-05 12:13

    Here's a 3-liner swift version:

    Swift 2

    let length = 2048
    let bytes = [UInt32](count: length, repeatedValue: 0).map { _ in arc4random() }
    let data = NSData(bytes: bytes, length: bytes.count * sizeof(UInt32))
    

    Swift 3

    let bytes = [UInt32](repeating: 0, count: length).map { _ in arc4random() }
    let data = Data(bytes: bytes, count: length)
    
    0 讨论(0)
  • 2021-02-05 12:15
    void * bytes = malloc(numberOfBytes);
    NSData * data = [NSData dataWithBytes:bytes length:numberOfBytes];
    free(bytes);
    

    The bytes are not 'random', but will contain garbage values (whatever was on the heap before this was run). The advantage being its fast and the code is concise.

    0 讨论(0)
提交回复
热议问题