How to use hash_sha1 method in localstack

Best Python code snippet using localstack_python

naming.py

Source:naming.py Github

copy

Full Screen

1#!/usr/bin/env python32# -*- coding: utf-8 -*-3import hashlib4import re5import uuid6import requests7DOMAIN_NAME = "dataeng.com"8DAGS_BUCKET_PREFIX = "composer"9DATAFLOW_BUCKET_PREFIX = "dataflow"10TEST_BUCKET_PREFIX = "test"11DAGS_PREFIX = "dags"12def get_ingestion_bucket_name(project_id, org_name, group_name):13 """14 Gets ingestion bucket name.15 :param project_id: GCP project ID16 :param org_name: organisation name17 :param group_name: business group name18 :type project_id: str19 :type org_name: str20 :type group_name: str21 :returns: ingestion bucket name22 :rtype: str23 """24 hash_sha1 = hashlib.sha1()25 org_name = org_name.lower()26 group_name = group_name.lower()27 text = "{project_id}{org_name}{group_name}".format(project_id=project_id, org_name=org_name, group_name=group_name)28 hash_sha1.update(text.encode("utf-8"))29 bucket_name = "{hashed}.{domain_name}".format(hashed=hash_sha1.hexdigest()[:10], domain_name=DOMAIN_NAME)30 return bucket_name31def get_modeling_bucket_name(project_id, org_name, group_name, repo_name):32 """33 Gets modeling bucket name.34 :param project_id: GCP project ID35 :param org_name: organisation name36 :param group_name: business group name37 :param repo_name: DataFlow Git repository name38 :type project_id: str39 :type org_name: str40 :type group_name: str41 :type repo_name: str42 :returns: modeling bucket name43 :rtype: str44 """45 hash_sha1 = hashlib.sha1()46 org_name = org_name.lower()47 group_name = group_name.lower()48 repo_name = repo_name.lower()49 text = "{project_id}{org_name}{group_name}{repo_name}".format(project_id=project_id, org_name=org_name,50 group_name=group_name, repo_name=repo_name)51 hash_sha1.update(text.encode("utf-8"))52 bucket_name = "{hashed}.{domain_name}".format(hashed=hash_sha1.hexdigest()[:10], domain_name=DOMAIN_NAME)53 return bucket_name54def get_dags_bucket_name(project_id):55 """56 Gets a DAGs bucket name.57 :param project_id:58 :type project_id: str59 :returns: DAGs bucket name60 :rtype: str61 """62 hash_sha1 = hashlib.sha1()63 hash_sha1.update(project_id.encode("utf-8"))64 bucket_name = "{bucket_prefix}-{hashed}.{domain_name}".format(bucket_prefix=DAGS_BUCKET_PREFIX,65 hashed=hash_sha1.hexdigest()[:10],66 domain_name=DOMAIN_NAME)67 return bucket_name68def get_dataflow_bucket_name(project_id):69 """70 Gets a DataFlow staging bucket name.71 :param project_id:72 :type project_id: str73 :returns: DataFlow staging bucket name74 :rtype: str75 """76 hash_sha1 = hashlib.sha1()77 hash_sha1.update(project_id.encode("utf-8"))78 bucket_name = "{bucket_prefix}-{hashed}.{domain_name}".format(bucket_prefix=DATAFLOW_BUCKET_PREFIX,79 hashed=hash_sha1.hexdigest()[:10],80 domain_name=DOMAIN_NAME)81 return bucket_name82def get_test_bucket_name(project_id):83 """84 Gets a test bucket name.85 :param project_id:86 :type project_id: str87 :returns: test bucket name88 :rtype: str89 """90 hash_sha1 = hashlib.sha1()91 hash_sha1.update(project_id.encode("utf-8"))92 bucket_name = "{bucket_prefix}-{hashed}.{domain_name}".format(bucket_prefix=TEST_BUCKET_PREFIX,93 hashed=hash_sha1.hexdigest()[:10],94 domain_name=DOMAIN_NAME)95 return bucket_name96def get_dags_location(dags_bucket_name):97 """98 Gets DAGs location.99 :param dags_bucket_name: DAGs bucket name100 :type dags_bucket_name: str101 :returns: DAGs location on GCS102 :rtype: str103 """104 return "gs://{dags_bucket_name}/{dags_prefix}".format(dags_bucket_name=dags_bucket_name, dags_prefix=DAGS_PREFIX)105def get_prefix(data_source, data_source_type, format, location="_", year="_", month="_", day="_", hour="_", minute="_",106 second="_"):107 """108 Gets prefix for a data source or a model. Note that, there is no validation of appropriate timestamp portions.109 110 :param data_source: data source name or model name111 :param data_source_type : data source type112 :param format: format113 :param location: location114 :param year: year115 :param month: month116 :param day: day117 :param hour: hour118 :param minute: minute119 :param second: second120 :type data_source: str121 :type data_source_type : str122 :type format: str123 :type location: str124 :type year: str125 :type month: str126 :type day: str127 :type hour: str128 :type minute: str129 :type second: str130 :returns: prefix131 :rtype: str132 """133 prefix = "{data_source}/{data_source_type}/{location}/{year}/{month}/{day}/{hour}/{minute}/{second}/{format}/".format(134 data_source=data_source, data_source_type=data_source_type, location=location, year=_zfill_with_len(year, 4),135 month=_zfill_with_len(month, 2), day=_zfill_with_len(day, 2), hour=_zfill_with_len(hour, 2),136 minute=_zfill_with_len(minute, 2), second=_zfill_with_len(second, 2), format=format)137 return prefix138def get_project_id():139 """140 Gets GCP project ID.141 :returns: GCP project ID142 :rtype: str143 """144 metadata_server = "http://metadata/computeMetadata/v1/project"145 metadata_flavor = {"Metadata-Flavor": "Google"}146 url = "{metadata_server}/project-id".format(metadata_server=metadata_server)147 project_id = requests.get(url, headers=metadata_flavor).text148 return project_id149def get_staging_dataset_id(data_source, data_source_type):150 """151 Gets staging BigQuery dataset ID.152 :param data_source: data source153 :param data_source_type: data source type154 :type data_source: str155 :type data_source_type: str156 :returns: staging dataset ID157 :rtype: str158 """159 pattern = re.compile(r"[\W]")160 data_source = pattern.sub("_", data_source)161 data_source = data_source.strip("_")162 data_source_type = pattern.sub("_", data_source_type)163 data_source_type = data_source_type.strip("_")164 random_string = str(uuid.uuid4()).replace("-", "_")165 return "staging_{data_source}_{data_source_type}_{random_string}".format(data_source=data_source,166 data_source_type=data_source_type,167 random_string=random_string)168def _zfill_with_len(val, len):169 """170 Returns the appropriate place holder for a given length171 :param val: value172 :param len: expected length of value173 :type val: str174 :type len: int175 :returns: formatted value176 :rtype: str177 """178 if val == "_" or val is None:179 return val180 else:181 return val.zfill(len)182def get_success_file_location(data_source, data_source_type):183 """184 Gets success file location.185 :param data_source: data source name or model name186 :param data_source_type: data source type187 :type data_source: str188 :type data_source_type: str189 :returns: success file location190 :rtype: str191 """192 success_file_location = "{data_source}/{data_source_type}/_SUCCESS".format(data_source=data_source,193 data_source_type=data_source_type)...

