Ejemplo n.º 1
0
 def test_zero_bytes(self):
   self.assertEqual(integer.uint_to_bytes(integer.bytes_to_uint(ZERO_BYTES)),
                    ONE_ZERO_BYTE)
   self.assertEqual(integer.uint_to_bytes(_alt_integer.bytes_to_uint_naive(ZERO_BYTES)),
                    ONE_ZERO_BYTE)
   self.assertEqual(integer.uint_to_bytes(_alt_integer.bytes_to_uint_simple(ZERO_BYTES)),
                    ONE_ZERO_BYTE)
Ejemplo n.º 2
0
 def test_zero_bytes(self):
   self.assertEqual(uint_to_bytes(bytes_to_uint(zero_bytes)),
                    one_zero_byte)
   self.assertEqual(uint_to_bytes(bytes_to_uint_naive(zero_bytes)),
                    one_zero_byte)
   self.assertEqual(uint_to_bytes(bytes_to_uint_simple(zero_bytes)),
                    one_zero_byte)
Ejemplo n.º 3
0
 def test_overflow_allowed(self):
   self.assertEqual(uint_to_bytes(0xc0ff, fill_size=1, overflow=True),
                    b('\xc0\xff'))
   self.assertEqual(uint_to_bytes(123456789, fill_size=3, overflow=True),
                    b('\x07\x5b\xcd\x15'))
   self.assertEqual(uint_to_bytes(0xf00dc0ffee, fill_size=4,
                                  overflow=True),
                    b('\xf0\x0d\xc0\xff\xee'))
Ejemplo n.º 4
0
 def test_fill_size(self):
   self.assertEqual(integer.uint_to_bytes(0xc0ff, fill_size=4),
                    b("\x00\x00\xc0\xff"))
   self.assertEqual(integer.uint_to_bytes(0xc0ffee, fill_size=6),
                    b("\x00\x00\x00\xc0\xff\xee"))
   self.assertEqual(integer.uint_to_bytes(123456789, fill_size=6),
                    b("\x00\x00\x07[\xcd\x15"))
   self.assertEqual(integer.uint_to_bytes(123456789, fill_size=7),
                    b("\x00\x00\x00\x07[\xcd\x15"))
Ejemplo n.º 5
0
 def test_fill_size(self):
   self.assertEqual(uint_to_bytes(0xc0ff, fill_size=4),
                    b('\x00\x00\xc0\xff'))
   self.assertEqual(uint_to_bytes(0xc0ffee, fill_size=6),
                    b('\x00\x00\x00\xc0\xff\xee'))
   self.assertEqual(uint_to_bytes(123456789, fill_size=6),
                    b('\x00\x00\x07[\xcd\x15'))
   self.assertEqual(uint_to_bytes(123456789, fill_size=7),
                    b('\x00\x00\x00\x07[\xcd\x15'))
Ejemplo n.º 6
0
 def test_codec_equivalence(self):
   # Padding bytes are not preserved (it is acceptable here).
   random_bytes = b("\x00\xbcE\x9a\xda]")
   expected_bytes = b("\xbcE\x9a\xda]")
   self.assertEqual(uint_to_bytes(bytes_to_uint(random_bytes)),
                    expected_bytes)
   self.assertEqual(uint_to_bytes(bytes_to_uint_naive(random_bytes)),
                    expected_bytes)
   self.assertEqual(uint_to_bytes(bytes_to_uint_simple(random_bytes)),
                    expected_bytes)
Ejemplo n.º 7
0
  def test_correctness(self):
    self.assertEqual(integer.uint_to_bytes(0xeeeeffff),
                     b("\xee\xee\xff\xff"))
    self.assertEqual(integer.uint_to_bytes(0xeeeeff),
                     b("\xee\xee\xff"))

    self.assertEqual(_alt_integer.uint_to_bytes_naive(0xeeeeffff),
                     b("\xee\xee\xff\xff"))
    self.assertEqual(_alt_integer.uint_to_bytes_naive(0xeeeeff),
                     b("\xee\xee\xff"))

    self.assertEqual(_alt_integer.uint_to_bytes_simple(0xeeeeffff),
                     b("\xee\xee\xff\xff"))
    self.assertEqual(_alt_integer.uint_to_bytes_simple(0xeeeeff),
                     b("\xee\xee\xff"))
Ejemplo n.º 8
0
  def test_correctness(self):
    self.assertEqual(uint_to_bytes(0xeeeeffff),
                     b('\xee\xee\xff\xff'))
    self.assertEqual(uint_to_bytes(0xeeeeff),
                     b('\xee\xee\xff'))

    self.assertEqual(uint_to_bytes_naive(0xeeeeffff),
                     b('\xee\xee\xff\xff'))
    self.assertEqual(uint_to_bytes_naive(0xeeeeff),
                     b('\xee\xee\xff'))

    self.assertEqual(uint_to_bytes_simple(0xeeeeffff),
                     b('\xee\xee\xff\xff'))
    self.assertEqual(uint_to_bytes_simple(0xeeeeff),
                     b('\xee\xee\xff'))
Ejemplo n.º 9
0
  def test_zero(self):
    self.assertEqual(uint_to_bytes(0, 4), b('\x00') * 4)
    self.assertEqual(uint_to_bytes_array_based(0, 4), b('\x00') * 4)
    self.assertEqual(uint_to_bytes_naive(0, 4), b('\x00') * 4)
    self.assertEqual(uint_to_bytes_naive_array_based(0, 4), b('\x00') * 4)
    self.assertEqual(uint_to_bytes_pycrypto(0, 4), b('\x00') * 4)

    self.assertEqual(uint_to_bytes(0, 7), b('\x00') * 7)
    self.assertEqual(uint_to_bytes_naive(0, 7), b('\x00') * 7)
    self.assertEqual(uint_to_bytes_array_based(0, 7), b('\x00') * 7)
    self.assertEqual(uint_to_bytes_naive_array_based(0, 7), b('\x00') * 7)
    self.assertEqual(uint_to_bytes_pycrypto(0, 7), b('\x00') * 7)

    self.assertEqual(uint_to_bytes(0), b('\x00'))
    self.assertEqual(uint_to_bytes_naive(0), b('\x00'))
    self.assertEqual(uint_to_bytes_array_based(0), b('\x00'))
    self.assertEqual(uint_to_bytes_naive_array_based(0), b('\x00'))
    self.assertEqual(uint_to_bytes_pycrypto(0), b('\x00'))
Ejemplo n.º 10
0
  def test_zero(self):
    self.assertEqual(integer.uint_to_bytes(0, 4), b("\x00") * 4)
    self.assertEqual(_alt_integer.uint_to_bytes_array_based(0, 4), b("\x00") * 4)
    self.assertEqual(_alt_integer.uint_to_bytes_naive(0, 4), b("\x00") * 4)
    self.assertEqual(_alt_integer.uint_to_bytes_naive_array_based(0, 4), b("\x00") * 4)
    self.assertEqual(_alt_integer.uint_to_bytes_pycrypto(0, 4), b("\x00") * 4)

    self.assertEqual(integer.uint_to_bytes(0, 7), b("\x00") * 7)
    self.assertEqual(_alt_integer.uint_to_bytes_naive(0, 7), b("\x00") * 7)
    self.assertEqual(_alt_integer.uint_to_bytes_array_based(0, 7), b("\x00") * 7)
    self.assertEqual(_alt_integer.uint_to_bytes_naive_array_based(0, 7), b("\x00") * 7)
    self.assertEqual(_alt_integer.uint_to_bytes_pycrypto(0, 7), b("\x00") * 7)

    self.assertEqual(integer.uint_to_bytes(0), b("\x00"))
    self.assertEqual(_alt_integer.uint_to_bytes_naive(0), b("\x00"))
    self.assertEqual(_alt_integer.uint_to_bytes_array_based(0), b("\x00"))
    self.assertEqual(_alt_integer.uint_to_bytes_naive_array_based(0), b("\x00"))
    self.assertEqual(_alt_integer.uint_to_bytes_pycrypto(0), b("\x00"))
Ejemplo n.º 11
0
def uint_to_base256(number, encoded, base_zero):
  """Convert uint to base 256."""
  if number == 0:
    raw_bytes = EMPTY_BYTE
  else:
    raw_bytes = uint_to_bytes(number)
  zero_leading = bytes_leading(encoded, base_zero)
  if zero_leading:
    raw_bytes = raw_bytes.rjust(len(raw_bytes) + zero_leading, ZERO_BYTE)
  return raw_bytes
