Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'digest': 0.04; 'anyway.': 0.05; '(using': 0.07; 'lately': 0.07; 'string': 0.09; '22,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:keys': 0.09; 'python': 0.11; 'adam': 0.16; 'functions;': 0.16; 'hex': 0.16; 'integer.': 0.16; 'internally': 0.16; "languages'": 0.16; "module's": 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:sqlite3': 0.16; 'ties': 0.16; 'followed': 0.16; 'language': 0.16; 'wrote:': 0.18; 'numerical': 0.19; 'things.': 0.19; 'thu,': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'module,': 0.24; 'string,': 0.24; "haven't": 0.24; 'looks': 0.24; '---': 0.24; 'question': 0.24; "i've": 0.25; 'this:': 0.26; 'least': 0.26; 'header:X-Complaints-To:1': 0.27; 'wondering': 0.29; 'chris': 0.29; 'related': 0.29; 'converting': 0.30; '(which': 0.31; 'getting': 0.31; 'struct': 0.31; 'skip:m 30': 0.32; 'skip:d 20': 0.34; 'but': 0.35; 'there': 0.35; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'easy': 0.60; 'new': 0.61; 'designers': 0.74; 'hardly': 0.84; 'inefficient': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: hashing strings to integers for sqlite3 keys Date: Thu, 22 May 2014 17:34:02 +0200 Organization: None References: <05c15bxrpj.ln2@news.ducksburg.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9323.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1400772863 news.xs4all.nl 2971 [2001:888:2000:d::a6]:58489 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71901 Adam Funk wrote: > On 2014-05-22, Chris Angelico wrote: > >> On Thu, May 22, 2014 at 11:54 PM, Adam Funk wrote: > >>> That ties in with a related question I've been wondering about lately >>> (using MD5s & SHAs for other things) --- getting a hash value (which >>> is internally numeric, rather than string, right?) out as a hex string >>> & then converting that to an int looks inefficient to me --- is there >>> any better way to get an int? (I haven't seen any other way in the >>> API.) >> >> I don't know that there is, at least not with hashlib. You might be >> able to use digest() followed by the struct module, but it's no less >> convoluted. It's the same in several other languages' hashing >> functions; the result is a string, not an integer. > > Well, J*v* returns a byte array, so I used to do this: > > digester = MessageDigest.getInstance("MD5"); > ... > digester.reset(); > byte[] digest = digester.digest(bytes); > return new BigInteger(+1, digest); In Python 3 there's int.from_bytes() >>> h = hashlib.sha1(b"Hello world") >>> int.from_bytes(h.digest(), "little") 538059071683667711846616050503420899184350089339 > I dunno why language designers don't make it easy to get a single big > number directly out of these things. You hardly ever need to manipulate the numerical value of the digest. And on its way into the database it will be re-serialized anyway. > I just had a look at the struct module's fearsome documentation & > think it would present a good shoot(self, foot) opportunity.