Python hashlib.sha256() Examples

The following are 30 code examples of hashlib.sha256(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module hashlib , or try the search function .
Example #1
Source Project: wechatpy   Author: wechatpy   File: utils.py    License: MIT License 7 votes vote down vote up
def calculate_signature_hmac(params, api_key):
    url = format_url(params, api_key)
    sign = to_text(hmac.new(api_key.encode(), msg=url, digestmod=hashlib.sha256).hexdigest().upper())
    return sign 
Example #2
Source Project: Telethon   Author: LonamiWebs   File: mtprotostate.py    License: MIT License 7 votes vote down vote up
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)) 
Example #3
Source Project: cmrc2019   Author: ymcui   File: file_utils.py    License: Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
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 
Example #4
Source Project: pywarp   Author: pyauth   File: rp.py    License: Apache License 2.0 6 votes vote down vote up
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} 
Example #5
Source Project: glazier   Author: google   File: beyondcorp.py    License: Apache License 2.0 6 votes vote down vote up
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()) 
Example #6
Source Project: sarlacc   Author: scrapbird   File: storage.py    License: MIT License 6 votes vote down vote up
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) 
Example #7
Source Project: certidude   Author: laurivosandi   File: authority.py    License: MIT License 6 votes vote down vote up
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"}) 
Example #8
Source Project: oscrypto   Author: wbond   File: _asymmetric.py    License: MIT License 6 votes vote down vote up
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 
Example #9
Source Project: hacking-tools   Author: girishramnani   File: _pfish_tools.py    License: MIT License 6 votes vote down vote up
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' 
Example #10
Source Project: mrc-for-flat-nested-ner   Author: pranciskus   File: file_utils.py    License: Apache License 2.0 6 votes vote down vote up
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 
Example #11
Source Project: EarthSim   Author: pyviz-topics   File: __init__.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #12
Source Project: EarthSim   Author: pyviz-topics   File: setup.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #13
Source Project: indy-anoncreds   Author: hyperledger-archives   File: utils.py    License: Apache License 2.0 6 votes vote down vote up
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()) 
Example #14
Source Project: ConvLab   Author: ConvLab   File: allennlp_file_utils.py    License: MIT License 6 votes vote down vote up
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 
Example #15
Source Project: aegea   Author: kislyuk   File: batch.py    License: Apache License 2.0 5 votes vote down vote up
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 
Example #16
Source Project: aegea   Author: kislyuk   File: logs.py    License: Apache License 2.0 5 votes vote down vote up
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) 
Example #17
Source Project: aegea   Author: kislyuk   File: __init__.py    License: Apache License 2.0 5 votes vote down vote up
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 
Example #18
Source Project: aegea   Author: kislyuk   File: batch.py    License: Apache License 2.0 5 votes vote down vote up
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)) 
Example #19
Source Project: BASS   Author: Cisco-Talos   File: util.py    License: GNU General Public License v2.0 5 votes vote down vote up
def file_sha256(path):
    """Get the hex digest of the given file"""
    sha256 = hashlib.sha256()
    with open(path, "rb") as f:
        map(sha256.update, iter(partial(f.read, BLOCK_SIZE), ""))
    return sha256.hexdigest() 
Example #20
Source Project: BASS   Author: Cisco-Talos   File: server.py    License: GNU General Public License v2.0 5 votes vote down vote up
def _function_calculate_raw_sha256(func):
    """Calculate the hash over the raw function bytes"""
    hashsum = hashlib.sha256()
    for chunk in func.chunks:
        hashsum.update(chunk.bytes)
    return hashsum.hexdigest() 
Example #21
Source Project: BASS   Author: Cisco-Talos   File: server.py    License: GNU General Public License v2.0 5 votes vote down vote up
def _function_calculate_mnem_sha256(func):
    """Calculate the hash over the function's opcode mnemonics"""
    hashsum = hashlib.sha256()
    for bb in func.basic_blocks:
        for head in bb.code_heads:
            hashsum.update(head.mnemonic)
    return hashsum.hexdigest() 
Example #22
Source Project: BASS   Author: Cisco-Talos   File: util.py    License: GNU General Public License v2.0 5 votes vote down vote up
def file_sha256(path):
    """Get the hex digest of the given file"""
    sha256 = hashlib.sha256()
    with open(path, "rb") as f:
        map(sha256.update, iter(partial(f.read, BLOCK_SIZE), ""))
    return sha256.hexdigest() 
