def test_could_be(self):
        # base-32 encoded strings could be
        for j in range(2**9):
            rands = insecurerandstr(random.randrange(1, 2**7))
            randsenc = b2a(rands)
            assert could_be_base32_encoded(randsenc), "rands: %s, randsenc: %s, a2b(randsenc): %s" % (`rands`, `randsenc`, `a2b(randsenc)`,)

        # base-32 encoded strings with unusual bit lengths could be, too
        for j in range(2**9):
            bitl = random.randrange(1, 2**7)
            bs = insecurerandstr((bitl+7)/8)
            # zero-out unused least-sig bits
            if bitl%8:
                b=ord(bs[-1])
                b = b >> (8 - (bitl%8))
                b = b << (8 - (bitl%8))
                bs = bs[:-1] + chr(b)
            assert could_be_base32_encoded_l(b2a_l(bs, bitl), bitl)

        # anything with a bogus character couldn't be
        s = b2a(insecurerandstr(random.randrange(3, 2**7)))
        assert not could_be_base32_encoded('\x00' + s)
        assert not could_be_base32_encoded(s + '\x00')
        assert not could_be_base32_encoded(s[:1] + '\x00' + s[1:])
        assert not could_be_base32_encoded(s[:1] + '\x00' + s[2:])

        # a base-32 encoded string with an alleged lengthinbits of 16 but with 1-bits in the 18th, 19th,
        # and 20th location couldn't be.
        assert not could_be_base32_encoded_l('yyz', 16)
示例#2
0
    def test_could_be(self):
        # base-32 encoded strings could be
        for j in range(2**9):
            rands = insecurerandstr(random.randrange(1, 2**7))
            randsenc = b2a(rands)
            assert could_be_base32_encoded(
                randsenc), "rands: %s, randsenc: %s, a2b(randsenc): %s" % (
                    ` rands `,
                    ` randsenc `,
                    ` a2b(randsenc) `,
                )

        # base-32 encoded strings with unusual bit lengths could be, too
        for j in range(2**9):
            bitl = random.randrange(1, 2**7)
            bs = insecurerandstr((bitl + 7) / 8)
            # zero-out unused least-sig bits
            if bitl % 8:
                b = ord(bs[-1])
                b = b >> (8 - (bitl % 8))
                b = b << (8 - (bitl % 8))
                bs = bs[:-1] + chr(b)
            assert could_be_base32_encoded_l(b2a_l(bs, bitl), bitl)

        # anything with a bogus character couldn't be
        s = b2a(insecurerandstr(random.randrange(3, 2**7)))
        assert not could_be_base32_encoded('\x00' + s)
        assert not could_be_base32_encoded(s + '\x00')
        assert not could_be_base32_encoded(s[:1] + '\x00' + s[1:])
        assert not could_be_base32_encoded(s[:1] + '\x00' + s[2:])

        # a base-32 encoded string with an alleged lengthinbits of 16 but with 1-bits in the 18th, 19th,
        # and 20th location couldn't be.
        assert not could_be_base32_encoded_l('yyz', 16)
 def test_big(self):
     bs = insecurerandstr(2**9)
     asciis=b2a(bs)
     asciisl=b2a_long(bs)
     assert asciis == asciisl, "asciis: %s, asciisl: %s" % (`asciis`, `asciisl`,)
     asciis=b2a(bs)
     bs2=a2b(asciis)
     bs2l=a2b_long(asciis)
     assert bs2 == bs2l
     assert bs2 == bs
示例#4
0
 def test_big(self):
     bs = insecurerandstr(2**9)
     cs=b2a(bs)
     asl=b2a_long(bs)
     assert cs == asl, "cs: %s, asl: %s" % (`cs`, `asl`,)
     cs=b2a(bs)
     bs2=a2b(cs)
     bs2l=a2b_long(cs)
     assert bs2 == bs2l
     assert bs2 == bs
示例#5
0
 def test_big(self):
     bs = insecurerandstr(2**9)
     cs = b2a(bs)
     asl = b2a_long(bs)
     assert cs == asl, "cs: %s, asl: %s" % (
         ` cs `,
         ` asl `,
     )
     cs = b2a(bs)
     bs2 = a2b(cs)
     bs2l = a2b_long(cs)
     assert bs2 == bs2l
     assert bs2 == bs
示例#6
0
 def test_ende(self):
     bs = insecurerandstr(2**3)
     cs = b2a(bs)
     bs2 = a2b(cs)
     assert bs2 == bs, "bs2: %s, bs: %s" % (
         ` bs2 `,
         ` bs `,
     )
 def test_ende(self):
     bs = insecurerandstr(2**3)
     asciis=b2a(bs)
     bs2=a2b(asciis)
     assert bs2 == bs, "bs2: %s, bs: %s" % (`bs2`, `bs`,)
def _help_bench_ed(N):
    return a2b(b2a(insecurerandstr(N)))
def _help_bench_e(N):
    return b2a(insecurerandstr(N))
示例#10
0
def _help_bench_ed(N):
    return a2b(b2a(insecurerandstr(N)))
示例#11
0
def _help_bench_e(N):
    return b2a(insecurerandstr(N))
示例#12
0
文件: codec.py 项目: nejucomo/c2c
def encode_a(bits):
    return 'a' + zbase32.b2a(bits)