Hell of a time extracting bits from a vector

Discussion in 'Perl Misc' started by Idgarad, Feb 29, 2008.

  1. Idgarad

    Idgarad Guest

    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,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?
     
    Idgarad, Feb 29, 2008
    #1
    1. Advertising

  2. 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
     
    John W. Krahn, Feb 29, 2008
    #2
    1. Advertising

  3. On Feb 29, 9:09 am, 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
    >
    > 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,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
     
    comp.llang.perl.moderated, Feb 29, 2008
    #3
  4. Idgarad

    Idgarad Guest

    On Feb 29, 1:53 pm, "John W. Krahn" <> 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?
     
    Idgarad, Mar 13, 2008
    #4
  5. Idgarad

    Ben Morrow Guest

    Quoth Idgarad <>:
    >
    > 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
     
    Ben Morrow, Mar 13, 2008
    #5
  6. Idgarad

    Idgarad Guest

    On Mar 13, 9:40 am, Ben Morrow <> wrote:
    > Quoth Idgarad <>:
    >
    >
    >
    > > 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!
     
    Idgarad, Mar 19, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GGG
    Replies:
    10
    Views:
    12,737
    Donar
    Jul 6, 2006
  2. sarmin kho
    Replies:
    2
    Views:
    855
    A. Lloyd Flanagan
    Jun 15, 2004
  3. Miki Tebeka
    Replies:
    1
    Views:
    465
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  4. sergey

    "casting" bits to bits?

    sergey, Nov 8, 2006, in forum: VHDL
    Replies:
    1
    Views:
    751
    sergey
    Nov 8, 2006
  5. Replies:
    8
    Views:
    1,999
    Csaba
    Feb 18, 2006
Loading...

Share This Page