[python3.0] s = sha1(random()).hexdigest()

G

gert

from random import random
from hashlib import sha1
s = sha1(random()).hexdigest()

TypeError: object supporting the buffer API required,

How does sha1 work in python3.0 please ?
 
P

Paul Rubin

gert said:
s = sha1(random()).hexdigest()

TypeError: object supporting the buffer API required,

How does sha1 work in python3.0 please ?

sha1 hashes strings, not numbers. Try using str(random()). But if
you want some random hex digits, try os.urandom(10).encode('hex')
rather than messing with sha1.
 
G

gert

from random import random
from hashlib import sha1
s = sha1(random()).hexdigest()

TypeError: object supporting the buffer API required,

How does sha1 work in python3.0 please ?

s = sha1(bytes(random(),'utf-8')).hexdigest()

i found this, looks let say strange. But it works :)
 
G

gert

sha1 hashes strings, not numbers.  Try using str(random()).  But if
you want some random hex digits, try os.urandom(10).encode('hex')
rather than messing with sha1.

s = urandom(10).encode('hex')

AttributeError: 'bytes' object has no attribute 'encode'
 
P

Paul Rubin

gert said:
s = urandom(10).encode('hex')
AttributeError: 'bytes' object has no attribute 'encode'

Oh, Python 3. It's done some different way, someone else will have to
specify. I'm still using 2.x.
 
P

Paul Rubin

gert said:
s = sha1(bytes(random(),'utf-8')).hexdigest()
i found this, looks let say strange. But it works :)

Be careful, if you're relying on the uniqueness and unpredictability
of this number for program security, that random() isn't designed
for such purposes. Use os.urandom instead.
 
M

Martin v. Löwis

s = urandom(10).encode('hex')
AttributeError: 'bytes' object has no attribute 'encode'

py> binascii.hexlify(os.urandom(10))
b'92b91d5734a9fe562f23'

Regards,
Martin
 
G

gert

py> binascii.hexlify(os.urandom(10))
b'92b91d5734a9fe562f23'

sqlite3

s = hexlify(urandom(10))
db.execute('SELECT sid FROM sessions WHERE sid=?',(s))

('SELECT sid FROM sessions WHERE sid=?', b'c916f03d441a0b2b5a9d')
[error] Incorrect number of bindings supplied. The current statement
uses 1, and there are 20 supplied.

???
 
G

gert

py> binascii.hexlify(os.urandom(10))
b'92b91d5734a9fe562f23'

sqlite3

 s = hexlify(urandom(10))
 db.execute('SELECT sid FROM sessions WHERE sid=?',(s))

('SELECT sid FROM sessions WHERE sid=?', b'c916f03d441a0b2b5a9d')
[error] Incorrect number of bindings supplied. The current statement
uses 1, and there are 20 supplied.

???

db.execute('SELECT sid FROM sessions WHERE sid=?',(s,))
This works ?

Is this the new way to create a list in Python3.0 ?
s=('test',)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top