Example #23
Source Project: pywarp   Author: pyauth   File: rp.py    License: Apache License 2.0 5 votes vote down vote up
def register(self, client_data_json, attestation_object, email):
        "Store the credential public key and related metadata on the server using the associated storage backend"
        authenticator_attestation_response = cbor2.loads(attestation_object)
        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.create"
        print("client data", client_data)
        expect_challenge = self.storage_backend.get_challenge_for_user(email=email, type="registration")
        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_attestation_response["authData"])
        assert authenticator_data.user_present
        # If user verification is required for this registration,
        # verify that the User Verified bit of the flags in authData is set.
        assert authenticator_attestation_response["fmt"] == "fido-u2f"
        att_stmt = FIDOU2FAttestationStatement(authenticator_attestation_response['attStmt'])
        attestation = att_stmt.validate(authenticator_data,
                                        rp_id_hash=authenticator_data.rp_id_hash,
                                        client_data_hash=client_data_hash)
        credential = attestation.credential
        # TODO: ascertain user identity here
        self.storage_backend.save_credential_for_user(email=email, credential=credential)
        return {"registered": True}

    # https://www.w3.org/TR/webauthn/#verifying-assertion 
Example #24
Source Project: spleeter   Author: deezer   File: github.py    License: MIT License 5 votes vote down vote up
def compute_file_checksum(path):
    """ Computes given path file sha256.

    :param path: Path of the file to compute checksum for.
    :returns: File checksum.
    """
    sha256 = hashlib.sha256()
    with open(path, 'rb') as stream:
        for chunk in iter(lambda: stream.read(4096), b''):
            sha256.update(chunk)
    return sha256.hexdigest() 
Example #25
Source Project: python-rest-api   Author: messagebird   File: signed_request.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def verify(self, signing_key):
        payload = self._build_payload()
        expected_signature = base64.b64decode(self._requestSignature)
        calculated_signature = hmac.new(signing_key.encode('latin-1'), payload.encode('latin-1'),
                                        hashlib.sha256).digest()
        return expected_signature == calculated_signature 
Example #26
Source Project: python-rest-api   Author: messagebird   File: signed_request.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _build_payload(self):
        checksum_body = hashlib.sha256(self._requestBody.encode('latin-1')).digest()
        str_checksum_body = checksum_body.decode('latin-1')
        parts = [self._requestTimestamp, urlencode(self._sort_dict(self._requestParameters), True), str_checksum_body]
        return "\n".join(parts) 
Example #27
Source Project: smbprotocol   Author: jborean93   File: connection.py    License: MIT License 5 votes vote down vote up
def _generate_signature(self, b_header, signing_key):
        b_header = b_header[:48] + (b"\x00" * 16) + b_header[64:]

        if self.dialect >= Dialects.SMB_3_0_0:
            c = cmac.CMAC(algorithms.AES(signing_key), backend=default_backend())
            c.update(b_header)
            signature = c.finalize()
        else:
            hmac_algo = hmac.new(signing_key, msg=b_header, digestmod=hashlib.sha256)
            signature = hmac_algo.digest()[:16]

        return signature 
Example #28
Source Project: TradzQAI   Author: kkuette   File: cbpro_auth.py    License: Apache License 2.0 5 votes vote down vote up
def get_auth_headers(timestamp, message, api_key, secret_key, passphrase):
    message = message.encode('ascii')
    hmac_key = base64.b64decode(secret_key)
    signature = hmac.new(hmac_key, message, hashlib.sha256)
    signature_b64 = base64.b64encode(signature.digest()).decode('utf-8')
    return {
        'Content-Type': 'Application/JSON',
        'CB-ACCESS-SIGN': signature_b64,
        'CB-ACCESS-TIMESTAMP': timestamp,
        'CB-ACCESS-KEY': api_key,
        'CB-ACCESS-PASSPHRASE': passphrase
    } 
Example #29
Source Project: eth-account   Author: ethereum   File: _utils.py    License: MIT License 5 votes vote down vote up
def sha256(data: bytes) -> bytes:
    return hashlib.sha256(data).digest() 
Example #30
Source Project: unicorn-binance-websocket-api   Author: oliver-zehentleitner   File: unicorn_binance_websocket_api_restclient.py    License: MIT License 5 votes vote down vote up
def _get_signature(self, data):
        """
        Get the signature of 'data'

        :param data: the data you want to sign
        :type data: str

        :return: signature
        :rtype: str
        """
        hmac_signature = hmac.new(self.api_secret.encode('utf-8'), data.encode('utf-8'), hashlib.sha256)
        return hmac_signature.hexdigest()