Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-12-2011, 04:16 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 84
prathibha is on a distinguished road
Thumbs up generate 16byte random number

Hi,
I want to generate the 16byte random number in objective c . Can anyone help me how can i do please ?
prathibha is offline   Reply With Quote
Old 09-12-2011, 05:06 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Mumbai
Posts: 38
redcup is on a distinguished road
Default

random number is generated by arc4random() function.
redcup is offline   Reply With Quote
Old 09-12-2011, 05:08 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 84
prathibha is on a distinguished road
Thumbs up

Quote:
Originally Posted by redcup View Post
random number is generated by arc4random() function.
and that number into 16byte how to convert ? any idea please
prathibha is offline   Reply With Quote
Old 09-12-2011, 05:15 AM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Mumbai
Posts: 38
redcup is on a distinguished road
Default

Quote:
Originally Posted by prathibha View Post
and that number into 16byte how to convert ? any idea please
Is it 16 byte or 16 bit coz 16 byte no. would be huge.

to get any 16 bit no just do arc4random()%65536.
redcup is offline   Reply With Quote
Old 09-12-2011, 05:19 AM   #5 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 84
prathibha is on a distinguished road
Thumbs up

Quote:
Originally Posted by redcup View Post
Is it 16 byte or 16 bit coz 16 byte no. would be huge.

to get any 16 bit no just do arc4random()%65536.
it is 16byte only... i want to generate 16byte number .. please any clue to work on it?
prathibha is offline   Reply With Quote
Old 09-12-2011, 05:25 AM   #6 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Mumbai
Posts: 38
redcup is on a distinguished road
Default

Quote:
Originally Posted by prathibha View Post
it is 16byte only... i want to generate 16byte number .. please any clue to work on it?
Can you please tell the range of numbers u want i.e from 0 to ???
redcup is offline   Reply With Quote
Old 09-12-2011, 05:26 AM   #7 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Quote:
Originally Posted by prathibha View Post
it is 16byte only... i want to generate 16byte number .. please any clue to work on it?
Use arc4random 16 times, each time creating one byte. But if you want 16 bytes, I'll bet you want it for security reasons (encryption) in which case arc4random isn't good enough because it is only pseudo-random. What are you using it for?
RLScott is offline   Reply With Quote
Old 09-12-2011, 05:36 AM   #8 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 84
prathibha is on a distinguished road
Thumbs up

Quote:
Originally Posted by RLScott View Post
Use arc4random 16 times, each time creating one byte. But if you want 16 bytes, I'll bet you want it for security reasons (encryption) in which case arc4random isn't good enough because it is only pseudo-random. What are you using it for?
thanks for the reply, i am using it for encryption purpose only , if it is pseudo random,how will i get a exact random number?please help me.
prathibha is offline   Reply With Quote
Old 09-12-2011, 05:38 AM   #9 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 84
prathibha is on a distinguished road
Thumbs up

Quote:
Originally Posted by redcup View Post
Can you please tell the range of numbers u want i.e from 0 to ???
i want the range upto 16byte only that is 128
prathibha is offline   Reply With Quote
Old 09-12-2011, 06:17 AM   #10 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Mumbai
Posts: 38
redcup is on a distinguished road
Default

Quote:
Originally Posted by prathibha View Post
i want the range upto 16byte only that is 128
May be this could help you
Loading…
redcup is offline   Reply With Quote
Old 09-12-2011, 06:25 AM   #11 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 84
prathibha is on a distinguished road
Thumbs up

Quote:
Originally Posted by redcup View Post
May be this could help you
Loading…
Hi,
finally cant we convert into 16bytes?there should be some way to get right?
prathibha is offline   Reply With Quote
Old 09-12-2011, 07:20 AM   #12 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Quote:
Originally Posted by prathibha View Post
Hi,
finally cant we convert into 16bytes?there should be some way to get right?
The link from redcup tells you everything you need to know:

SecRandomCopyBytes

Generates an array of cryptographically secure random bytes.
int SecRandomCopyBytes (
SecRandomRef rnd,
size_t count,
uint8_t *bytes
);

where:

rnd = The random number generator object to use. Specify kSecRandomDefault to use the default random number generator.

count = The number of random bytes to return in the array pointed to by the bytes parameter. (Use 16 for your purposes).

bytes = The random bytes generated by the function. (Make this point to your receiving array of 16 bytes).
RLScott is offline   Reply With Quote
Old 09-12-2011, 07:42 AM   #13 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by RLScott View Post
Use arc4random 16 times, each time creating one byte. But if you want 16 bytes, I'll bet you want it for security reasons (encryption) in which case arc4random isn't good enough because it is only pseudo-random. What are you using it for?
Bollocks.

arc4random is suitable for cryptographic use. ALL computer random number generators are pseudo-random. You need specialized hardware like scintillation detectors or noisy resistors to create truly random numbers. Pseudo-random numbers are the basis of much of computer cryptography.


Duncan
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-12-2011, 08:24 AM   #14 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Bollocks.

arc4random is suitable for cryptographic use. ALL computer random number generators are pseudo-random. You need specialized hardware like scintillation detectors or noisy resistors to create truly random numbers. Pseudo-random numbers are the basis of much of computer cryptography.


Duncan
I don't think arc4random is suitable because it is, as you say, pseudo random. Given the same seed, it always produces the same sequence of results. But SecRandomCopyBytes is different inthat it gets its randomness from the Linux: dev/random, which gets its randomness from a combination of unpredictable kernel events. There is even some speculation that the iPhone version of dev/random gets some input from the accelerometer. It may not be as good as noisy resistors, but it is certainly in a different class than arc4random.

