help tranlating perl expressions

Discussion in 'Python' started by David Bear, Sep 28, 2006.

  1. David Bear

    David Bear Guest

    I am trying to translate some perl code to python and I need some advice on
    making fixed sized strings. The perl code creates a sha1 signatured using a
    key and a 'pad'. It creates fixed sized strings as follows:

    my $klen = length($key);
    my $blen = 64;
    my $ipad = chr(0x36)x$blen;
    my $opad = chr(0x5c)x$blen;

    I don't know if I ever seen any way in python of created a fixed size
    string. Can anyone show me how to implement the same statements in python?

    Next, it zero-fills a string to a certain size.

    if($klen <= $blen) {
    $key .= "\0"x($blen-length($key)); #zero-fill to blocksize
    } else {
    $key = sha1($key); #if longer, pre-hash key
    }

    Finally it concatenates and xors these strings together like this:

    return sha1($key^$opad . sha1($key^$ipad . $data));

    I when python XOR's strings, is it the same as when perl xor's them?

    --
    David Bear
    -- let me buy your intellectual property, I want to own your thoughts --
    David Bear, Sep 28, 2006
    #1
    1. Advertising

  2. David Bear

    Erik Johnson Guest

    "David Bear" <> wrote in message
    news:5452284.reKt6RD0Uf@teancum...

    > my $klen = length($key);
    > my $blen = 64;
    > my $ipad = chr(0x36)x$blen;
    > my $opad = chr(0x5c)x$blen;
    >
    > I don't know if I ever seen any way in python of created a fixed size
    > string. Can anyone show me how to implement the same statements in python?


    Certainly, you can create fixed-length strings in python:
    >>> n = 7
    >>> chr(0x36) * n

    '6666666'

    > I when python XOR's strings, is it the same as when perl xor's them?

    No, XOR is a bitwise operation that does not apply to strings.

    >>> 5 ^ 0

    5
    >>> 5 ^ 0xF

    10
    >>> 'a' ^ 'b'

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: unsupported operand type(s) for ^: 'str' and 'str'

    See also the 'struct' module: http://docs.python.org/lib/module-struct.html
    Erik Johnson, Sep 28, 2006
    #2
    1. Advertising

  3. David Bear wrote:

    > I am trying to translate some perl code to python and I need some
    > advice on making fixed sized strings.


    looks like you're reimplementing HMAC; there's no need to do that in
    Python, really, since it's part of the standard library:

    http://docs.python.org/lib/module-hmac.html

    pass in hashlib.sha1 instead of the default, and you're done.

    if you insist on doing it yourself, read on:

    > my $ipad = chr(0x36)x$blen;
    > my $opad = chr(0x5c)x$blen;
    >
    > I don't know if I ever seen any way in python of created a fixed size
    > string. Can anyone show me how to implement the same statements in python?


    just remove all the junk, and use multiply instead of "x":

    ipad = chr(0x36) * blen
    opad = chr(0x5c) * blen

    however, Python strings are not mutable, so to implement the rest of
    that algorithm, you probably want to use a list or array object instead.
    the md5-example-4.py script on this page shows one way to do that:

    http://effbot.org/librarybook/md5.htm

    to get a SHA-1 hmac, you have to replace "md5" with "sha".

    </F>
    Fredrik Lundh, Sep 28, 2006
    #3
  4. David Bear

    David Bear Guest

    Fredrik Lundh wrote:

    > David Bear wrote:
    >
    > > I am trying to translate some perl code to python and I need some
    > > advice on making fixed sized strings.

    >
    > looks like you're reimplementing HMAC; there's no need to do that in
    > Python, really, since it's part of the standard library:
    >
    > http://docs.python.org/lib/module-hmac.html
    >
    > pass in hashlib.sha1 instead of the default, and you're done.
    >
    > if you insist on doing it yourself, read on:
    >
    >> my $ipad = chr(0x36)x$blen;
    >> my $opad = chr(0x5c)x$blen;
    >>
    >> I don't know if I ever seen any way in python of created a fixed size
    >> string. Can anyone show me how to implement the same statements in
    >> python?

    >
    > just remove all the junk, and use multiply instead of "x":
    >
    > ipad = chr(0x36) * blen
    > opad = chr(0x5c) * blen
    >
    > however, Python strings are not mutable, so to implement the rest of
    > that algorithm, you probably want to use a list or array object instead.
    > the md5-example-4.py script on this page shows one way to do that:
    >
    > http://effbot.org/librarybook/md5.htm
    >
    > to get a SHA-1 hmac, you have to replace "md5" with "sha".
    >
    > </F>


    Yes, this is what I am doing. Because I am using code sold to me by a vendor
    -- I was worried that they are doing something with it that had some
    dependencies on the way perl was making the digest. So I was trying to
    better understand the perl by doing it in python.

    --
    David Bear
    -- let me buy your intellectual property, I want to own your thoughts --
    David Bear, Sep 28, 2006
    #4
  5. On Thu, 28 Sep 2006 10:57:19 -0700, David Bear <>
    declaimed the following in comp.lang.python:

    > Yes, this is what I am doing. Because I am using code sold to me by a vendor
    > -- I was worried that they are doing something with it that had some
    > dependencies on the way perl was making the digest. So I was trying to
    > better understand the perl by doing it in python.


    For the most part, it looks like they were trying to preset the
    result to some sort of padding, then modifying parts of that to contain
    the real data.

    Since Python strings are immutable, one way to produce a right
    padded string is just to concatenate the data with padding, then
    slice...

    res = "".join([data, "pad"*lngth])[:lngth]

    The XOR needs to be done piecewise...; presuming both sides (a and
    b) are same length (which I presume the padding is meant to achieve)

    res = "".join([chr(ord(a) ^ ord(b) for i in len(a)])
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Sep 28, 2006
    #5
    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. dpackwood
    Replies:
    3
    Views:
    1,767
  2. Jay Douglas
    Replies:
    0
    Views:
    592
    Jay Douglas
    Aug 15, 2003
  3. Rumpa

    Perl regular expressions help

    Rumpa, Aug 26, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    131
    Tad McClellan
    Aug 27, 2005
  4. Graham
    Replies:
    3
    Views:
    162
    Graham
    Mar 18, 2006
  5. Noman Shapiro
    Replies:
    0
    Views:
    219
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page