Re: help tranlating perl expressions



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 --
.



Relevant Pages

  • Re: Python or PHP?
    ... If it's one of the things for which Python ... every language here and there more ways to do something. ... you make Perl more complicated than it is:-D. ... Not the programmer. ...
    (comp.lang.python)
  • Recommend an E-book Meeting the Following Criteria (Newbie, Long)
    ... I know several programming languages namely Java, Perl and C in this order ... Now I'm wondering which Python book I should get as there are so many out ... I'd like to mostly concentrate on language features but some pointers ...
    (comp.lang.python)
  • Re: Python for Perl programmers?
    ... I know pretty much every trick in perl and have ... > But I'd like to try a cleaner language, where you don't have to type ... > so much crap to create a class etc. So, I wanna give python a try. ... horrible experience of trying to maintain 25,000 lines of perl code. ...
    (comp.lang.python)
  • Re: Choosing Perl/Python for my particular niche
    ... >> For most general purpose tasks, I reach for Python first. ... > syntax and perhaps my lack of bottomless commitment (knowing Perl would not be ... verilog) so that it can be used for another e.g. verilator, ... using a standalone simulator to simulate the original verilog. ...
    (comp.lang.perl.misc)
  • Re: Choosing Perl/Python for my particular niche
    ... >> For most general purpose tasks, I reach for Python first. ... > syntax and perhaps my lack of bottomless commitment (knowing Perl would not be ... verilog) so that it can be used for another e.g. verilator, ... using a standalone simulator to simulate the original verilog. ...
    (comp.lang.python)