本文整理汇总了Python中Crypto.Hash.SHA属性的典型用法代码示例。如果您正苦于以下问题:Python Hash.SHA属性的具体用法?Python Hash.SHA怎么用?Python Hash.SHA使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类Crypto.Hash
的用法示例。
在下文中一共展示了Hash.SHA属性的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: new
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def new(key, hashAlgo=None, mgfunc=None, label=b('')):
"""Return a cipher object `PKCS1OAEP_Cipher` that can be used to perform PKCS#1 OAEP encryption or decryption.
:Parameters:
key : RSA key object
The key to use to encrypt or decrypt the message. This is a `Crypto.PublicKey.RSA` object.
Decryption is only possible if *key* is a private RSA key.
hashAlgo : hash object
The hash function to use. This can be a module under `Crypto.Hash`
or an existing hash object created from any of such modules. If not specified,
`Crypto.Hash.SHA` (that is, SHA-1) is used.
mgfunc : callable
A mask generation function that accepts two parameters: a string to
use as seed, and the lenth of the mask to generate, in bytes.
If not specified, the standard MGF1 is used (a safe choice).
label : string
A label to apply to this particular encryption. If not specified,
an empty string is used. Specifying a label does not improve
security.
:attention: Modify the mask generation function only if you know what you are doing.
Sender and receiver must use the same one.
"""
return PKCS1OAEP_Cipher(key, hashAlgo, mgfunc, label)
示例2: create_signature
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def create_signature(self, secret_key, to_sign):
sign = HMAC.new(secret_key, to_sign, SHA)
return str(b64encode(sign.hexdigest().encode()))
# creates device spec JSON
示例3: PBKDF1
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def PBKDF1(password, salt, dkLen, count=1000, hashAlgo=None):
"""Derive one key from a password (or passphrase).
This function performs key derivation according an old version of
the PKCS#5 standard (v1.5).
This algorithm is called ``PBKDF1``. Even though it is still described
in the latest version of the PKCS#5 standard (version 2, or RFC2898),
newer applications should use the more secure and versatile `PBKDF2` instead.
:Parameters:
password : string
The secret password or pass phrase to generate the key from.
salt : byte string
An 8 byte string to use for better protection from dictionary attacks.
This value does not need to be kept secret, but it should be randomly
chosen for each derivation.
dkLen : integer
The length of the desired key. Default is 16 bytes, suitable for instance for `Crypto.Cipher.AES`.
count : integer
The number of iterations to carry out. It's recommended to use at least 1000.
hashAlgo : module
The hash algorithm to use, as a module or an object from the `Crypto.Hash` package.
The digest length must be no shorter than ``dkLen``.
The default algorithm is `SHA1`.
:Return: A byte string of length `dkLen` that can be used as key.
"""
if not hashAlgo:
hashAlgo = SHA1
password = tobytes(password)
pHash = hashAlgo.new(password+salt)
digest = pHash.digest_size
if dkLen>digest:
raise ValueError("Selected hash algorithm has a too short digest (%d bytes)." % digest)
if len(salt)!=8:
raise ValueError("Salt is not 8 bytes long.")
for i in xrange(count-1):
pHash = pHash.new(pHash.digest())
return pHash.digest()[:dkLen]
示例4: PBKDF2
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def PBKDF2(password, salt, dkLen=16, count=1000, prf=None):
"""Derive one or more keys from a password (or passphrase).
This performs key derivation according to the PKCS#5 standard (v2.0),
by means of the ``PBKDF2`` algorithm.
:Parameters:
password : string
The secret password or pass phrase to generate the key from.
salt : string
A string to use for better protection from dictionary attacks.
This value does not need to be kept secret, but it should be randomly
chosen for each derivation. It is recommended to be at least 8 bytes long.
dkLen : integer
The cumulative length of the desired keys. Default is 16 bytes, suitable for instance for `Crypto.Cipher.AES`.
count : integer
The number of iterations to carry out. It's recommended to use at least 1000.
prf : callable
A pseudorandom function. It must be a function that returns a pseudorandom string
from two parameters: a secret and a salt. If not specified, HMAC-SHA1 is used.
:Return: A byte string of length `dkLen` that can be used as key material.
If you wanted multiple keys, just break up this string into segments of the desired length.
"""
password = tobytes(password)
if prf is None:
prf = lambda p,s: HMAC.new(p,s,SHA1).digest()
key = b('')
i = 1
while len(key)<dkLen:
U = previousU = prf(password,salt+struct.pack(">I", i))
for j in xrange(count-1):
previousU = t = prf(password,previousU)
U = strxor(U,t)
key += U
i = i + 1
return key[:dkLen]
示例5: get_tests
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def get_tests(config={}):
global test_data
from Crypto.Hash import HMAC, MD5, SHA as SHA1, SHA256
from common import make_mac_tests
hashmods = dict(MD5=MD5, SHA1=SHA1, SHA256=SHA256, default=None)
try:
from Crypto.Hash import SHA224, SHA384, SHA512
hashmods.update(dict(SHA224=SHA224, SHA384=SHA384, SHA512=SHA512))
test_data += hashlib_test_data
except ImportError:
import sys
sys.stderr.write("SelfTest: warning: not testing HMAC-SHA224/384/512 (not available)\n")
return make_mac_tests(HMAC, "HMAC", test_data, hashmods)
示例6: test1
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def test1(self):
v = self._testData[0]
res = PBKDF1(v[0], t2b(v[1]), v[2], v[3], SHA1)
self.assertEqual(res, t2b(v[4]))
示例7: __init__
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def __init__(self, key, hashAlgo, mgfunc, label):
"""Initialize this PKCS#1 OAEP cipher object.
:Parameters:
key : an RSA key object
If a private half is given, both encryption and decryption are possible.
If a public half is given, only encryption is possible.
hashAlgo : hash object
The hash function to use. This can be a module under `Crypto.Hash`
or an existing hash object created from any of such modules. If not specified,
`Crypto.Hash.SHA` (that is, SHA-1) is used.
mgfunc : callable
A mask generation function that accepts two parameters: a string to
use as seed, and the lenth of the mask to generate, in bytes.
If not specified, the standard MGF1 is used (a safe choice).
label : string
A label to apply to this particular encryption. If not specified,
an empty string is used. Specifying a label does not improve
security.
:attention: Modify the mask generation function only if you know what you are doing.
Sender and receiver must use the same one.
"""
self._key = key
if hashAlgo:
self._hashObj = hashAlgo
else:
self._hashObj = Crypto.Hash.SHA
if mgfunc:
self._mgf = mgfunc
else:
self._mgf = lambda x,y: Crypto.Signature.PKCS1_PSS.MGF1(x,y,self._hashObj)
self._label = label
示例8: string_to_key
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def string_to_key(cls, string, salt, params):
(iterations,) = unpack('>L', params or '\x00\x00\x10\x00')
prf = lambda p, s: HMAC.new(p, s, SHA).digest()
seed = PBKDF2(string, salt, cls.seedsize, iterations, prf)
tkey = cls.random_to_key(seed)
return cls.derive(tkey, 'kerberos')
示例9: prf
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def prf(cls, key, string):
return HMAC.new(key.contents, string, SHA).digest()
示例10: _query
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def _query(self, endpoint, params):
# TODO: Figure this bullshit out
import httplib
httplib.HTTPConnection.debuglevel = 1
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
query_url = "%s/%s" % (self.url, endpoint)
hmac = HMAC.new(self.key, digestmod=SHA)
param_string = "=".join(params.items()[0])
sig_data = "GET\n/%s\n%s\n\n" % (endpoint, param_string)
log.debug(sig_data)
hmac.update(sig_data)
signature = hmac.digest()
encoded = base64.b64encode(signature)
r = requests.get(
query_url,
params=params,
verify=False,
headers={
"Accept": self.json_header,
"Authorization": "RiskIQ %s:%s" % (self.token, encoded)
}
)
if r.status_code != requests.codes.ok:
return self._error(
self._get_indicator(params),
endpoint,
r
)
return r.json()
示例11: create_signature
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def create_signature(secret_key, to_sign):
sign = HMAC.new(secret_key, to_sign, SHA)
return str(b64encode(sign.hexdigest().encode()))
示例12: hash_name
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def hash_name(algorithm):
"""
Return RSA hash name for DKIM algorithm.
"""
return {
RSA1: "SHA-1",
RSA256: "SHA-256",
}[algorithm]
示例13: hash_func
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def hash_func(algorithm):
"""
Return RSA hash name for DKIM algorithm.
"""
return {
RSA1: SHA,
RSA256: SHA256,
}[algorithm]
示例14: prf_numbytes
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA [as 别名]
def prf_numbytes(self, secret, label, random, numbytes):
hs = (len(secret)+1)/2
s1 = secret[:hs]
s2 = secret[-hs:]
#print "randlen=",len(random)
#print "hs=",hs
#print "s1=",s1
#print "s2=",s2
#print "label+random=",label+random
#print "label=",label
#1) compute P_md5(secret_part_1, label+random)
md5_hmac=''
block=HMAC.new(key=s1,
msg=label+random,
digestmod=MD5).digest()
while len(md5_hmac)<numbytes:
md5_hmac += HMAC.new(key=s1,
msg=block+label+random,
digestmod=MD5).digest()
block = HMAC.new(key=s1,
msg=block,
digestmod=MD5).digest()
#print [ "%.2x"%ord(i) for i in md5_hmac]
md5_hmac=md5_hmac[:numbytes]
# sha stuff
sha_hmac=''
block=HMAC.new(key=s2,
msg=label+random,
digestmod=SHA).digest()
while len(sha_hmac)<numbytes:
sha_hmac += HMAC.new(key=s2,
msg=block+label+random,
digestmod=SHA).digest()
block = HMAC.new(key=s2,
msg=block,
digestmod=SHA).digest()
#print [ "%.2x"%ord(i) for i in sha_hmac]
# XOR both strings
sha_hmac=sha_hmac[:numbytes]
m = array.array("B",md5_hmac)
s = array.array("B",sha_hmac)
for i in xrange(numbytes):
m[i] ^= s[i]
#print "%0.2x"%m[i],
return m.tostring()
'''
data = ''
for block in self.prf(secret,label,seed):
data +=block
if len(data)>=numbytes:
return data[:numbytes]
'''