Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Adam Funk Newsgroups: comp.lang.python Subject: Re: hashing strings to integers Date: Fri, 23 May 2014 11:36:01 +0100 Organization: $CABAL Lines: 37 Message-ID: References: <05c15bxrpj.ln2@news.ducksburg.com> X-Trace: individual.net J+w9T3fYagGs6bmLBuG7GgPLkRCVGRYnX3v019YvUVg4gMPosU X-Orig-Path: news.ducksburg.com!not-for-mail Cancel-Lock: sha1:LqNkUrVmXWb0P4C9I3fqBmI0OZU= sha1:zW88hWhEFeXhV8MCg9R4XdgyXGM= User-Agent: slrn/pre1.0.2-9 (Linux) Xref: csiph.com comp.lang.python:71923 On 2014-05-23, Adam Funk wrote: > On 2014-05-22, Peter Otten wrote: >> In Python 3 there's int.from_bytes() >> >>>>> h = hashlib.sha1(b"Hello world") >>>>> int.from_bytes(h.digest(), "little") >> 538059071683667711846616050503420899184350089339 > > Excellent, thanks for pointing that out. I've just recently started > using Python 3 instead of 2, & appreciate pointers to new things like > that. BTW, I just tested that & it should be "big" for consistency with the hexdigest: Python 3.3.2+ (default, Feb 28 2014, 00:52:16) [GCC 4.8.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import hashlib >>> h0 = hashlib.sha1(bytes('pants', 'UTF-8')).digest() >>> h1 = hashlib.sha1(bytes('pants', 'UTF-8')).hexdigest() >>> int.from_bytes(h0, 'big') 1315090007003469710610607131160586168131917298749 >>> int.from_bytes(h0, 'little') 352462323236431222976527983157432783788229548774 >>> int(h1, 16) 1315090007003469710610607131160586168131917298749 Thanks. -- The kid's a hot prospect. He's got a good head for merchandising, an agent who can take you downtown and one of the best urine samples I've seen in a long time. [Dead Kennedys t-shirt]