Full Screen

Full Screen

testencoding.py

Source:testencoding.py Github

copy

Full Screen

1#!/usr/bin/python2__copyright__ = """3Copyright (C) Timo Engel (timo-e@freenet.de), Berlin 2012.4This program was written as part of a master thesis advised by 5Prof. Dr. Ruediger Weis at the Beuth University of Applied 6Sciences Berlin.7This program is free software: you can redistribute it and/or modify8it under the terms of the GNU General Public License as published by9the Free Software Foundation, either version 3 of the License, or10(at your option) any later version.11This program is distributed in the hope that it will be useful,12but WITHOUT ANY WARRANTY; without even the implied warranty of13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14GNU General Public License for more details.15You should have received a copy of the GNU General Public License16along with this program. If not, see <http://www.gnu.org/licenses/>.17"""18import sys19sys.path.append('..')20import unittest21from encoding import *22import crypto23# ------------------------------------------------------------------------------24class TestEncoding(unittest.TestCase):25 def testEcash(self):26 self.assertTrue(len(ecash('foobar', 1024, crypto.HASH_SHA1)) * 8 <= 1024)27 self.assertTrue(ecash('foobar', 1024, crypto.HASH_SHA1).startswith('foobar'))28# ------------------------------------------------------------------------------29 def testMgf(self):30 self.assertEqual(len(pssMGF('abc', 1, crypto.HASH_SHA1)), 1)31 self.assertEqual(len(pssMGF('abc', 2, crypto.HASH_SHA1)), 2)32 self.assertEqual(len(pssMGF('abc', 3, crypto.HASH_SHA1)), 3)33 self.assertEqual(len(pssMGF('abc', 4, crypto.HASH_SHA1)), 4)34 self.assertEqual(len(pssMGF('abc', 19, crypto.HASH_SHA1)), 19)35 self.assertEqual(len(pssMGF('abc', 20, crypto.HASH_SHA1)), 20)36 self.assertEqual(len(pssMGF('abc', 21, crypto.HASH_SHA1)), 21)37 self.assertEqual(len(pssMGF('abc', 22, crypto.HASH_SHA1)), 22)38 self.assertEqual(pssMGF('abc', 4, crypto.HASH_SHA1).encode('hex'),39 'a03eb8ac')40 self.assertEqual(41 pssMGF('abcdefghijklmnopqrst', 43, crypto.HASH_SHA1).encode('hex'),42 '23cf4b6d0149c1edfe4444807deb454e1e15369437679463961c86426180c0736dabd'43 'cbb38464e1cca90df')44 45# ------------------------------------------------------------------------------46 def testEmsapss(self):47 self.assertEqual(len(pssEncode('abcdef', 20, 512, crypto.HASH_SHA1)), 64)48 self.assertEqual(len(pssEncode('abcdef', 20, 513, crypto.HASH_SHA1)), 65)49 self.assertEqual(len(pssEncode('abcdef', 20, 511, crypto.HASH_SHA1)), 64)50 M = 'Foobar'51 EM = pssEncode(M, 20, 512, crypto.HASH_SHA1)52 self.assertTrue(pssVerify(M, EM, 20, 512, crypto.HASH_SHA1))53 self.assertTrue(pssVerify(M,54 pssEncode(M, 20, 512, crypto.HASH_SHA1),55 20, 512, crypto.HASH_SHA1))56 self.assertTrue(pssVerify(M,57 pssEncode(M, 20, 513, crypto.HASH_SHA1),58 20, 513, crypto.HASH_SHA1))59 self.assertTrue(pssVerify(M,60 pssEncode(M, 20, 511, crypto.HASH_SHA1),61 20, 511, crypto.HASH_SHA1))62 self.assertFalse(pssVerify('FooBar', EM, 20, 512, crypto.HASH_SHA1))63 64 self.assertTrue(pssVerify(M,65 pssEncode(M, 20, 512, crypto.HASH_MD5),66 20, 512, crypto.HASH_MD5))67 self.assertTrue(pssVerify(M,68 pssEncode(M, 20, 512, crypto.HASH_SHA256),69 20, 512, crypto.HASH_SHA256))70 self.assertTrue(pssVerify(M,71 pssEncode(M, 20, 512, crypto.HASH_SHA224),72 20, 512, crypto.HASH_SHA224))73 self.assertTrue(74 pssVerify(M,75 hashEncode(M, 512, crypto.HASH_MD5, ENCODING_PKCSPSS), 76 16, 512, crypto.HASH_MD5))77 self.assertTrue(78 pssVerify(M,79 hashEncode(M, 512, crypto.HASH_SHA1, ENCODING_PKCSPSS),80 20, 512, crypto.HASH_SHA1))81 self.assertTrue(82 pssVerify(M,83 hashEncode(M, 512, crypto.HASH_SHA224, ENCODING_PKCSPSS),84 28, 512, crypto.HASH_SHA224))85 86 self.assertTrue(87 pssVerify(M,88 hashEncode(M, 1024, crypto.HASH_SHA256, ENCODING_PKCSPSS),89 32, 1024, crypto.HASH_SHA256))90 91# ------------------------------------------------------------------------------92 def testHashEncode(self):93 hashEncode('Foobar', 512, crypto.HASH_MD5, ENCODING_PKCS15)94 hashEncode('Foobar', 512, crypto.HASH_MD5, ENCODING_ECASH)95 hashEncode('Foobar', 512, crypto.HASH_MD5, ENCODING_PKCSPSS)96 hashEncode('Foobar', 512, crypto.HASH_SHA1, ENCODING_PKCS15)97 hashEncode('Foobar', 512, crypto.HASH_SHA1, ENCODING_ECASH)98 hashEncode('Foobar', 512, crypto.HASH_SHA1, ENCODING_PKCSPSS)99 hashEncode('Foobar', 512, crypto.HASH_SHA256, ENCODING_PKCS15)100 hashEncode('Foobar', 512, crypto.HASH_SHA256, ENCODING_ECASH)101 hashEncode('Foobar', 1024, crypto.HASH_SHA256, ENCODING_PKCSPSS)102 hashEncode('Foobar', 1024, crypto.HASH_SHA384, ENCODING_PKCS15)103 hashEncode('Foobar', 1024, crypto.HASH_SHA384, ENCODING_ECASH)104 hashEncode('Foobar', 1024, crypto.HASH_SHA384, ENCODING_PKCSPSS)105 hashEncode('Foobar', 1024, crypto.HASH_SHA512, ENCODING_PKCS15)106 hashEncode('Foobar', 1024, crypto.HASH_SHA512, ENCODING_ECASH)107 hashEncode('Foobar', 2048, crypto.HASH_SHA512, ENCODING_PKCSPSS)108 hashEncode('Foobar', 1024, crypto.HASH_SHA224, ENCODING_PKCS15)109 hashEncode('Foobar', 1024, crypto.HASH_SHA224, ENCODING_ECASH)110 hashEncode('Foobar', 1024, crypto.HASH_SHA224, ENCODING_PKCSPSS)111 112# ------------------------------------------------------------------------------113 114if __name__ == '__main__':...