Ejemplo n.º 12
0
Archivo: keys.py Proyecto: rajeshvv/mom
    def sign(self, digest):
        """
    Signs a digest with the key.

    :param digest:
        The SHA-1 digest of the data.
    :returns:
        Signature byte string.
    """
        return integer.uint_to_bytes(self._sign(digest))
Ejemplo n.º 13
0
def uint_to_base256(number, encoded, base_zero):
  """Convert uint to base 256."""
  if number == 0:
    raw_bytes = EMPTY_BYTE
  else:
    raw_bytes = integer.uint_to_bytes(number)
  zero_leading = builtins.bytes_leading(encoded, base_zero)
  if zero_leading:
    raw_bytes = raw_bytes.rjust(len(raw_bytes) + zero_leading, ZERO_BYTE)
  return raw_bytes
Ejemplo n.º 14
0
Archivo: keys.py Proyecto: rajeshvv/mom
    def sign(self, digest):
        """
    Signs a digest with the key.

    :param digest:
        The SHA-1 digest of the data.
    :returns:
        Signature byte string.
    """
        return integer.uint_to_bytes(self._sign(digest))
Ejemplo n.º 15
0
 def test_correctness_for_primes(self):
   for prime in prime_sieve.SIEVE:
     self.assertEqual(integer.uint_to_bytes(prime), _alt_integer.uint_to_bytes_naive(prime),
                      "Boom %d" % prime)
     self.assertEqual(_alt_integer.uint_to_bytes_array_based(prime),
                      _alt_integer.uint_to_bytes_naive(prime),
                      "Boom %d" % prime)
     self.assertEqual(_alt_integer.uint_to_bytes_pycrypto(prime),
                      _alt_integer.uint_to_bytes_naive(prime),
                      "Boom %d" % prime)
Ejemplo n.º 16
0
 def test_correctness_for_primes(self):
   for prime in SIEVE:
     self.assertEqual(uint_to_bytes(prime), uint_to_bytes_naive(prime),
                      "Boom %d" % prime)
     self.assertEqual(uint_to_bytes_array_based(prime),
                      uint_to_bytes_naive(prime),
                      "Boom %d" % prime)
     self.assertEqual(uint_to_bytes_pycrypto(prime),
                      uint_to_bytes_naive(prime),
                      "Boom %d" % prime)
Ejemplo n.º 17
0
  def test_accuracy(self):
    self.assertEqual(integer.uint_to_bytes(123456789), b("\x07[\xcd\x15"))
    self.assertEqual(_alt_integer.uint_to_bytes_pycrypto(123456789),
                     b("\x07[\xcd\x15"))
    self.assertEqual(_alt_integer.uint_to_bytes_array_based(123456789),
                     b("\x07[\xcd\x15"))
    self.assertEqual(_alt_integer.uint_to_bytes_naive(123456789), b("\x07[\xcd\x15"))
    self.assertEqual(_alt_integer.uint_to_bytes_naive_array_based(123456789),
                     b("\x07[\xcd\x15"))

    self.assertEqual(integer.uint_to_bytes(LONG_VALUE),
                     EXPECTED_BYTES)
    self.assertEqual(_alt_integer.uint_to_bytes_pycrypto(LONG_VALUE),
                     EXPECTED_BYTES)
    self.assertEqual(_alt_integer.uint_to_bytes_array_based(LONG_VALUE),
                     EXPECTED_BYTES)
    self.assertEqual(_alt_integer.uint_to_bytes_naive(LONG_VALUE),
                     EXPECTED_BYTES)
    self.assertEqual(_alt_integer.uint_to_bytes_naive_array_based(LONG_VALUE),
                     EXPECTED_BYTES)
Ejemplo n.º 18
0
 def test_correctness_against_base_implementation(self):
   # Slow test.
   values = [
     1 << 512,
     1 << 8192,
     1 << 77,
     ]
   for value in values:
     self.assertEqual(uint_to_bytes(value),
                      uint_to_bytes_naive(value),
                      "Boom %d" % value)
Ejemplo n.º 19
0
 def test_correctness_against_base_implementation(self):
   # Slow test.
   values = [
     1 << 512,
     1 << 8192,
     1 << 77,
     ]
   for value in values:
     self.assertEqual(integer.uint_to_bytes(value),
                      _alt_integer.uint_to_bytes_naive(value),
                      "Boom %d" % value)
Ejemplo n.º 20
0
  def test_accuracy(self):
    self.assertEqual(uint_to_bytes(123456789), b('\x07[\xcd\x15'))
    self.assertEqual(uint_to_bytes_pycrypto(123456789),
                     b('\x07[\xcd\x15'))
    self.assertEqual(uint_to_bytes_array_based(123456789),
                     b('\x07[\xcd\x15'))
    self.assertEqual(uint_to_bytes_naive(123456789), b('\x07[\xcd\x15'))
    self.assertEqual(uint_to_bytes_naive_array_based(123456789),
                     b('\x07[\xcd\x15'))

    self.assertEqual(uint_to_bytes(long_value),
                     expected_bytes)
    self.assertEqual(uint_to_bytes_pycrypto(long_value),
                     expected_bytes)
    self.assertEqual(uint_to_bytes_array_based(long_value),
                     expected_bytes)
    self.assertEqual(uint_to_bytes_naive(long_value),
                     expected_bytes)
    self.assertEqual(uint_to_bytes_naive_array_based(long_value),
                     expected_bytes)
Ejemplo n.º 21
0
  def test_chunk_size(self):
    self.assertEqual(uint_to_bytes(long_value,
                                   long_value_blocksize),
                     expected_blocksize_bytes)
    self.assertEqual(uint_to_bytes_pycrypto(long_value,
                                            long_value_blocksize),
                     expected_blocksize_bytes)
    self.assertEqual(uint_to_bytes_array_based(long_value,
                                               long_value_blocksize),
                     expected_blocksize_bytes)
    self.assertEqual(uint_to_bytes_naive(long_value,
                                         long_value_blocksize),
                     expected_blocksize_bytes)
    self.assertEqual(uint_to_bytes_naive_array_based(long_value,
                                                     long_value_blocksize),
                     expected_blocksize_bytes)

    self.assertEqual(uint_to_bytes(123456789, 6),
                     b('\x00\x00\x07[\xcd\x15'))
    self.assertEqual(uint_to_bytes(123456789, 7),
                     b('\x00\x00\x00\x07[\xcd\x15'))

    self.assertEqual(uint_to_bytes_pycrypto(123456789, 6),
                     b('\x00\x00\x07[\xcd\x15'))
    self.assertEqual(uint_to_bytes_pycrypto(123456789, 7),
                     b('\x00\x00\x00\x07[\xcd\x15'))

    self.assertEqual(uint_to_bytes_array_based(123456789, 6),
                     b('\x00\x00\x07[\xcd\x15'))
    self.assertEqual(uint_to_bytes_array_based(123456789, 7),
                     b('\x00\x00\x00\x07[\xcd\x15'))

    self.assertEqual(uint_to_bytes_naive(123456789, 6),
                     b('\x00\x00\x07[\xcd\x15'))
    self.assertEqual(uint_to_bytes_naive(123456789, 7),
                     b('\x00\x00\x00\x07[\xcd\x15'))

    self.assertEqual(uint_to_bytes_naive_array_based(123456789, 6),
                     b('\x00\x00\x07[\xcd\x15'))
    self.assertEqual(uint_to_bytes_naive_array_based(123456789, 7),
                     b('\x00\x00\x00\x07[\xcd\x15'))
