Re: help tranlating perl expressions
- From: Fredrik Lundh <fredrik@xxxxxxxxxxxxxx>
- Date: Thu, 28 Sep 2006 19:26:08 +0200
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>
.
- Follow-Ups:
- Re: help tranlating perl expressions
- From: David Bear
- Re: help tranlating perl expressions
- References:
- help tranlating perl expressions
- From: David Bear
- help tranlating perl expressions
- Prev by Date: ctypes.c_void_p(-1) might not be C return (void *) -1
- Next by Date: Re: Resuming a program's execution after correcting error
- Previous by thread: Re: help tranlating perl expressions
- Next by thread: Re: help tranlating perl expressions
- Index(es):
Relevant Pages
|