本文整理汇总了Python中hashlib.sha256方法的典型用法代码示例。如果您正苦于以下问题:Python hashlib.sha256方法的具体用法?Python hashlib.sha256怎么用?Python hashlib.sha256使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hashlib
的用法示例。
在下文中一共展示了hashlib.sha256方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: url_to_filename
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def url_to_filename(url: str, etag: str = None) -> str:
"""
Convert `url` into a hashed filename in a repeatable way.
If `etag` is specified, append its hash to the url's, delimited
by a period.
"""
url_bytes = url.encode('utf-8')
url_hash = sha256(url_bytes)
filename = url_hash.hexdigest()
if etag:
etag_bytes = etag.encode('utf-8')
etag_hash = sha256(etag_bytes)
filename += '.' + etag_hash.hexdigest()
return filename
示例2: verify
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def verify(self, authenticator_data, client_data_json, signature, user_handle, raw_id, email):
"Ascertain the validity of credentials supplied by the client user agent via navigator.credentials.get()"
email = email.decode()
if not re.match(r"[^@]+@[^@]+\.[^@]+", email):
raise Exception("Invalid email address")
client_data_hash = hashlib.sha256(client_data_json).digest()
client_data = json.loads(client_data_json)
assert client_data["type"] == "webauthn.get"
expect_challenge = self.storage_backend.get_challenge_for_user(email=email, type="authentication")
assert b64url_decode(client_data["challenge"]) == expect_challenge
print("expect RP ID:", self.rp_id)
if self.rp_id:
assert "https://" + self.rp_id == client_data["origin"]
# Verify that the value of C.origin matches the Relying Party's origin.
# Verify that the RP ID hash in authData is indeed the SHA-256 hash of the RP ID expected by the RP.
authenticator_data = AuthenticatorData(authenticator_data)
assert authenticator_data.user_present
credential = self.storage_backend.get_credential_by_email(email)
credential.verify(signature, authenticator_data.raw_auth_data + client_data_hash)
# signature counter check
return {"verified": True}
示例3: _GetHash
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def _GetHash(self, file_path: Text) -> bytes:
"""Calculates the hash of the boot wim.
Args:
file_path: path to the file to be hashed
Returns:
hash of boot wim in hex
"""
block_size = 33554432 # define bytes to read at a time when hashing (32mb)
hasher = hashlib.sha256()
with open(file_path, 'rb') as f:
fb = f.read(block_size)
while fb:
hasher.update(fb)
fb = f.read(block_size)
return base64.b64encode(hasher.digest())
示例4: get_attachment_by_selector
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def get_attachment_by_selector(self, selector):
"""Gets an attachment using a mongodb query.
Args:
selector -- a query object for mongodb as documented here: https://docs.mongodb.com/manual/reference/method/db.collection.findOne/
Returns:
The attachment object in the following format:
{
tags[]: a list of tag strings attached to this attachment,
sha256: the sha256 hash of this attachment,
content: the raw file,
filename: the filename,
_id: the id of the attachment's postgresql record
}
"""
sarlacc = self.mongo["sarlacc"]
return await sarlacc["samples"].find_one(selector)
示例5: delete_request
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def delete_request(common_name, user="root"):
# Validate CN
if not re.match(const.RE_COMMON_NAME, common_name):
raise ValueError("Invalid common name")
path, buf, csr, submitted = get_request(common_name)
os.unlink(path)
logger.info("Rejected signing request %s by %s" % (
common_name, user))
# Publish event at CA channel
push.publish("request-deleted", common_name)
# Write empty certificate to long-polling URL
requests.delete(
config.LONG_POLL_PUBLISH % hashlib.sha256(buf).hexdigest(),
headers={"User-Agent": "Certidude API"})
示例6: fingerprint
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def fingerprint(self):
"""
Creates a fingerprint that can be compared with a private key to see if
the two form a pair.
This fingerprint is not compatible with fingerprints generated by any
other software.
:return:
A byte string that is a sha256 hash of selected components (based
on the key type)
"""
if self._fingerprint is None:
self._fingerprint = _fingerprint(self.asn1, None)
return self._fingerprint
示例7: parse_command_line
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def parse_command_line():
parser= argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required = True)
group.add_argument('--md5',help='specifies MD5 algorithm',action='store_true')
group.add_argument('--sha256', help='specifies SHA256 algorithm', action='store_true')
group.add_argument('--sha512', help='specifies SHA512 algorithm', action='store_true')
parser.add_argument('-d','--dirpath',type=ValidateDirectory,required=True,help="specify the root path for hashing")
parser.add_argument('-r','--reportpath',type=ValidateDirectoryWritable,required=True,help="specify the path for reports and logs will be written")
global gl_args
global gl_hashType
gl_args = parser.parse_args()
if gl_args.md5:
gl_hashType='MD5'
elif gl_args.sha256:
gl_hashType='SHA256'
elif gl_args.sha512:
gl_hashType='SHA512'
else:
gl_hashType='unknown'
示例8: load_compiled_models
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def load_compiled_models(custom_model, implementation):
"""
Custom hook to load cached implementation of custom models.
"""
compiled = old_hook(custom_model, implementation)
if compiled is not None:
return compiled
model = CUSTOM_MODELS.get(custom_model.full_name)
if model is None:
return
ts_file = model.__implementation__
json_file = ts_file.replace('.ts', '.json')
if not os.path.isfile(json_file):
return
with io.open(ts_file, encoding="utf-8") as f:
code = f.read()
with io.open(json_file, encoding="utf-8") as f:
compiled = json.load(f)
hashed = hashlib.sha256(code.encode('utf-8')).hexdigest()
if compiled['hash'] == hashed:
return AttrDict(compiled)
return None
示例9: build_custom_models
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def build_custom_models():
"""
Compiles custom bokeh models and stores the compiled JSON alongside
the original code.
"""
import earthsim.models.custom_tools
from earthsim.models import _CUSTOM_MODELS
from bokeh.util.compiler import _get_custom_models, _compile_models
custom_models = _get_custom_models(list(_CUSTOM_MODELS.values()))
compiled_models = _compile_models(custom_models)
for name, model in custom_models.items():
compiled = compiled_models.get(name)
if compiled is None:
return
print('\tBuilt %s custom model' % name)
impl = model.implementation
hashed = hashlib.sha256(impl.code.encode('utf-8')).hexdigest()
compiled['hash'] = hashed
fp = impl.file.replace('.ts', '.json')
with open(fp, 'w') as f:
json.dump(compiled, f)
示例10: get_hash_as_int
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def get_hash_as_int(*args, group: cmod.PairingGroup = None):
"""
Enumerate over the input tuple and generate a hash using the tuple values
:param args: sequence of either group or integer elements
:param group: pairing group if an element is a group element
:return:
"""
group = group if group else cmod.PairingGroup(PAIRING_GROUP)
h_challenge = sha256()
serialedArgs = [group.serialize(arg) if isGroupElement(arg)
else cmod.Conversion.IP2OS(arg)
for arg in args]
for arg in sorted(serialedArgs):
h_challenge.update(arg)
return bytes_to_int(h_challenge.digest())
示例11: encrypt_message_data
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def encrypt_message_data(self, data):
"""
Encrypts the given message data using the current authorization key
following MTProto 2.0 guidelines core.telegram.org/mtproto/description.
"""
data = struct.pack('<qq', self.salt, self.id) + data
padding = os.urandom(-(len(data) + 12) % 16 + 12)
# Being substr(what, offset, length); x = 0 for client
# "msg_key_large = SHA256(substr(auth_key, 88+x, 32) + pt + padding)"
msg_key_large = sha256(
self.auth_key.key[88:88 + 32] + data + padding).digest()
# "msg_key = substr (msg_key_large, 8, 16)"
msg_key = msg_key_large[8:24]
aes_key, aes_iv = self._calc_key(self.auth_key.key, msg_key, True)
key_id = struct.pack('<Q', self.auth_key.key_id)
return (key_id + msg_key +
AES.encrypt_ige(data + padding, aes_key, aes_iv))
示例12: ensure_launch_template
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def ensure_launch_template(prefix=__name__.replace(".", "_"), **kwargs):
name = prefix + "_" + hashlib.sha256(json.dumps(kwargs, sort_keys=True).encode()).hexdigest()[:32]
try:
clients.ec2.create_launch_template(LaunchTemplateName=name, LaunchTemplateData=kwargs)
except ClientError as e:
expect_error_codes(e, "InvalidLaunchTemplateName.AlreadyExistsException")
return name
示例13: grep
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def grep(args):
if args.context:
args.before_context = args.after_context = args.context
if not args.end_time:
args.end_time = Timestamp("-0s")
query = clients.logs.start_query(logGroupName=args.log_group,
startTime=int(timestamp(args.start_time) * 1000),
endTime=int(timestamp(args.end_time) * 1000),
queryString=args.query)
seen_results = {}
print_with_context = partial(print_log_event_with_context, before=args.before_context, after=args.after_context)
try:
with ThreadPoolExecutor() as executor:
while True:
res = clients.logs.get_query_results(queryId=query["queryId"])
log_record_pointers = []
for record in res["results"]:
event = {r["field"]: r["value"] for r in record}
event_hash = hashlib.sha256(json.dumps(event, sort_keys=True).encode()).hexdigest()[:32]
if event_hash in seen_results:
continue
if "@ptr" in event and (args.before_context or args.after_context):
log_record_pointers.append(event["@ptr"])
else:
print_log_event(event)
seen_results[event_hash] = event
if log_record_pointers:
executor.map(print_with_context, log_record_pointers)
if res["status"] == "Complete":
break
elif res["status"] in {"Failed", "Cancelled"}:
raise AegeaException("Query status: {}".format(res["status"]))
time.sleep(1)
finally:
try:
clients.logs.stop_query(queryId=query["queryId"])
except clients.logs.exceptions.InvalidParameterException:
pass
logger.debug("Query %s: %s", query["queryId"], res["statistics"])
return SystemExit(os.EX_OK if seen_results else os.EX_DATAERR)
示例14: get_pricing_data
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def get_pricing_data(service_code, filters=None, max_cache_age_days=30):
from ... import config
if filters is None:
filters = [("location", region_name(clients.ec2.meta.region_name))]
get_products_args = dict(ServiceCode=service_code,
Filters=[dict(Type="TERM_MATCH", Field=k, Value=v) for k, v in filters])
cache_key = hashlib.sha256(json.dumps(get_products_args, sort_keys=True).encode()).hexdigest()[:32]
service_code_filename = os.path.join(config.user_config_dir, "pricing_cache_{}.json.gz".format(cache_key))
try:
cache_date = datetime.fromtimestamp(os.path.getmtime(service_code_filename))
if cache_date < datetime.now() - timedelta(days=max_cache_age_days):
raise Exception("Cache is too old, discard")
with gzip.open(service_code_filename) as gz_fh:
with io.BufferedReader(gz_fh) as buf_fh:
pricing_data = json.loads(buf_fh.read().decode())
except Exception:
logger.info("Fetching pricing data for %s", service_code)
client = boto3.client("pricing", region_name="us-east-1")
pricing_data = [json.loads(p) for p in paginate(client.get_paginator("get_products"), **get_products_args)]
try:
with gzip.open(service_code_filename, "w") as fh:
fh.write(json.dumps(pricing_data).encode())
except Exception as e:
print(e, file=sys.stderr)
return pricing_data
示例15: ensure_job_definition
# 需要导入模块: import hashlib [as 别名]
# 或者: from hashlib import sha256 [as 别名]
def ensure_job_definition(args):
if args.ecs_image:
args.image = get_ecr_image_uri(args.ecs_image)
container_props = {k: getattr(args, k) for k in ("image", "vcpus", "privileged")}
container_props.update(memory=4, volumes=[], mountPoints=[], environment=[], command=[], resourceRequirements=[])
set_volumes(args, container_props)
set_ulimits(args, container_props)
if args.gpus:
container_props["resourceRequirements"] = [{"type": "GPU", "value": str(args.gpus)}]
iam_role = ensure_iam_role(args.job_role, trust=["ecs-tasks"], policies=args.default_job_role_iam_policies)
container_props.update(jobRoleArn=iam_role.arn)
expect_job_defn = dict(status="ACTIVE", type="container", parameters={},
retryStrategy={'attempts': args.retry_attempts}, containerProperties=container_props)
job_hash = hashlib.sha256(json.dumps(container_props, sort_keys=True).encode()).hexdigest()[:8]
job_defn_name = __name__.replace(".", "_") + "_jd_" + job_hash
describe_job_definitions_paginator = Paginator(method=clients.batch.describe_job_definitions,
pagination_config=dict(result_key="jobDefinitions",
input_token="nextToken",
output_token="nextToken",
limit_key="maxResults"),
model=None)
for job_defn in paginate(describe_job_definitions_paginator, jobDefinitionName=job_defn_name):
job_defn_desc = {k: job_defn.pop(k) for k in ("jobDefinitionName", "jobDefinitionArn", "revision")}
if job_defn == expect_job_defn:
return job_defn_desc
return clients.batch.register_job_definition(jobDefinitionName=job_defn_name,
type="container",
containerProperties=container_props,
retryStrategy=dict(attempts=args.retry_attempts))