Ejemplo n.º 22
0
  def test_chunk_size(self):
    self.assertEqual(integer.uint_to_bytes(LONG_VALUE,
                                   LONG_VALUE_BLOCKSIZE),
                     EXPECTED_BLOCKSIZE_BYTES)
    self.assertEqual(_alt_integer.uint_to_bytes_pycrypto(LONG_VALUE,
                                            LONG_VALUE_BLOCKSIZE),
                     EXPECTED_BLOCKSIZE_BYTES)
    self.assertEqual(_alt_integer.uint_to_bytes_array_based(LONG_VALUE,
                                               LONG_VALUE_BLOCKSIZE),
                     EXPECTED_BLOCKSIZE_BYTES)
    self.assertEqual(_alt_integer.uint_to_bytes_naive(LONG_VALUE,
                                         LONG_VALUE_BLOCKSIZE),
                     EXPECTED_BLOCKSIZE_BYTES)
    self.assertEqual(_alt_integer.uint_to_bytes_naive_array_based(LONG_VALUE,
                                                     LONG_VALUE_BLOCKSIZE),
                     EXPECTED_BLOCKSIZE_BYTES)

    self.assertEqual(integer.uint_to_bytes(123456789, 6),
                     b("\x00\x00\x07[\xcd\x15"))
    self.assertEqual(integer.uint_to_bytes(123456789, 7),
                     b("\x00\x00\x00\x07[\xcd\x15"))

    self.assertEqual(_alt_integer.uint_to_bytes_pycrypto(123456789, 6),
                     b("\x00\x00\x07[\xcd\x15"))
    self.assertEqual(_alt_integer.uint_to_bytes_pycrypto(123456789, 7),
                     b("\x00\x00\x00\x07[\xcd\x15"))

    self.assertEqual(_alt_integer.uint_to_bytes_array_based(123456789, 6),
                     b("\x00\x00\x07[\xcd\x15"))
    self.assertEqual(_alt_integer.uint_to_bytes_array_based(123456789, 7),
                     b("\x00\x00\x00\x07[\xcd\x15"))

    self.assertEqual(_alt_integer.uint_to_bytes_naive(123456789, 6),
                     b("\x00\x00\x07[\xcd\x15"))
    self.assertEqual(_alt_integer.uint_to_bytes_naive(123456789, 7),
                     b("\x00\x00\x00\x07[\xcd\x15"))

    self.assertEqual(_alt_integer.uint_to_bytes_naive_array_based(123456789, 6),
                     b("\x00\x00\x07[\xcd\x15"))
    self.assertEqual(_alt_integer.uint_to_bytes_naive_array_based(123456789, 7),
                     b("\x00\x00\x00\x07[\xcd\x15"))
Ejemplo n.º 23
0
 def test_correctness_against_base_implementation(self):
   # Slow test.
   values = [
     1 << 512,
     1 << 8192,
     1 << 77,
     ]
   for value in values:
     self.assertEqual(integer.uint_to_bytes(value), _alt_integer.uint_to_bytes_naive(value),
                      "Boom %d" % value)
     self.assertEqual(_alt_integer.uint_to_bytes_array_based(value),
                      _alt_integer.uint_to_bytes_naive(value),
                      "Boom %d" % value)
     self.assertEqual(integer.uint_to_bytes(value),
                      _alt_integer.uint_to_bytes_naive_array_based(value),
                      "Boom %d" % value)
     self.assertEqual(_alt_integer.uint_to_bytes_pycrypto(value),
                      _alt_integer.uint_to_bytes_naive(value),
                      "Boom %d" % value)
     self.assertEqual(integer.bytes_to_uint(integer.uint_to_bytes(value)),
                      value,
                      "Boom %d" % value)
Ejemplo n.º 24
0
def b58decode_naive(encoded,
                    _charset=base58.ASCII58_BYTES,
                    _lookup=base58.ASCII58_ORDS):
  """
  Simple implementation for benchmarking.

  Base-58 decodes a sequence of bytes into raw bytes. Whitespace is ignored.

  :param encoded:
      Base-58 encoded bytes.
  :param _charset:
      (Internal) The character set to use. Defaults to ``base58.ASCII58_BYTES``
      that uses natural ASCII order.
  :param _lookup:
      (Internal) Ordinal-to-character lookup table for the specified
      character set.
  :returns:
      Raw bytes.
  """
  if not builtins.is_bytes(encoded):
    raise TypeError("encoded data must be bytes: got %r" %
                    type(encoded).__name__)

  # Ignore whitespace.
  encoded = re.sub(WHITESPACE_PATTERN, EMPTY_BYTE, encoded)

  # Convert to big integer.
  number = 0
  for i, char in enumerate(reversed(encoded)):
    number += _lookup[char] * (58 ** i)

  # Obtain raw bytes.
  if number:
    raw_bytes = integer.uint_to_bytes(number)
  else:
    # We don't want to convert to b"\x00" when we get number == 0.
    # That would add an off-by-one extra zero byte in the result.
    raw_bytes = EMPTY_BYTE

  # Add prefixed padding if required.
  # 0 byte is represented using the first character in the character set.
  zero_char = _charset[0]
  # The extra [0] index in zero_byte_char[0] is for Python2.x-Python3.x
  # compatibility. Indexing into Python 3 bytes yields an integer, whereas
  # in Python 2.x it yields a single-byte string.
  zero_leading = functional.leading(lambda w: w == zero_char[0], encoded)
  if zero_leading:
    padding = ZERO_BYTE * zero_leading
    raw_bytes = padding + raw_bytes
  return raw_bytes
