Re: help tranlating perl expressions



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>

.



Relevant Pages

  • Re: UTF-8 / German, Scandinavian letters - is it really this difficult?? Linux & Windows XP
    ... For string literals, with the "coding" declaration, Python will accept ... "coding" declaration to produce a Unicode object which unambiguously ... represents the sequence of characters - ie. something that can be ... > strings and/or gibberished characters in Tk GUI title? ...
    (comp.lang.python)
  • Re: A Python newbie ask a simple question
    ... the first character in the user supplied response as strings support ... What is the good book to learn Python? ... and having checked out some of the tutorials so you know ... using the Help Index again with "sequence" (since you will probably ...
    (comp.lang.python)
  • Re: Problem of function calls from map()
    ... 'lines' is a large list of strings each of which is seperated by '\t' ... usually easier to understand as a single gestalt (i.e., ... performance in Python (and other languages, too, but perhaps easier to ... map() isn't much of a win in this case. ...
    (comp.lang.python)
  • Re: Seeking regex optimizer
    ... Note that the Python regex engine will consider each candidate in Paddy's solution left to right until it gets a match or reaches the end. ... This is worst-case Owhere N is the total of the lengths of all the strings in your list. ... "sx.matchyields a SRE_Match object" ... ... I would have thought the way to approach this would be a simple character-by-character tree/trie-driven lookup. ...
    (comp.lang.python)
  • Re: fast text processing
    ... What I have now is roughly as follows (on python 2.3.5). ... Note that this is going to read the whole file in to memory before entering the loop. ... currentGeno = extract ... So on every iteration I'm processing mutable strings -- this seems wrong. ...
    (comp.lang.python)