made pbkdf2 funuctional in python 3
started refactoring other code to work in python 3 --HG-- branch : mung
This commit is contained in:
parent
0b3c747e93
commit
25db8c81e0
8 changed files with 233 additions and 219 deletions
|
@ -1,2 +1,5 @@
|
||||||
syntax: glob
|
syntax: glob
|
||||||
*.pyc
|
*.pyc
|
||||||
|
bin
|
||||||
|
lib
|
||||||
|
include
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from builtins_code import bi
|
from bi_code import bi
|
||||||
|
|
||||||
__all__ = ['builtin_map']
|
__all__ = ['builtin_map']
|
||||||
|
|
14
bytecode.py
14
bytecode.py
|
@ -1,5 +1,5 @@
|
||||||
from language_tools import *
|
from language_tools import *
|
||||||
from builtins import builtin_map
|
from bi import builtin_map
|
||||||
import optimizer
|
import optimizer
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ def coerce(value):
|
||||||
return VMInteger(value)
|
return VMInteger(value)
|
||||||
elif isinstance(value, (tuple, list)):
|
elif isinstance(value, (tuple, list)):
|
||||||
return VMList(list(value))
|
return VMList(list(value))
|
||||||
elif isinstance(value, unicode):
|
elif isinstance(value, str):
|
||||||
return VMString(value)
|
return VMString(value)
|
||||||
elif isinstance(value, dict):
|
elif isinstance(value, dict):
|
||||||
return VMTable(value)
|
return VMTable(value)
|
||||||
|
@ -298,7 +298,7 @@ class UnaryOp(CodeOp):
|
||||||
rv = []
|
rv = []
|
||||||
ops = []
|
ops = []
|
||||||
for t in tokens:
|
for t in tokens:
|
||||||
if isinstance(t, (str, unicode)) and t in UnaryOp.map:
|
if isinstance(t, (str, bytes)) and t in UnaryOp.map:
|
||||||
ops.append(UnaryOp(UnaryOp.map[t]))
|
ops.append(UnaryOp(UnaryOp.map[t]))
|
||||||
else:
|
else:
|
||||||
rv.append(t)
|
rv.append(t)
|
||||||
|
@ -524,7 +524,7 @@ class WhileBlock(CodeOp):
|
||||||
def parse(tokens):
|
def parse(tokens):
|
||||||
rv = WhileBlock()
|
rv = WhileBlock()
|
||||||
|
|
||||||
for i in xrange(1, len(tokens)):
|
for i in range(1, len(tokens)):
|
||||||
tok = tokens[i]
|
tok = tokens[i]
|
||||||
if rv.cond == None:
|
if rv.cond == None:
|
||||||
rv.cond = tok
|
rv.cond = tok
|
||||||
|
@ -583,7 +583,7 @@ class TryBlock(CodeOp):
|
||||||
rv = TryBlock()
|
rv = TryBlock()
|
||||||
|
|
||||||
active_tok = None
|
active_tok = None
|
||||||
for i in xrange(1, len(tokens)):
|
for i in range(1, len(tokens)):
|
||||||
tok = tokens[i]
|
tok = tokens[i]
|
||||||
if tok in ("try", "except", "else", "finally"):
|
if tok in ("try", "except", "else", "finally"):
|
||||||
active_tok = tok
|
active_tok = tok
|
||||||
|
@ -677,7 +677,7 @@ class ForeachBlock(CodeOp):
|
||||||
def parse(tokens):
|
def parse(tokens):
|
||||||
rv = ForeachBlock()
|
rv = ForeachBlock()
|
||||||
|
|
||||||
for i in xrange(1, len(tokens)):
|
for i in range(1, len(tokens)):
|
||||||
tok = tokens[i]
|
tok = tokens[i]
|
||||||
if rv.var == None:
|
if rv.var == None:
|
||||||
rv.var = tok
|
rv.var = tok
|
||||||
|
@ -717,7 +717,7 @@ class IfBlock(CodeOp):
|
||||||
conds = []
|
conds = []
|
||||||
tok_count = 0
|
tok_count = 0
|
||||||
active_tok = None
|
active_tok = None
|
||||||
for i in xrange(len(tokens)):
|
for i in range(len(tokens)):
|
||||||
tok = tokens[i]
|
tok = tokens[i]
|
||||||
|
|
||||||
if tok == "endif":
|
if tok == "endif":
|
||||||
|
|
|
@ -12,7 +12,7 @@ def disallow_keywords(tokens,keywords=None):
|
||||||
if isinstance(t, VMIdent):
|
if isinstance(t, VMIdent):
|
||||||
if t.name in keywords:
|
if t.name in keywords:
|
||||||
raise ParseException("Restricted keyword: %s" % (t.name,))
|
raise ParseException("Restricted keyword: %s" % (t.name,))
|
||||||
elif isinstance(t, unicode):
|
elif isinstance(t, str):
|
||||||
tstr = t.encode('ascii', 'ignore')
|
tstr = t.encode('ascii', 'ignore')
|
||||||
if tstr in keywords:
|
if tstr in keywords:
|
||||||
raise ParseException("Restricted keyword: %s" % (tstr,))
|
raise ParseException("Restricted keyword: %s" % (tstr,))
|
||||||
|
@ -98,10 +98,10 @@ class VMTablePair(VMType):
|
||||||
class VMString(VMType):
|
class VMString(VMType):
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
VMType.__init__(self)
|
VMType.__init__(self)
|
||||||
if isinstance(value, unicode):
|
if isinstance(value, str):
|
||||||
self.value = value
|
self.value = value
|
||||||
else:
|
else:
|
||||||
self.value = unicode(str(value), 'ascii', 'ignore')
|
self.value = str(value, 'ascii', 'ignore')
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "\"%s\"" % (repr(self.value)[1:].strip("'").replace("\\'", "'").replace('"', '\\"'),)
|
return "\"%s\"" % (repr(self.value)[1:].strip("'").replace("\\'", "'").replace('"', '\\"'),)
|
||||||
|
@ -138,7 +138,7 @@ class VMIdent(VMRef):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
def bytecode(self):
|
def bytecode(self):
|
||||||
return [StackLiteral(unicode(self.name))]
|
return [StackLiteral(str(self.name))]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@tokenparser
|
@tokenparser
|
||||||
|
@ -156,7 +156,7 @@ class VMVariable(VMRef):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
def ref(self):
|
def ref(self):
|
||||||
return [StackLiteral(unicode(self.name))]
|
return [StackLiteral(str(self.name))]
|
||||||
|
|
||||||
def bytecode(self):
|
def bytecode(self):
|
||||||
return codejoin(self.ref(), GetVariable())
|
return codejoin(self.ref(), GetVariable())
|
||||||
|
|
|
@ -92,12 +92,12 @@ class Connection(object):
|
||||||
data = self.conn.recv(bytes)
|
data = self.conn.recv(bytes)
|
||||||
|
|
||||||
if self.input_encoding == 'raw':
|
if self.input_encoding == 'raw':
|
||||||
return unicode(self.escape(data), 'ascii')
|
return str(self.escape(data), 'ascii')
|
||||||
else:
|
else:
|
||||||
return unicode(data, self.input_encoding, 'ignore')
|
return str(data, self.input_encoding, 'ignore')
|
||||||
|
|
||||||
def send(self, data):
|
def send(self, data):
|
||||||
assert isinstance(data, unicode)
|
assert isinstance(data, str)
|
||||||
try:
|
try:
|
||||||
encoded = data.encode(self.output_encoding, 'replace')
|
encoded = data.encode(self.output_encoding, 'replace')
|
||||||
except UnicodeEncodeError:
|
except UnicodeEncodeError:
|
||||||
|
|
9
parse.py
9
parse.py
|
@ -80,12 +80,14 @@ class Parser(object):
|
||||||
literal = integer | fnumber | stringlit | listlit | objref
|
literal = integer | fnumber | stringlit | listlit | objref
|
||||||
|
|
||||||
atom << (Optional(minus) + ZeroOrMore(neg) + (propref | literal | bifunction | bexpr | variable | funccall | fileref)).setParseAction(UnaryOp.parse)
|
atom << (Optional(minus) + ZeroOrMore(neg) + (propref | literal | bifunction | bexpr | variable | funccall | fileref)).setParseAction(UnaryOp.parse)
|
||||||
|
atom = atom.streamline()
|
||||||
|
|
||||||
|
|
||||||
# by defining exponentiation as "atom [ ^ factor ]..." instead of "atom [ ^ atom ]...", we get right-to-left exponents, instead of left-to-righ
|
# by defining exponentiation as "atom [ ^ factor ]..." instead of "atom [ ^ atom ]...", we get right-to-left exponents, instead of left-to-righ
|
||||||
# that is, 2^3^2 = 2^(3^2), not (2^3)^2.
|
# that is, 2^3^2 = 2^(3^2), not (2^3)^2.
|
||||||
factor = Forward()
|
factor = Forward()
|
||||||
factor << atom + ZeroOrMore( (expop + factor).setParseAction(ArithExp.parse) )
|
factor << atom + ZeroOrMore( (expop + factor).setParseAction(ArithExp.parse) )
|
||||||
|
factor = factor.streamline()
|
||||||
|
|
||||||
term = factor + ZeroOrMore( (multop + factor).setParseAction(ArithMul.parse) )
|
term = factor + ZeroOrMore( (multop + factor).setParseAction(ArithMul.parse) )
|
||||||
#term.setParseAction(self.nest)
|
#term.setParseAction(self.nest)
|
||||||
|
@ -114,6 +116,8 @@ class Parser(object):
|
||||||
assignable = variable | propref | fileref
|
assignable = variable | propref | fileref
|
||||||
assignexpr = Optional(assignable + assign) + boolexpr
|
assignexpr = Optional(assignable + assign) + boolexpr
|
||||||
expr << assignexpr.setParseAction(Assignment.parse)
|
expr << assignexpr.setParseAction(Assignment.parse)
|
||||||
|
expr = expr.streamline()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
""" phase 2:
|
""" phase 2:
|
||||||
|
@ -167,6 +171,9 @@ class Parser(object):
|
||||||
block << (exprblock + Optional(ifblock | tryblock | whileblock | forblock) + exprblock)
|
block << (exprblock + Optional(ifblock | tryblock | whileblock | forblock) + exprblock)
|
||||||
lblock << (lexprblock + Optional(iflblock | trylblock | whileblock | forblock) + lexprblock)
|
lblock << (lexprblock + Optional(iflblock | trylblock | whileblock | forblock) + lexprblock)
|
||||||
|
|
||||||
|
block = block.streamline()
|
||||||
|
lblock = lblock.streamline()
|
||||||
|
|
||||||
block.setParseAction(self.nest)
|
block.setParseAction(self.nest)
|
||||||
lblock.setParseAction(self.nest)
|
lblock.setParseAction(self.nest)
|
||||||
|
|
||||||
|
@ -197,7 +204,7 @@ class Parser(object):
|
||||||
def test(self):
|
def test(self):
|
||||||
#print(self.parse(u"if (1) #740.xyz + -hello.world; endif"))
|
#print(self.parse(u"if (1) #740.xyz + -hello.world; endif"))
|
||||||
|
|
||||||
data = unicode(open("test.moo", "r").read(), 'utf-8')
|
data = open("test.moo", "r", encoding="utf-8").read()
|
||||||
rv = self.parse(data)
|
rv = self.parse(data)
|
||||||
print(rv)
|
print(rv)
|
||||||
return rv
|
return rv
|
||||||
|
|
22
pbkdf2.py
22
pbkdf2.py
|
@ -44,7 +44,8 @@ import hmac
|
||||||
import hashlib
|
import hashlib
|
||||||
from struct import Struct
|
from struct import Struct
|
||||||
from operator import xor
|
from operator import xor
|
||||||
from itertools import izip, starmap
|
from itertools import starmap
|
||||||
|
import binascii
|
||||||
|
|
||||||
|
|
||||||
_pack_int = Struct('>I').pack
|
_pack_int = Struct('>I').pack
|
||||||
|
@ -52,7 +53,7 @@ _pack_int = Struct('>I').pack
|
||||||
|
|
||||||
def pbkdf2_hex(data, salt, iterations=1000, keylen=24, hashfunc=None):
|
def pbkdf2_hex(data, salt, iterations=1000, keylen=24, hashfunc=None):
|
||||||
"""Like :func:`pbkdf2_bin` but returns a hex encoded string."""
|
"""Like :func:`pbkdf2_bin` but returns a hex encoded string."""
|
||||||
return pbkdf2_bin(data, salt, iterations, keylen, hashfunc).encode('hex')
|
return str(binascii.hexlify(pbkdf2_bin(data, salt, iterations, keylen, hashfunc)), 'ascii')
|
||||||
|
|
||||||
|
|
||||||
def pbkdf2_bin(data, salt, iterations=1000, keylen=24, hashfunc=None):
|
def pbkdf2_bin(data, salt, iterations=1000, keylen=24, hashfunc=None):
|
||||||
|
@ -61,26 +62,29 @@ def pbkdf2_bin(data, salt, iterations=1000, keylen=24, hashfunc=None):
|
||||||
key of `keylen` bytes. By default SHA-256 is used as hash function,
|
key of `keylen` bytes. By default SHA-256 is used as hash function,
|
||||||
a different hashlib `hashfunc` can be provided.
|
a different hashlib `hashfunc` can be provided.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
bchr = lambda v: bytes((v,))
|
||||||
|
|
||||||
hashfunc = hashfunc or hashlib.sha256
|
hashfunc = hashfunc or hashlib.sha256
|
||||||
mac = hmac.new(data, None, hashfunc)
|
mac = hmac.new(data, None, hashfunc)
|
||||||
def _pseudorandom(x, mac=mac):
|
def _pseudorandom(x, mac=mac):
|
||||||
h = mac.copy()
|
h = mac.copy()
|
||||||
h.update(x)
|
h.update(x)
|
||||||
return map(ord, h.digest())
|
return h.digest()
|
||||||
buf = []
|
buf = []
|
||||||
for block in xrange(1, -(-keylen // mac.digest_size) + 1):
|
for block in range(1, -(-keylen // mac.digest_size) + 1):
|
||||||
rv = u = _pseudorandom(salt + _pack_int(block))
|
rv = u = _pseudorandom(salt + _pack_int(block))
|
||||||
for i in xrange(iterations - 1):
|
for i in range(iterations - 1):
|
||||||
u = _pseudorandom(''.join(map(chr, u)))
|
u = _pseudorandom(b''.join(map(bchr, u)))
|
||||||
rv = starmap(xor, izip(rv, u))
|
rv = starmap(xor, zip(rv, u))
|
||||||
buf.extend(rv)
|
buf.extend(rv)
|
||||||
return ''.join(map(chr, buf))[:keylen]
|
return b''.join(map(bchr, buf))[:keylen]
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
failed = []
|
failed = []
|
||||||
def check(data, salt, iterations, keylen, expected):
|
def check(data, salt, iterations, keylen, expected):
|
||||||
rv = pbkdf2_hex(data, salt, iterations, keylen)
|
rv = pbkdf2_hex(bytes(data, "utf-8"), bytes(salt, "utf-8"), iterations, keylen, hashlib.sha1)
|
||||||
if rv != expected:
|
if rv != expected:
|
||||||
print('Test failed:')
|
print('Test failed:')
|
||||||
print(' Expected: %s' % expected)
|
print(' Expected: %s' % expected)
|
||||||
|
|
Loading…
Add table
Reference in a new issue