Ejemplo n.º 25
0
def b58decode_naive(encoded,
                    _charset=base58.ASCII58_BYTES,
                    _lookup=base58.ASCII58_ORDS):
    """
  Simple implementation for benchmarking.

  Base-58 decodes a sequence of bytes into raw bytes. Whitespace is ignored.

  :param encoded:
      Base-58 encoded bytes.
  :param _charset:
      (Internal) The character set to use. Defaults to ``base58.ASCII58_BYTES``
      that uses natural ASCII order.
  :param _lookup:
      (Internal) Ordinal-to-character lookup table for the specified
      character set.
  :returns:
      Raw bytes.
  """
    if not builtins.is_bytes(encoded):
        raise TypeError("encoded data must be bytes: got %r" %
                        type(encoded).__name__)

    # Ignore whitespace.
    encoded = re.sub(WHITESPACE_PATTERN, EMPTY_BYTE, encoded)

    # Convert to big integer.
    number = 0
    for i, char in enumerate(reversed(encoded)):
        number += _lookup[char] * (58**i)

    # Obtain raw bytes.
    if number:
        raw_bytes = integer.uint_to_bytes(number)
    else:
        # We don't want to convert to b"\x00" when we get number == 0.
        # That would add an off-by-one extra zero byte in the result.
        raw_bytes = EMPTY_BYTE

    # Add prefixed padding if required.
    # 0 byte is represented using the first character in the character set.
    zero_char = _charset[0]
    # The extra [0] index in zero_byte_char[0] is for Python2.x-Python3.x
    # compatibility. Indexing into Python 3 bytes yields an integer, whereas
    # in Python 2.x it yields a single-byte string.
    zero_leading = functional.leading(lambda w: w == zero_char[0], encoded)
    if zero_leading:
        padding = ZERO_BYTE * zero_leading
        raw_bytes = padding + raw_bytes
    return raw_bytes
Ejemplo n.º 26
0
 def test_wikipedia_decoding(self):
   decoding_table = [
     (b("1"), 1),
     (b("10"), 36),
     (b("100"), 1296),
     (b("1000"), 46656),
     (b("10000"), 1679616),
     (b("100000"), 60466176),
     (b("1000000"), 2176782336),
     (b("10000000"), 78364164096),
     (b("100000000"), 2821109907456),
   ]
   for encoded, number in decoding_table:
     raw_bytes = integer.uint_to_bytes(number)
     self.assertEqual(base36.b36encode(raw_bytes), encoded)
     self.assertEqual(base36.b36decode(encoded), raw_bytes)
Ejemplo n.º 27
0
 def test_wikipedia_encoding(self):
   encoding_table = [
     (1, b("1")),
     (10, b("A")),
     (100, b("2S")),
     (1000, b("RS")),
     (10000, b("7PS")),
     (100000, b("255S")),
     (1000000, b("LFLS")),
     (1000000000, b("GJDGXS")),
     (1000000000000, b("CRE66I9S")),
   ]
   for number, encoded in encoding_table:
     raw_bytes = integer.uint_to_bytes(number)
     self.assertEqual(base36.b36encode(raw_bytes), encoded)
     self.assertEqual(base36.b36decode(encoded), raw_bytes)
Ejemplo n.º 28
0
 def test_wikipedia_decoding(self):
     decoding_table = [
         (b("1"), 1),
         (b("10"), 36),
         (b("100"), 1296),
         (b("1000"), 46656),
         (b("10000"), 1679616),
         (b("100000"), 60466176),
         (b("1000000"), 2176782336),
         (b("10000000"), 78364164096),
         (b("100000000"), 2821109907456),
     ]
     for encoded, number in decoding_table:
         raw_bytes = integer.uint_to_bytes(number)
         self.assertEqual(base36.b36encode(raw_bytes), encoded)
         self.assertEqual(base36.b36decode(encoded), raw_bytes)