If I wanted a very secure random sequence, I would probably call SecRandomCopyBytes numerous times over a long time period (several minutes) during which I was requesting input from the user. This input would cause more variations in Kernel events which would further increase the entropy in dev/random.
RLScott is offline   Reply With Quote
Old 09-12-2011, 08:31 AM   #15 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 84
prathibha is on a distinguished road
Thumbs up

Quote:
Originally Posted by Duncan C View Post
Bollocks.

arc4random is suitable for cryptographic use. ALL computer random number generators are pseudo-random. You need specialized hardware like scintillation detectors or noisy resistors to create truly random numbers. Pseudo-random numbers are the basis of much of computer cryptography.


Duncan
i am checking it now,i think it will help me a lot thanks for all ....
prathibha is offline   Reply With Quote
Old 09-12-2011, 08:47 AM   #16 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by RLScott View Post
I don't think arc4random is suitable because it is, as you say, pseudo random. Given the same seed, it always produces the same sequence of results. But SecRandomCopyBytes is different inthat it gets its randomness from the Linux: dev/random, which gets its randomness from a combination of unpredictable kernel events. There is even some speculation that the iPhone version of dev/random gets some input from the accelerometer. It may not be as good as noisy resistors, but it is certainly in a different class than arc4random.

If I wanted a very secure random sequence, I would probably call SecRandomCopyBytes numerous times over a long time period (several minutes) during which I was requesting input from the user. This input would cause more variations in Kernel events which would further increase the entropy in dev/random.

If you read the man page on arc4random(), it says:

The arc4random_stir() function reads data from /dev/urandom and uses it
to permute the S-Boxes via arc4random_addrandom().

There is no need to call arc4random_stir() before using arc4random(),
since arc4random() automatically initializes itself.


/dev/urandom is a system utility that maintains a pool of random seeds which is filled from "noise" in the system (like the timing for user actions, accelerometer data, jitter from the GPS, or whatever.) The details of the sources of system noise are platform-specific.

The system maintains an "entropy count" that tells it how much noise is available in this pool.

The one weakness in dev/urandom is that if the entropy count drops to zero, dev/urandom will start back at the beginning of the pool. That makes dev/urandom slightly less secure than dev/random, but with the benefit that it never blocks (aka causes your app to freeze). An equivalent system function, dev/random, blocks until the random seed pool collects more system noise.

I suspect that a handheld, network-connected, user-operated device like an iPhone/iPad/iPod touch is never lacking for abundant sources of system noise. The radio receivers are constantly receiving packets from the WiFi and/or cellular network with unpredictable (random) timing, the accelerometer magnetometer, and other sensors generate tons of very noisy data, etc.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-12-2011, 10:52 AM   #17 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
If you read the man page on arc4random(), it says:

The arc4random_stir() function reads data from /dev/urandom and uses it
to permute the S-Boxes via arc4random_addrandom().

There is no need to call arc4random_stir() before using arc4random(),
since arc4random() automatically initializes itself.
Ah, very good. I didn't realize that arc4random came from the same source. One possible concern is that if arc4random_stir is only called once the very first time you use arc4random, then the entropy is fixed at that time. It is deterministic from there on out. I might be concerned that a single snapshot from dev/random might not contain enough entropy to supply 128 bits of randomness. Explicitly calling arc4random_stir between bytes might be a good idea if you want in increase the randomness of a 16-byte array.
RLScott is offline   Reply With Quote
Old 09-13-2011, 01:08 AM   #18 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 84
prathibha is on a distinguished road
Thumbs up

Quote:
Originally Posted by prathibha View Post
it is 16byte only... i want to generate 16byte number .. please any clue to work on it?
ok please tell me to generate the random number for 1 byte using arc4random();
prathibha is offline   Reply With Quote
Old 09-13-2011, 05:43 AM   #19 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Quote:
Originally Posted by prathibha View Post
ok please tell me to generate the random number for 1 byte using arc4random();
oneByte = arc4random() & 0xFF;
RLScott is offline   Reply With Quote
Old 09-13-2011, 06:12 AM   #20 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 84
prathibha is on a distinguished road
Thumbs up

Quote:
Originally Posted by RLScott View Post
oneByte = arc4random() & 0xFF;
if i run arc4random() & 0xFF for 16times and if i append that into nsstring, will it become 16byte random number . I mean below is correct code for generating the 16byte radom number ? pleae help me?


NSMutableString *string = [[NSMutableString alloc]init];
for(int i = 0 ;i<16;i++)
{
// unsigned int a = arc4random()%65536;i was using this before
unsigned int a = arc4random() & 0xFF;

[string appendFormat:@"%llx",a];

}



NSLog(@"value of string is %@",string);
please help me
prathibha is offline   Reply With Quote
Old 09-13-2011, 06:32 AM   #21 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 84
prathibha is on a distinguished road
Thumbs up

Quote:
Originally Posted by prathibha View Post
if i run arc4random() & 0xFF for 16times and if i append that into nsstring, will it become 16byte random number . I mean below is correct code for generating the 16byte radom number ? pleae help me?


NSMutableString *string = [[NSMutableString alloc]init];
for(int i = 0 ;i<16;i++)
{
// unsigned int a = arc4random()%65536;i was using this before
unsigned int a = arc4random() & 0xFF;

[string appendFormat:@"%llx",a];

}



NSLog(@"value of string is %@",string);
please help me

Please help me.

Last edited by prathibha; 09-15-2011 at 11:56 PM. Reason: nobody replyed so
prathibha is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, objective c

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 404
10 members and 394 guests
7twenty7, apatsufas, Eclectic, eski, fiftysixty, JackReidy, teebee74, tim0504, UMAD, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:04 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0