Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Hell of a time extracting bits from a vector

Reply
Thread Tools

Hell of a time extracting bits from a vector

 
 
Idgarad
Guest
Posts: n/a
 
      02-29-2008
I am generating a SHA1 digest that I want to use for some values.

I want to take the digest that is generated at extact a given number
of bits, in sequence.

SHA1 generates 160 bits.

I would like to partition that 160 bits into an array storing the
value of those bits

for instance (in short form using only 10 bits grabbing 2 at a time)
lets say I have:

1010010101

and I am grabbing pairs I need (from least to most):

@somearray

$somearray[0] = 1 (01)
$somearray[1] = 1 (01)
$somearray[2] = 1 (01)
$somearray[3] = 2 (10)
$somearray[4] = 2 (10)


I I tried using vec (going back to the full 160 bits) but it fails
miserably. Here a sample

Using vec($digest,$loop*8, (Grabbing 8 bits)

I would get
0101010101
1101011010
100101001010101101
010110110101011101
....

The lengths would be wrong (doing an unpack "b*" on the array that was
holding the result. Sometimes I would get 8, other 16 and the number
even changes running the same data!)

I even tried using the Binary::Vector with a Chunk_Read and got
similar results.

This should be this hard to just extract X bits from a Y*X offset
where Y is the loop iteration.

What am I missing?
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      02-29-2008
Idgarad wrote:
> I am generating a SHA1 digest that I want to use for some values.
>
> I want to take the digest that is generated at extact a given number
> of bits, in sequence.
>
> SHA1 generates 160 bits.
>
> I would like to partition that 160 bits into an array storing the
> value of those bits


use Digest::SHA1 qw(sha1);

unpack '(a)*', unpack 'B*', sha1( $data );


> for instance (in short form using only 10 bits grabbing 2 at a time)
> lets say I have:
>
> 1010010101
>
> and I am grabbing pairs I need (from least to most):
>
> @somearray
>
> $somearray[0] = 1 (01)
> $somearray[1] = 1 (01)
> $somearray[2] = 1 (01)
> $somearray[3] = 2 (10)
> $somearray[4] = 2 (10)


my @somearray = unpack '(a2)*', unpack 'B*', sha1( $data );



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
 
 
 
comp.llang.perl.moderated
Guest
Posts: n/a
 
      02-29-2008
On Feb 29, 9:09 am, Idgarad <(E-Mail Removed)> wrote:
> I am generating a SHA1 digest that I want to use for some values.
>
> I want to take the digest that is generated at extact a given number
> of bits, in sequence.
>
> SHA1 generates 160 bits.
>
> I would like to partition that 160 bits into an array storing the
> value of those bits
>
> for instance (in short form using only 10 bits grabbing 2 at a time)
> lets say I have:
>
> 1010010101
>
> and I am grabbing pairs I need (from least to most):
>
> @somearray
>
> $somearray[0] = 1 (01)
> $somearray[1] = 1 (01)
> $somearray[2] = 1 (01)
> $somearray[3] = 2 (10)
> $somearray[4] = 2 (10)
>
> I I tried using vec (going back to the full 160 bits) but it fails
> miserably. Here a sample
>
> Using vec($digest,$loop*8, (Grabbing 8 bits)
>
> I would get
> 0101010101
> 1101011010
> 100101001010101101
> 010110110101011101
> ...
>
> The lengths would be wrong (doing an unpack "b*" on the array that was
> holding the result. Sometimes I would get 8, other 16 and the number
> even changes running the same data!)


you are probably forgetting that you need
to pack to an integer or short before you
you unpack to the bit string, eg,

# perl -le 'print unpack "B*",136'
001100010011001100110110

vs.

# perl -le 'print unpack "B*",pack("S",136)'
0000000010001000

--
Charles DeRykus
 
Reply With Quote
 
Idgarad
Guest
Posts: n/a
 
      03-13-2008
On Feb 29, 1:53*pm, "John W. Krahn" <(E-Mail Removed)> wrote:
> Idgarad wrote:
> > I am generating a SHA1 digest that I want to use for some values.

>
> > I want to take the digest that is generated at extact a given number
> > of bits, in sequence.

>
> > SHA1 generates 160 bits.

>
> > I would like to partition that 160 bits into an array storing the
> > value of those bits

>
> use Digest::SHA1 *qw(sha1);
>
> unpack '(a)*', unpack 'B*', sha1( $data );
>
> > for instance (in short form using only 10 bits grabbing 2 at a time)
> > lets say I have:

>
> > 1010010101

>
> > and I am grabbing pairs I need (from least to most):

>
> > @somearray

>
> > $somearray[0] = 1 (01)
> > $somearray[1] = 1 (01)
> > $somearray[2] = 1 (01)
> > $somearray[3] = 2 (10)
> > $somearray[4] = 2 (10)

>
> my @somearray = unpack '(a2)*', unpack 'B*', sha1( $data );
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order. * * * * * * * * * * * * * *-- Larry Wall


Exactly what I was looking for with one exception, I don't want to
store the ASCII in the array but rather the actual integer value.

my @somearray = unpack '(a2)*', unpack 'B*', sha1( $data );

Works perfect but I tried changing the a2 to N or n but that fails
miserable. I figure I have to append an bin to int of some sort to the
front of the unpacks but so far I have been unsuccessful. Any
suggestions?
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      03-13-2008

Quoth Idgarad <(E-Mail Removed)>:
>
> Exactly what I was looking for with one exception, I don't want to
> store the ASCII in the array but rather the actual integer value.
>
> my @somearray = unpack '(a2)*', unpack 'B*', sha1( $data );
>
> Works perfect but I tried changing the a2 to N or n but that fails
> miserable. I figure I have to append an bin to int of some sort to the
> front of the unpacks but so far I have been unsuccessful. Any
> suggestions?


The obvious way is to add

map { oct "0b$_" }

to the front; there may be a cleverer way with pack, but if you don't
need speed that's nice and simple.

Ben

 
Reply With Quote
 
Idgarad
Guest
Posts: n/a
 
      03-19-2008
On Mar 13, 9:40*am, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth Idgarad <(E-Mail Removed)>:
>
>
>
> > Exactly what I was looking for with one exception, I don't want to
> > store the ASCII in the array but rather the actual integer value.

>
> > my @somearray = unpack '(a2)*', unpack 'B*', sha1( $data );

>
> > Works perfect but I tried changing the a2 to N or n but that fails
> > miserable. I figure I have to append an bin to int of some sort to the
> > front of the unpacks but so far I have been unsuccessful. Any
> > suggestions?

>
> The obvious way is to add
>
> * * map { oct "0b$_" }
>
> to the front; there may be a cleverer way with pack, but if you don't
> need speed that's nice and simple.
>
> Ben


Worked like a charm, thank ya!
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
8-Bits vs 12 or 16 bits/pixel; When does more than 8 bits count ? Al Dykes Digital Photography 3 12-29-2003 07:08 PM



Advertisments