Iterated chain of values

  • Thread starter Jesper Stocholm
  • Start date
J

Jesper Stocholm

I have an iterated function like this:


01 string Guid = System.Guid.NewGuid().ToString();
02 _bSeed = (new UnicodeEncoding()).GetBytes(Guid);
03
04 CreateHashChain(0,_bSeed,10000);
05
06 private void CreateHashChain(int i,byte[] b, int max)
07 {
08 SHA1 sha = new SHA1CryptoServiceProvider();
09 byte[] result;
10 result = sha.ComputeHash(b);
11 if (i < max)
12 {
13 i++;
14 CreateHashChain(i,result,max);
15 }
16 }


It takes a random byte-array as input and iterates a hashfunction over it
a fixed number of times.

Seen from a mathematical point of view, a hash-function (here SHA1) takes
an input bit string of arbitrary length and returns a bit string of
length 160.

In my case I need to make sure that the input to the hashfunction is
always 100bits, so I need to figure out how to do the following:

1.
How do I take the data from my seed (a string), truncate the first (least
significant) 100 _bits_ of it and then put it into a byte-array for later
input for SHA1?

2.
How do I take a byte-array (here the return value of the hash-function),
truncate the first (least significant) 100 _bits_ of it and then put it
BACK into a byte-array for later input for SHA1?

I hope you can help me with this,

:blush:)

Thanks,
 
J

Juan Gabriel Del Cid

1.
How do I take the data from my seed (a string), truncate the first (least
significant) 100 _bits_ of it and then put it into a byte-array for later
input for SHA1?

2.
How do I take a byte-array (here the return value of the hash-function),
truncate the first (least significant) 100 _bits_ of it and then put it
BACK into a byte-array for later input for SHA1?

You can use Array.Copy() to move data to and fro. Like so:

byte []bigArray = new byte[1024];
byte []smallArray = new byte[512];

// fill the big array with some data

// copy over the least significant 512 bytes
Array.Copy(bigArray, 512, smallArray, 0, 512);

Now, this copies over whole bytes. I say this because you are asking for 100
bits. This is unheard of. You can't pass data around in smaller chunks than
8 bits (a byte) and 100 is not a multiple of 8. SHA1 takes a byte array, so
there is really no way to work around it.

I hope that helps,
-JG
 
J

Jesper Stocholm

Juan Gabriel Del Cid wrote :
1.
How do I take the data from my seed (a string), truncate the first
(least significant) 100 _bits_ of it and then put it into a
byte-array for later input for SHA1?

2.
How do I take a byte-array (here the return value of the
hash-function), truncate the first (least significant) 100 _bits_ of
it and then put it BACK into a byte-array for later input for SHA1?

You can use Array.Copy() to move data to and fro. Like so:

byte []bigArray = new byte[1024];
byte []smallArray = new byte[512];

// fill the big array with some data

// copy over the least significant 512 bytes
Array.Copy(bigArray, 512, smallArray, 0, 512);

Fabulous, thanks for this hint.
Now, this copies over whole bytes. I say this because you are asking
for 100 bits. This is unheard of. You can't pass data around in
smaller chunks than 8 bits (a byte) and 100 is not a multiple of 8.
SHA1 takes a byte array, so there is really no way to work around it.

Yas, I thought of this late last night. Doing what I suggested would be
like asking for 12½ electrons (given the classical idea of how the atom
is constructed).

I have changed the requirements to 96 bits corresponding to 12 bytes and
I will pass this to SHA1.

thanks for you input,

:blush:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top