CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Generate 640 bit random number and write to a file in specific format

 



tapu015
Novice

Jul 24, 2014, 11:21 PM

Post #1 of 15 (3191 views)
Generate 640 bit random number and write to a file in specific format Can't Post

Hi,
In my perl script i want to generate 640 bit random number and write the nuber in to file in the format given below.

scrypt_test_setting post_settings[] = {
{"0000318f7e71441b141fe951b2b0c7dfc791d4646240fc2a2d1b80900020a24dc501ef1599fc48ed6cbac920af75575618e7b1e8eaf0b62a90d1942ea64d250357e9a09c063a47827c57b44e01000000","0000318f7e71441b141fe951b2b0c7dfc791d4646240fc2a2d1b80900020a24dc501ef1599fc48ed6cbac920af75575618e7b1e8eaf0b62a90d1942ea64d250357e9a09c063a47827c57b44e01000000",10, 0, 0},
{0}
};

Can anyone help me pls.
I am new to perl and have some basic knowledge of it.


FishMonger
Veteran / Moderator

Jul 25, 2014, 6:13 AM

Post #2 of 15 (3185 views)
Re: [tapu015] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post

Please post your script and explain your format.


tapu015
Novice

Jul 25, 2014, 6:59 AM

Post #3 of 15 (3179 views)
Re: [FishMonger] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post

Hi,
I have not written the script yet.
But the format is that, first a string "scrypt_test_setting post_settings[] ="to be written in to the file.
then 640 bit random data will be written {
{"Random data",10, 0, 0}, {0}
};

The overall file that will be created will be look like as given below.
scrypt_test_setting post_settings[] = {
{"0000318f7e71441b141fe951b2b0c7dfc791d4646240fc2a2d1b80900020a24dc501ef1599fc48ed6cbac920af75575618e7b1e8eaf0b62a90d1942ea64d250357e9a09c063a47827c57b44e01000000","0000318f7e71441b141fe951b2b0c7dfc791d4646240fc2a2d1b80900020a24dc501ef1599fc48ed6cbac920af75575618e7b1e8eaf0b62a90d1942ea64d250357e9a09c063a47827c57b44e01000000",10, 0, 0},
{0}
};

Pls give me some info to start scripting.


FishMonger
Veteran / Moderator

Jul 25, 2014, 7:27 AM

Post #4 of 15 (3176 views)
Re: [tapu015] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post

The "random data" you show is not a number and it's not 640 bits. It's 2 separate strings of 160 characters/bytes (2560 bits total).

Show me the algorithm you want to use to convert your 640 bit number to the 2 separate strings and I'll see what I can do to help you troubleshoot the perl code you will be writing to implement that algorithm.


FishMonger
Veteran / Moderator

Jul 25, 2014, 7:38 AM

Post #5 of 15 (3174 views)
Re: [tapu015] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post

Are you dealing with RSA-640 encryption?
http://en.wikipedia.org/wiki/RSA_numbers#RSA-640

This might help. http://thinking-security.net/tag/public-key-cryptography/


(This post was edited by FishMonger on Jul 25, 2014, 7:40 AM)


BillKSmith
Veteran

Jul 25, 2014, 8:01 AM

Post #6 of 15 (3172 views)
Re: [tapu015] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post

It appears that you want a comma separated list of huge random numbers expressed as 160-character hex integers (over 192 decimal digits). Format is probably the least of your problems. You need an algorithm for generating these numbers. Some how, you will need to verify that it meets your requirement for randomness. Implementation in perl's bigint is probably straight forward.
Good Luck,
Bill


tapu015
Novice

Jul 27, 2014, 11:27 PM

Post #7 of 15 (3061 views)
Re: [FishMonger] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post

Hi,
It is not 160 byte data.
It is 80 byte random data which concatenated to form 160 byte data(i.e 640 bit data) .
There is no algorithm for generation of data it can be any random data.
My problem is how to generate this large random data and write to file in specified format.


FishMonger
Veteran / Moderator

Jul 28, 2014, 7:43 AM

Post #8 of 15 (3057 views)
Re: [tapu015] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post

Take a look at the rand function.

perldoc -f rand
http://perldoc.perl.org/functions/rand.html


FishMonger
Veteran / Moderator

Jul 28, 2014, 9:02 AM

Post #9 of 15 (3052 views)
Re: [tapu015] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post


Quote
It is 80 byte random data which concatenated to form 160 byte data(i.e 640 bit data) .


Can you explain your math?

Each ascii character in the string you gave takes up 1 byte. 1 byte is made up of 8 bits. 160 x 8 = 1280

How did you come up with 640 bits?

If you simply want to take a large integer and display it in hex format, then look at using sprintf or printf.

http://perldoc.perl.org/functions/sprintf.html


BillKSmith
Veteran

Jul 28, 2014, 11:27 AM

Post #10 of 15 (3045 views)
Re: [FishMonger] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post

We are still talking about HUGE numbers. Each string consists of the concatenation of two integers each in the range 0 through 16**80-1 (approx. 0 though 10**96). The numbers are expressed in hexadecimal. Each 'digit' is expressed as an ASCII character [0-9A-Z]. It is necessary to find or write a random number generator which will generate a series of big integers this large which meets the (yet to be stated) requirement for randomness.

It appears that formatting the numbers will be straight forward. Generating them will not be.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Jul 28, 2014, 3:33 PM

Post #11 of 15 (3039 views)
Re: [BillKSmith] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post

Actually, I find it very easy to generate the number.


Code
#!/usr/bin/perl 

use 5.010;
use strict;
use warnings;
use Data::Dumper;

my @num = (0..9,'a'..'f');
my @hex;

for (1..2) {
my $hex;
for (1..40) {
for (1..2) {
$hex .= $num[ int rand(16) ];
}
}
push @hex, $hex;
}

say Dumper \@hex;



BillKSmith
Veteran

Jul 28, 2014, 8:03 PM

Post #12 of 15 (3034 views)
Re: [FishMonger] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post

I cannot prove whether or not this approach is correct, but it sure looks reasonable.
Good Luck,
Bill


BillKSmith
Veteran

Jul 29, 2014, 7:41 AM

Post #13 of 15 (3002 views)
Re: [tapu015] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post

Use Ron's insight that this problem can be solved with text manipulation rather than advanced math and the template your provided in Post 3.


Code
use strict; 
use warnings;
my @hex_digit = (0..9, 'a'..'f');
my $Random_data;
$Random_data .= $hex_digit[int rand 16] for (1..160);

print <<"END_OF_TEMPLATE";
scrypt_test_setting post_settings[] = {
{"$Random_data",10, 0, 0},{0}
};
END_OF_TEMPLATE


Note: there is a minor difference between your template and your example. (I followed the template).
Good Luck,
Bill


tapu015
Novice

Jul 31, 2014, 3:50 AM

Post #14 of 15 (2651 views)
Re: [BillKSmith] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post

Thanks Everyone for your help.
At last i am able to get the random data in my desired format.


tapu015
Novice

Jul 31, 2014, 3:54 AM

Post #15 of 15 (2649 views)
Re: [FishMonger] Generate 640 bit random number and write to a file in specific format [In reply to] Can't Post

The 160 byte is actually created using 80 byte of data merged with same 80 byte of data.

So i refer to 80 byte as 640 bit.
Sorry for explaining it in wrong way.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives