rev280, The whole project reformatted to PEP8, UiRequest getPosted to query posted variables
This commit is contained in:
parent
a5741704e4
commit
b5ecb62bc6
49 changed files with 5704 additions and 5205 deletions
|
@ -1,72 +1,75 @@
|
|||
import logging
|
||||
|
||||
from lib.BitcoinECC import BitcoinECC
|
||||
from lib.pybitcointools import bitcoin as btctools
|
||||
import logging
|
||||
from Config import config
|
||||
|
||||
# Try to load openssl
|
||||
try:
|
||||
if not config.use_openssl: raise Exception("Disabled by config")
|
||||
from lib.opensslVerify import opensslVerify
|
||||
logging.info("OpenSSL loaded, version: %s" % opensslVerify.openssl_version)
|
||||
if not config.use_openssl:
|
||||
raise Exception("Disabled by config")
|
||||
from lib.opensslVerify import opensslVerify
|
||||
logging.info("OpenSSL loaded, version: %s" % opensslVerify.openssl_version)
|
||||
except Exception, err:
|
||||
logging.info("OpenSSL load failed: %s, falling back to slow bitcoin verify" % err)
|
||||
opensslVerify = None
|
||||
logging.info("OpenSSL load failed: %s, falling back to slow bitcoin verify" % err)
|
||||
opensslVerify = None
|
||||
|
||||
|
||||
def newPrivatekey(uncompressed=True): # Return new private key
|
||||
privatekey = btctools.encode_privkey(btctools.random_key(), "wif")
|
||||
return privatekey
|
||||
def newPrivatekey(uncompressed=True): # Return new private key
|
||||
privatekey = btctools.encode_privkey(btctools.random_key(), "wif")
|
||||
return privatekey
|
||||
|
||||
|
||||
def newSeed():
|
||||
return btctools.random_key()
|
||||
return btctools.random_key()
|
||||
|
||||
|
||||
def hdPrivatekey(seed, child):
|
||||
masterkey = btctools.bip32_master_key(seed)
|
||||
childkey = btctools.bip32_ckd(masterkey, child % 100000000) # Too large child id could cause problems
|
||||
key = btctools.bip32_extract_key(childkey)
|
||||
return btctools.encode_privkey(key, "wif")
|
||||
masterkey = btctools.bip32_master_key(seed)
|
||||
childkey = btctools.bip32_ckd(masterkey, child % 100000000) # Too large child id could cause problems
|
||||
key = btctools.bip32_extract_key(childkey)
|
||||
return btctools.encode_privkey(key, "wif")
|
||||
|
||||
|
||||
def privatekeyToAddress(privatekey): # Return address from private key
|
||||
if privatekey.startswith("23") and len(privatekey) > 52: # Backward compatibility to broken lib
|
||||
bitcoin = BitcoinECC.Bitcoin()
|
||||
bitcoin.BitcoinAddressFromPrivate(privatekey)
|
||||
return bitcoin.BitcoinAddresFromPublicKey()
|
||||
else:
|
||||
try:
|
||||
return btctools.privkey_to_address(privatekey)
|
||||
except Exception, err: # Invalid privatekey
|
||||
return False
|
||||
def privatekeyToAddress(privatekey): # Return address from private key
|
||||
if privatekey.startswith("23") and len(privatekey) > 52: # Backward compatibility to broken lib
|
||||
bitcoin = BitcoinECC.Bitcoin()
|
||||
bitcoin.BitcoinAddressFromPrivate(privatekey)
|
||||
return bitcoin.BitcoinAddresFromPublicKey()
|
||||
else:
|
||||
try:
|
||||
return btctools.privkey_to_address(privatekey)
|
||||
except Exception: # Invalid privatekey
|
||||
return False
|
||||
|
||||
|
||||
def sign(data, privatekey): # Return sign to data using private key
|
||||
if privatekey.startswith("23") and len(privatekey) > 52: return None # Old style private key not supported
|
||||
sign = btctools.ecdsa_sign(data, privatekey)
|
||||
return sign
|
||||
def sign(data, privatekey): # Return sign to data using private key
|
||||
if privatekey.startswith("23") and len(privatekey) > 52:
|
||||
return None # Old style private key not supported
|
||||
sign = btctools.ecdsa_sign(data, privatekey)
|
||||
return sign
|
||||
|
||||
|
||||
def signOld(data, privatekey): # Return sign to data using private key (backward compatible old style)
|
||||
bitcoin = BitcoinECC.Bitcoin()
|
||||
bitcoin.BitcoinAddressFromPrivate(privatekey)
|
||||
sign = bitcoin.SignECDSA(data)
|
||||
return sign
|
||||
def signOld(data, privatekey): # Return sign to data using private key (backward compatible old style)
|
||||
bitcoin = BitcoinECC.Bitcoin()
|
||||
bitcoin.BitcoinAddressFromPrivate(privatekey)
|
||||
sign = bitcoin.SignECDSA(data)
|
||||
return sign
|
||||
|
||||
|
||||
def verify(data, address, sign): # Verify data using address and sign
|
||||
if hasattr(sign, "endswith"):
|
||||
if opensslVerify: # Use the faster method if avalible
|
||||
pub = opensslVerify.getMessagePubkey(data, sign)
|
||||
sign_address = btctools.pubtoaddr(pub)
|
||||
else: # Use pure-python
|
||||
pub = btctools.ecdsa_recover(data, sign)
|
||||
sign_address = btctools.pubtoaddr(pub)
|
||||
|
||||
if type(address) is list: # Any address in the list
|
||||
return sign_address in address
|
||||
else: # One possible address
|
||||
return sign_address == address
|
||||
else: # Backward compatible old style
|
||||
bitcoin = BitcoinECC.Bitcoin()
|
||||
return bitcoin.VerifyMessageFromBitcoinAddress(address, data, sign)
|
||||
def verify(data, address, sign): # Verify data using address and sign
|
||||
if hasattr(sign, "endswith"):
|
||||
if opensslVerify: # Use the faster method if avalible
|
||||
pub = opensslVerify.getMessagePubkey(data, sign)
|
||||
sign_address = btctools.pubtoaddr(pub)
|
||||
else: # Use pure-python
|
||||
pub = btctools.ecdsa_recover(data, sign)
|
||||
sign_address = btctools.pubtoaddr(pub)
|
||||
|
||||
if type(address) is list: # Any address in the list
|
||||
return sign_address in address
|
||||
else: # One possible address
|
||||
return sign_address == address
|
||||
else: # Backward compatible old style
|
||||
bitcoin = BitcoinECC.Bitcoin()
|
||||
return bitcoin.VerifyMessageFromBitcoinAddress(address, data, sign)
|
||||
|
|
|
@ -4,103 +4,104 @@ import os
|
|||
import ssl
|
||||
|
||||
from Config import config
|
||||
import gevent
|
||||
from util import SslPatch
|
||||
|
||||
|
||||
class CryptConnectionManager:
|
||||
def __init__(self):
|
||||
# OpenSSL params
|
||||
if sys.platform.startswith("win"):
|
||||
self.openssl_bin = "src\\lib\\opensslVerify\\openssl.exe"
|
||||
else:
|
||||
self.openssl_bin = "openssl"
|
||||
self.openssl_env = {"OPENSSL_CONF": "src/lib/opensslVerify/openssl.cnf"}
|
||||
def __init__(self):
|
||||
# OpenSSL params
|
||||
if sys.platform.startswith("win"):
|
||||
self.openssl_bin = "src\\lib\\opensslVerify\\openssl.exe"
|
||||
else:
|
||||
self.openssl_bin = "openssl"
|
||||
self.openssl_env = {"OPENSSL_CONF": "src/lib/opensslVerify/openssl.cnf"}
|
||||
|
||||
self.crypt_supported = [] # Supported cryptos
|
||||
self.crypt_supported = [] # Supported cryptos
|
||||
|
||||
# Select crypt that supported by both sides
|
||||
# Return: Name of the crypto
|
||||
def selectCrypt(self, client_supported):
|
||||
for crypt in self.crypt_supported:
|
||||
if crypt in client_supported:
|
||||
return crypt
|
||||
return False
|
||||
|
||||
# Wrap socket for crypt
|
||||
# Return: wrapped socket
|
||||
def wrapSocket(self, sock, crypt, server=False):
|
||||
if crypt == "tls-rsa":
|
||||
ciphers = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:AES128-GCM-SHA256:AES128-SHA256:HIGH:"
|
||||
ciphers += "!aNULL:!eNULL:!EXPORT:!DSS:!DES:!RC4:!3DES:!MD5:!PSK"
|
||||
if server:
|
||||
return ssl.wrap_socket(
|
||||
sock, server_side=server, keyfile='%s/key-rsa.pem' % config.data_dir,
|
||||
certfile='%s/cert-rsa.pem' % config.data_dir, ciphers=ciphers)
|
||||
else:
|
||||
return ssl.wrap_socket(sock, ciphers=ciphers)
|
||||
else:
|
||||
return sock
|
||||
|
||||
def removeCerts(self):
|
||||
for file_name in ["cert-rsa.pem", "key-rsa.pem"]:
|
||||
file_path = "%s/%s" % (config.data_dir, file_name)
|
||||
if os.path.isfile(file_path):
|
||||
os.unlink(file_path)
|
||||
|
||||
# Load and create cert files is necessary
|
||||
def loadCerts(self):
|
||||
if config.disable_encryption:
|
||||
return False
|
||||
|
||||
if self.loadSslRsaCert():
|
||||
self.crypt_supported.append("tls-rsa")
|
||||
|
||||
# Try to create RSA server cert + sign for connection encryption
|
||||
# Return: True on success
|
||||
def loadSslRsaCert(self):
|
||||
import subprocess
|
||||
|
||||
if os.path.isfile("%s/cert-rsa.pem" % config.data_dir) and os.path.isfile("%s/key-rsa.pem" % config.data_dir):
|
||||
return True # Files already exits
|
||||
|
||||
back = subprocess.Popen(
|
||||
"%s req -x509 -newkey rsa:2048 -sha256 -batch -keyout %s/key-rsa.pem -out %s/cert-rsa.pem -nodes -config %s" % (
|
||||
self.openssl_bin, config.data_dir, config.data_dir, self.openssl_env["OPENSSL_CONF"]
|
||||
),
|
||||
shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, env=self.openssl_env
|
||||
).stdout.read().strip()
|
||||
logging.debug("Generating RSA cert and key PEM files...%s" % back)
|
||||
|
||||
if os.path.isfile("%s/cert-rsa.pem" % config.data_dir) and os.path.isfile("%s/key-rsa.pem" % config.data_dir):
|
||||
return True
|
||||
else:
|
||||
logging.error("RSA ECC SSL cert generation failed, cert or key files not exits.")
|
||||
return False
|
||||
|
||||
# Not used yet: Missing on some platform
|
||||
def createSslEccCert(self):
|
||||
return False
|
||||
import subprocess
|
||||
|
||||
# Create ECC privatekey
|
||||
back = subprocess.Popen(
|
||||
"%s ecparam -name prime256v1 -genkey -out %s/key-ecc.pem" % (self.openssl_bin, config.data_dir),
|
||||
shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, env=self.openssl_env
|
||||
).stdout.read().strip()
|
||||
self.log.debug("Generating ECC privatekey PEM file...%s" % back)
|
||||
|
||||
# Create ECC cert
|
||||
back = subprocess.Popen(
|
||||
"%s req -new -key %s/key-ecc.pem -x509 -nodes -out %s/cert-ecc.pem -config %s" % (
|
||||
self.openssl_bin, config.data_dir, config.data_dir, self.openssl_env["OPENSSL_CONF"]),
|
||||
shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, env=self.openssl_env
|
||||
).stdout.read().strip()
|
||||
self.log.debug("Generating ECC cert PEM file...%s" % back)
|
||||
|
||||
if os.path.isfile("%s/cert-ecc.pem" % config.data_dir) and os.path.isfile("%s/key-ecc.pem" % config.data_dir):
|
||||
return True
|
||||
else:
|
||||
self.logging.error("ECC SSL cert generation failed, cert or key files not exits.")
|
||||
return False
|
||||
|
||||
|
||||
# Select crypt that supported by both sides
|
||||
# Return: Name of the crypto
|
||||
def selectCrypt(self, client_supported):
|
||||
for crypt in self.crypt_supported:
|
||||
if crypt in client_supported:
|
||||
return crypt
|
||||
return False
|
||||
|
||||
|
||||
# Wrap socket for crypt
|
||||
# Return: wrapped socket
|
||||
def wrapSocket(self, sock, crypt, server=False):
|
||||
if crypt == "tls-rsa":
|
||||
ciphers = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:AES128-GCM-SHA256:AES128-SHA256:HIGH:!aNULL:!eNULL:!EXPORT:!DSS:!DES:!RC4:!3DES:!MD5:!PSK"
|
||||
if server:
|
||||
return ssl.wrap_socket(sock, server_side=server, keyfile='%s/key-rsa.pem' % config.data_dir, certfile='%s/cert-rsa.pem' % config.data_dir, ciphers=ciphers)
|
||||
else:
|
||||
return ssl.wrap_socket(sock, ciphers=ciphers)
|
||||
else:
|
||||
return sock
|
||||
|
||||
|
||||
def removeCerts(self):
|
||||
for file_name in ["cert-rsa.pem", "key-rsa.pem"]:
|
||||
file_path = "%s/%s" % (config.data_dir, file_name)
|
||||
if os.path.isfile(file_path): os.unlink(file_path)
|
||||
|
||||
|
||||
# Load and create cert files is necessary
|
||||
def loadCerts(self):
|
||||
if config.disable_encryption: return False
|
||||
|
||||
if self.loadSslRsaCert():
|
||||
self.crypt_supported.append("tls-rsa")
|
||||
|
||||
|
||||
# Try to create RSA server cert + sign for connection encryption
|
||||
# Return: True on success
|
||||
def loadSslRsaCert(self):
|
||||
import subprocess
|
||||
|
||||
if os.path.isfile("%s/cert-rsa.pem" % config.data_dir) and os.path.isfile("%s/key-rsa.pem" % config.data_dir):
|
||||
return True # Files already exits
|
||||
|
||||
back = subprocess.Popen(
|
||||
"%s req -x509 -newkey rsa:2048 -sha256 -batch -keyout %s/key-rsa.pem -out %s/cert-rsa.pem -nodes -config %s" % (self.openssl_bin, config.data_dir, config.data_dir, self.openssl_env["OPENSSL_CONF"]),
|
||||
shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, env=self.openssl_env
|
||||
).stdout.read().strip()
|
||||
logging.debug("Generating RSA cert and key PEM files...%s" % back)
|
||||
|
||||
if os.path.isfile("%s/cert-rsa.pem" % config.data_dir) and os.path.isfile("%s/key-rsa.pem" % config.data_dir):
|
||||
return True
|
||||
else:
|
||||
logging.error("RSA ECC SSL cert generation failed, cert or key files not exits.")
|
||||
return False
|
||||
|
||||
|
||||
# Not used yet: Missing on some platform
|
||||
def createSslEccCert(self):
|
||||
return False
|
||||
import subprocess
|
||||
|
||||
# Create ECC privatekey
|
||||
back = subprocess.Popen(
|
||||
"%s ecparam -name prime256v1 -genkey -out %s/key-ecc.pem" % (self.openssl_bin, config.data_dir),
|
||||
shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, env=self.openssl_env
|
||||
).stdout.read().strip()
|
||||
self.log.debug("Generating ECC privatekey PEM file...%s" % back)
|
||||
|
||||
# Create ECC cert
|
||||
back = subprocess.Popen(
|
||||
"%s req -new -key %s/key-ecc.pem -x509 -nodes -out %s/cert-ecc.pem -config %s" % (self.openssl_bin, config.data_dir, config.data_dir, self.openssl_env["OPENSSL_CONF"]),
|
||||
shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, env=self.openssl_env
|
||||
).stdout.read().strip()
|
||||
self.log.debug("Generating ECC cert PEM file...%s" % back)
|
||||
|
||||
if os.path.isfile("%s/cert-ecc.pem" % config.data_dir) and os.path.isfile("%s/key-ecc.pem" % config.data_dir):
|
||||
return True
|
||||
else:
|
||||
self.logging.error("ECC SSL cert generation failed, cert or key files not exits.")
|
||||
return False
|
||||
|
||||
|
||||
manager = CryptConnectionManager()
|
||||
manager = CryptConnectionManager()
|
||||
|
|
|
@ -1,36 +1,37 @@
|
|||
import hashlib
|
||||
|
||||
|
||||
def sha1sum(file, blocksize=65536):
|
||||
if hasattr(file, "endswith"): # Its a string open it
|
||||
file = open(file, "rb")
|
||||
hash = hashlib.sha1()
|
||||
for block in iter(lambda: file.read(blocksize), ""):
|
||||
hash.update(block)
|
||||
return hash.hexdigest()
|
||||
if hasattr(file, "endswith"): # Its a string open it
|
||||
file = open(file, "rb")
|
||||
hash = hashlib.sha1()
|
||||
for block in iter(lambda: file.read(blocksize), ""):
|
||||
hash.update(block)
|
||||
return hash.hexdigest()
|
||||
|
||||
|
||||
def sha512sum(file, blocksize=65536):
|
||||
if hasattr(file, "endswith"): # Its a string open it
|
||||
file = open(file, "rb")
|
||||
hash = hashlib.sha512()
|
||||
for block in iter(lambda: file.read(blocksize), ""):
|
||||
hash.update(block)
|
||||
return hash.hexdigest()[0:64] # Truncate to 256bits is good enough
|
||||
if hasattr(file, "endswith"): # Its a string open it
|
||||
file = open(file, "rb")
|
||||
hash = hashlib.sha512()
|
||||
for block in iter(lambda: file.read(blocksize), ""):
|
||||
hash.update(block)
|
||||
return hash.hexdigest()[0:64] # Truncate to 256bits is good enough
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import cStringIO as StringIO
|
||||
a = StringIO.StringIO()
|
||||
a.write("hello!")
|
||||
a.seek(0)
|
||||
print hashlib.sha1("hello!").hexdigest()
|
||||
print sha1sum(a)
|
||||
import cStringIO as StringIO
|
||||
a = StringIO.StringIO()
|
||||
a.write("hello!")
|
||||
a.seek(0)
|
||||
print hashlib.sha1("hello!").hexdigest()
|
||||
print sha1sum(a)
|
||||
|
||||
import time
|
||||
s = time.time()
|
||||
print sha1sum(open("F:\\Temp\\bigfile")),
|
||||
print time.time()-s
|
||||
import time
|
||||
s = time.time()
|
||||
print sha1sum(open("F:\\Temp\\bigfile")),
|
||||
print time.time() - s
|
||||
|
||||
s = time.time()
|
||||
print sha512sum(open("F:\\Temp\\bigfile")),
|
||||
print time.time()-s
|
||||
s = time.time()
|
||||
print sha512sum(open("F:\\Temp\\bigfile")),
|
||||
print time.time() - s
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue