美文网首页
iOS安全-SecRandomCopyBytes随机数

iOS安全-SecRandomCopyBytes随机数

作者: Bobo_Ma | 来源:发表于2019-05-20 14:59 被阅读0次
    1. rand()和random()实际并不是一个真正的伪随机数发生器,在使用之前需要先初始化随机种子,否则每次生成的随机数一样。
    2. arc4random()和arc4random_uniform(u_int32_t)是一个真正的伪随机算法,不需要生成随机种子,因为第一次调用的时候就会自动生成。而且范围是rand()的两倍。
    3. 如果渗透测试 arc4random()和arc4random_uniform(u_int32_t)不能通过,使用如下代码生成随机数
    uint8_t randomBytes[16];
        NSMutableString *ivStr;
        int result = SecRandomCopyBytes(kSecRandomDefault, 16, randomBytes);
        if(result == 0) {
            ivStr = [[NSMutableString alloc] initWithCapacity:16];
            for(NSInteger index = 0; index < 16; index++)
            {
                [ivStr appendFormat: @"%02x", randomBytes[index]];
            }
            NSLog(@"randomStr is %@", ivStr);
        } else {
            NSLog(@"SecRandomCopyBytes failed for some reason");
        }
    
    

    相关文章

      网友评论

          本文标题:iOS安全-SecRandomCopyBytes随机数

          本文链接:https://www.haomeiwen.com/subject/wvekzqtx.html