Full Screen

Full Screen

hash_helper.py

Source:hash_helper.py Github

copy

Full Screen

1import hashlib2import base643####4# Helpers5####6def chunked_file_import(fname,chunk_trunk):7 with open(fname, "rb") as f:8 for chunk in iter(lambda: f.read(4096), b""):9 chunk_trunk.update(chunk)10 return chunk_trunk11####12# SHA113####14def sha1_hex(fname):15 hash_sha1 = hashlib.sha1()16 hash_sha1 = chunked_file_import(fname,hash_sha1)17 return hash_sha1.hexdigest()18def sha1_base32(fname):19 hash_sha1 = hashlib.sha1()20 hash_sha1 = chunked_file_import(fname,hash_sha1)21 digest = hash_sha1.digest()22 return base64.b32encode(digest)23def sha1_base64(fname):24 hash_sha1 = hashlib.sha1()25 hash_sha1 = chunked_file_import(fname,hash_sha1)26 digest = hash_sha1.digest()27 return base64.b64encode(digest)28####29# MD530####31def md5_hex(fname):32 hash_md5 = hashlib.md5()33 hash_md5 = chunked_file_import(fname,hash_md5)34 return hash_md5.hexdigest()35def md5_base32(fname):36 hash_md5 = hashlib.md5()37 hash_md5 = chunked_file_import(fname,hash_md5)38 digest = hash_md5.digest()39 return base64.b32encode(digest)40def md5_base64(fname):41 hash_md5 = hashlib.md5()42 hash_md5 = chunked_file_import(fname,hash_md5)43 digest = hash_md5.digest()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful