def _corrupt_crypttext_hash_tree_byte_x221(data, debug=False): """Scramble the file data -- the byte at offset 0x221 will have its 7th (b1) bit flipped. """ sharevernum = struct.unpack(">L", data[0x0c:0x0c + 4])[0] assert sharevernum in ( 1, 2 ), "This test is designed to corrupt immutable shares of v1 or v2 in specific ways." if debug: log.msg("original data: %r" % (data, )) return data[:0x0c + 0x221] + byteschr( ord(data[0x0c + 0x221:0x0c + 0x221 + 1]) ^ 0x02) + data[0x0c + 0x2210 + 1:]
def test_odd_sizes(self): for j in range(2**6): lib = random.randrange(1, 2**8) numos = mathutil.div_ceil(lib, 8) bs = insecurerandstr(numos) # zero-out unused least-sig bits if lib % 8: b = ord(bs[-1:]) b = b >> (8 - (lib % 8)) b = b << (8 - (lib % 8)) bs = bs[:-1] + byteschr(b) asl = base62.b2a_l(bs, lib) assert len( asl) == base62.num_chars_that_this_many_octets_encode_to( numos ) # the size of the base-62 encoding must be just right bs2l = base62.a2b_l(asl, lib) assert len( bs2l) == numos # the size of the result must be just right assert bs == bs2l
def _corrupt_share_data_last_byte(data, debug=False): """Scramble the file data -- flip all bits of the last byte.""" sharevernum = struct.unpack(">L", data[0x0c:0x0c + 4])[0] assert sharevernum in ( 1, 2 ), "This test is designed to corrupt immutable shares of v1 or v2 in specific ways, not v%d." % sharevernum if sharevernum == 1: sharedatasize = struct.unpack(">L", data[0x0c + 0x08:0x0c + 0x08 + 4])[0] offset = 0x0c + 0x24 + sharedatasize - 1 else: sharedatasize = struct.unpack(">Q", data[0x0c + 0x08:0x0c + 0x0c + 8])[0] offset = 0x0c + 0x44 + sharedatasize - 1 newdata = data[:offset] + byteschr(ord(data[offset:offset + 1]) ^ 0xFF) + data[offset + 1:] if debug: log.msg( "testing: flipping all bits of byte at offset %d: %r, newdata: %r" % (offset, data[offset], newdata[offset])) return newdata
import string maketrans = string.maketrans translate = string.translate else: maketrans = bytes.maketrans translate = bytes.translate from past.builtins import chr as byteschr from allmydata.util.mathutil import log_ceil, log_floor chars = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" BASE62CHAR = b'[' + chars + b']' vals = b''.join([byteschr(i) for i in range(62)]) c2vtranstable = maketrans(chars, vals) v2ctranstable = maketrans(vals, chars) identitytranstable = maketrans(chars, chars) def b2a(os): """ @param os the data to be encoded (as bytes) @return the contents of os in base-62 encoded form, as bytes """ cs = b2a_l(os, len(os) * 8) assert num_octets_that_encode_to_this_many_chars( len(cs)) == len(os), "%s != %s, numchars: %s" % ( num_octets_that_encode_to_this_many_chars(
def _xor(a, b): return b"".join([byteschr(c ^ b) for c in a])
def flip_bit(good): # flips the last bit return good[:-1] + byteschr(ord(good[-1]) ^ 0x01)
def _xor(a, b): return b"".join([byteschr(c ^ b) for c in future_bytes(a)])