Ejemplo n.º 29
0
 def test_wikipedia_encoding(self):
     encoding_table = [
         (1, b("1")),
         (10, b("A")),
         (100, b("2S")),
         (1000, b("RS")),
         (10000, b("7PS")),
         (100000, b("255S")),
         (1000000, b("LFLS")),
         (1000000000, b("GJDGXS")),
         (1000000000000, b("CRE66I9S")),
     ]
     for number, encoded in encoding_table:
         raw_bytes = integer.uint_to_bytes(number)
         self.assertEqual(base36.b36encode(raw_bytes), encoded)
         self.assertEqual(base36.b36decode(encoded), raw_bytes)
Ejemplo n.º 30
0
def decimal_decode(encoded):
    """
  Decodes decimal-encoded bytes to raw bytes. Leading zeros are converted to
  leading zero bytes.

  :param encoded:
      Decimal-encoded representation.
  :returns:
      Raw bytes.
  """
    padding = ZERO_BYTE * builtins.bytes_leading(encoded, DIGIT_ZERO_BYTE)
    int_val = int(encoded)
    if int_val:
        decoded = padding + integer.uint_to_bytes(int_val)
    else:
        decoded = padding
    return decoded
Ejemplo n.º 31
0
def decimal_decode(encoded):
  """
  Decodes decimal-encoded bytes to raw bytes. Leading zeros are converted to
  leading zero bytes.

  :param encoded:
      Decimal-encoded representation.
  :returns:
      Raw bytes.
  """
  padding = ZERO_BYTE * bytes_leading(encoded, DIGIT_ZERO_BYTE)
  int_val = int(encoded)
  if int_val:
    decoded = padding + uint_to_bytes(int_val)
  else:
    decoded = padding
  return decoded
Ejemplo n.º 32
0
Archivo: keys.py Proyecto: rajeshvv/mom
def pkcs1_v1_5_encode(key_size, data):
    """
  Encodes a key using PKCS1's emsa-pkcs1-v1_5 encoding.

  :author:
      Rick Copeland <*****@*****.**>

  :param key_size:
      RSA key size.
  :param data:
      Data
  :returns:
      A blob of data as large as the key's N, using PKCS1's
      "emsa-pkcs1-v1_5" encoding.
  """
    size = len(integer.uint_to_bytes(key_size))
    filler = FF_BYTE * (size - SHA1_DIGESTINFO_LEN - len(data) - 3)
    return ZERO_ONE_BYTES + filler + ZERO_BYTE + SHA1_DIGESTINFO + data
Ejemplo n.º 33
0
Archivo: keys.py Proyecto: rajeshvv/mom
def pkcs1_v1_5_encode(key_size, data):
    """
  Encodes a key using PKCS1's emsa-pkcs1-v1_5 encoding.

  :author:
      Rick Copeland <*****@*****.**>

  :param key_size:
      RSA key size.
  :param data:
      Data
  :returns:
      A blob of data as large as the key's N, using PKCS1's
      "emsa-pkcs1-v1_5" encoding.
  """
    size = len(integer.uint_to_bytes(key_size))
    filler = FF_BYTE * (size - SHA1_DIGESTINFO_LEN - len(data) - 3)
    return ZERO_ONE_BYTES + filler + ZERO_BYTE + SHA1_DIGESTINFO + data
Ejemplo n.º 34
0
  def test_zero(self):
    self.assertEqual(integer.uint_to_bytes(0), b("\x00"))
    self.assertEqual(integer.uint_to_bytes(0, 4), b("\x00") * 4)
    self.assertEqual(integer.uint_to_bytes(0, 7), b("\x00") * 7)
    self.assertEqual(integer.uint_to_bytes(0, chunk_size=1), b("\x00"))
    self.assertEqual(integer.uint_to_bytes(0, chunk_size=4), b("\x00") * 4)
    self.assertEqual(integer.uint_to_bytes(0, chunk_size=7), b("\x00") * 7)

    self.assertEqual(_alt_integer.uint_to_bytes_naive(0), b("\x00"))
    self.assertEqual(_alt_integer.uint_to_bytes_naive(0, 4), b("\x00") * 4)
    self.assertEqual(_alt_integer.uint_to_bytes_naive(0, 7), b("\x00") * 7)

    self.assertEqual(_alt_integer.uint_to_bytes_simple(0), b("\x00"))
Ejemplo n.º 35
0
  def test_zero(self):
    self.assertEqual(uint_to_bytes(0), b('\x00'))
    self.assertEqual(uint_to_bytes(0, 4), b('\x00') * 4)
    self.assertEqual(uint_to_bytes(0, 7), b('\x00') * 7)
    self.assertEqual(uint_to_bytes(0, chunk_size=1), b('\x00'))
    self.assertEqual(uint_to_bytes(0, chunk_size=4), b('\x00') * 4)
    self.assertEqual(uint_to_bytes(0, chunk_size=7), b('\x00') * 7)

    self.assertEqual(uint_to_bytes_naive(0), b('\x00'))
    self.assertEqual(uint_to_bytes_naive(0, 4), b('\x00') * 4)
    self.assertEqual(uint_to_bytes_naive(0, 7), b('\x00') * 7)

    self.assertEqual(uint_to_bytes_simple(0), b('\x00'))
Ejemplo n.º 36
0
  def test_encoding_and_decoding(self):
    hello_world = b('\x48\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64')
    encoded_hello_world = b58encode(hello_world)

    self.assertEqual(encoded_hello_world, b58encode_naive(hello_world))
    self.assertEqual(b58decode(encoded_hello_world), hello_world)

    self.assertEqual(bytes_to_uint(b58decode(b("16Ho7Hs"))), 3471844090)
    self.assertEqual(b58encode(uint_to_bytes(3471844090, 5)), b("16Ho7Hs"))

    self.assertEqual(b58encode(raw_data), encoded)
    self.assertEqual(b58decode(encoded), raw_data)
    self.assertEqual(b58decode(encoded_with_whitespace), raw_data)
    self.assertEqual(b58decode_naive(encoded), raw_data)
    self.assertEqual(b58decode_naive(encoded_with_whitespace), raw_data)

    self.assertEqual(base58_encode(raw_data), encoded)
    self.assertEqual(base58_decode(encoded), raw_data)
    self.assertEqual(base58_decode(encoded_with_whitespace), raw_data)
Ejemplo n.º 37
0
  def test_encoding_and_decoding(self):
    hello_world = b("\x48\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64")
    encoded_hello_world = base58.b58encode(hello_world)

    self.assertEqual(encoded_hello_world, _alt_base.b58encode_naive(hello_world))
    self.assertEqual(base58.b58decode(encoded_hello_world), hello_world)

    self.assertEqual(integer.bytes_to_uint(base58.b58decode(b("16Ho7Hs"))), 3471844090)
    self.assertEqual(base58.b58encode(integer.uint_to_bytes(3471844090, 5)), b("16Ho7Hs"))

    self.assertEqual(base58.b58encode(RAW_DATA), ENCODED)
    self.assertEqual(base58.b58decode(ENCODED), RAW_DATA)
    self.assertEqual(base58.b58decode(ENCODED_WITH_WHITESPACE), RAW_DATA)
    self.assertEqual(_alt_base.b58decode_naive(ENCODED), RAW_DATA)
    self.assertEqual(_alt_base.b58decode_naive(ENCODED_WITH_WHITESPACE), RAW_DATA)

    self.assertEqual(codec.base58_encode(RAW_DATA), ENCODED)
    self.assertEqual(codec.base58_decode(ENCODED), RAW_DATA)
    self.assertEqual(codec.base58_decode(ENCODED_WITH_WHITESPACE), RAW_DATA)
Ejemplo n.º 38
0
 def test_chunk_size(self):
   self.assertEqual(uint_to_bytes(0xffffeeeeaa, chunk_size=4),
                    b('\x00\x00\x00\xff\xff\xee\xee\xaa'))
Ejemplo n.º 39
0
 def test_long_value(self):
   self.assertEqual(integer.uint_to_bytes(LONG_VALUE),
                    EXPECTED_BYTES)
   self.assertEqual(integer.uint_to_bytes(LONG_VALUE, fill_size=45),
                    EXPECTED_FILL_BYTES)
Ejemplo n.º 40
0
 def test_chunk_size(self):
   self.assertEqual(integer.uint_to_bytes(0xffffeeeeaa, chunk_size=4),
                    b("\x00\x00\x00\xff\xff\xee\xee\xaa"))
Ejemplo n.º 41
0
 def test_correctness_for_primes(self):
   for prime in prime_sieve.SIEVE:
     self.assertEqual(integer.uint_to_bytes(prime),
                      _alt_integer.uint_to_bytes_naive(prime),
                      "Boom %d" % prime)