def test_chunksize(self): # hashes can be computed on arbitrarily-sized chunks problems = False for length in range(2, 140): s = "a" * length expected = sha256.SHA256(s).hexdigest() for a in range(0, length): h = sha256.SHA256() h.update(s[:a]) h.update(s[a:]) got = h.hexdigest() if got != expected: problems = True print len(s[:a]), len(s[a:]), len(s), got, expected self.failIf(problems)
def test_monte(self): inlines = resource_string_lines('pycryptopp', 'testvectors/SHA256Monte.txt') for line in inlines: line = line.strip() if line[:7] == 'Seed = ': seed = a2b_hex(line[7:]) break j = 0 for line in inlines: line = line.strip() if line[:8] == 'COUNT = ': assert int(line[8:]) == j elif line[:5] == 'MD = ': mds = [] mds.append(seed) mds.append(seed) mds.append(seed) for i in range(1000): m = mds[-3] + mds[-2] + mds[-1] mds.append(sha256.SHA256(m).digest()) seed = mds[-1] self.failUnlessEqual(line[5:], b2a_hex(seed)) j += 1
def test_digest_then_update_fail(self): h = sha256.SHA256() h.digest() try: h.update("oops") except sha256.Error, le: self.failUnless("digest() has been called" in str(le), le)
def test_recursive_different_chunksizes(self): """ Test that updating a hasher with various sized inputs yields the expected answer. This is somewhat redundant with test_chunksize(), but that's okay. This one exercises some slightly different situations (such as finalizing a hash after different length inputs.) This one is recursive so that there is a single fixed result that we expect. """ hx = sha256.SHA256() s = ''.join([chr(c) for c in range(65)]) for i in range(0, 65): hy = sha256.SHA256(s[:i]).digest() hx.update(hy) for i in range(0, 65): hx.update(chr(0xFE)) hx.update(s[:64]) self.failUnlessEqual( hx.hexdigest().lower(), '5191c7841dd4e16aa454d40af924585dffc67157ffdbfd0236acddd07901629d')
def _test_vect(self, vects_str): for mo in VECTS_RE.finditer(vects_str): msglenbits = int(mo.group(1)) assert msglenbits % 8 == 0 msglen = msglenbits / 8 msg = a2b_hex( mo.group(2) )[: msglen] # The slice is necessary because NIST seems to think that "00" is a reasonable representation for the zero-length string. assert len(msg) == msglen, (len(msg), msglen) md = a2b_hex(mo.group(3)) computed_md = sha256.SHA256(msg).digest() self.failUnlessEqual(computed_md, md)
def _do_parse(self, raw): """ Parse raw message and return it along with relevant information about its outer level. This is done in a separate thread, and the callback is passed to `_do_add_msg` method. :param raw: the raw message :type raw: StringIO or basestring :return: msg, parts, chash, size, multi :rtype: tuple """ msg = message_from_string(raw) parts = walk.get_parts(msg) size = len(raw) chash = sha256.SHA256(raw).hexdigest() multi = msg.is_multipart() return msg, parts, chash, size, multi
def test_digest_twice(self): h = sha256.SHA256() d1 = h.digest() self.failUnless(isinstance(d1, str)) d2 = h.digest() self.failUnlessEqual(d1, d2)
def test_update_type_check(self): h = sha256.SHA256() self.failUnlessRaises(TypeError, h.update, None)
def test_update(self): s = sha256.SHA256("\x5f") s.update("\xd4") d = s.digest() self.failUnlessEqual(d, h_5fd4)
def test_onebyte_2(self): s = sha256.SHA256() s.update("\xbd") d = s.digest() self.failUnlessEqual(d, h_bd)
def test_onebyte_1(self): d = sha256.SHA256("\xbd").digest() self.failUnlessEqual(d, h_bd)
def test_hexdigest(self): empty_hexdigest = sha256.SHA256().hexdigest() self.failUnlessEqual(a2b_hex(empty_hexdigest), h0)
def _parse_msg(raw): msg = message_from_string(raw) parts = walk.get_parts(msg) chash = sha256.SHA256(raw).hexdigest() multi = msg.is_multipart() return msg, parts, chash, multi
def _get_chash(self): return sha256.SHA256(self.raw).hexdigest()
def test_digest(self): empty_digest = sha256.SHA256().digest() self.failUnless(isinstance(empty_digest, str)) self.failUnlessEqual(len(empty_digest), 32) self.failUnlessEqual(empty_digest, h0)
# # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. """ Utilities for walking along a message tree. """ import os from pycryptopp.hash import sha256 from leap.mail.utils import first DEBUG = os.environ.get("BITMASK_MAIL_DEBUG") if DEBUG: get_hash = lambda s: sha256.SHA256(s).hexdigest()[:10] else: get_hash = lambda s: sha256.SHA256(s).hexdigest() """ Get interesting message parts """ get_parts = lambda msg: [ {'multi': part.is_multipart(), 'ctype': part.get_content_type(), 'size': len(part.as_string()), 'parts': len(part.get_payload()) if isinstance(part.get_payload(), list) else 1, 'headers': part.items(),
def proc(self, N): h = sha256.SHA256() h.update(self.msg) h.digest()