Example #1
0
  def __next__(self):
    # type: () -> PrivateKey
    while self.current >= 0:
      sponge = self._create_sponge(self.current)

      key     = [0] * (self.fragment_length * self.security_level)
      buffer  = [0] * len(self.seed_as_trits)

      for fragment_seq in range(self.security_level):
        # Squeeze trits from the buffer and append them to the key, one
        # hash at a time.
        for hash_seq in range(self.hashes_per_fragment):
          sponge.squeeze(buffer)

          key_start =\
            (fragment_seq * self.fragment_length) + (hash_seq * HASH_LENGTH)

          key_stop = key_start + HASH_LENGTH

          # Ensure we only capture one hash from the buffer, in case
          # it is longer than that (i.e., if the seed is longer than 81
          # trytes).
          key[key_start:key_stop] = buffer[0:HASH_LENGTH]

      private_key =\
        PrivateKey.from_trits(
          key_index       = self.current,
          security_level  = self.security_level,
          trits           = key,
        ) # type: PrivateKey

      self.advance()

      return private_key
Example #2
0
    def __next__(self) -> PrivateKey:
        while self.current >= 0:
            sponge = self._create_sponge(self.current)

            key = [0] * (self.fragment_length * self.security_level)
            buffer = [0] * len(self.seed_as_trits)

            for fragment_seq in range(self.security_level):
                # Squeeze trits from the buffer and append them to the
                # key, one hash at a time.
                for hash_seq in range(self.hashes_per_fragment):
                    sponge.squeeze(buffer)

                    key_start = (
                            (fragment_seq * self.fragment_length) +
                            (hash_seq * HASH_LENGTH)
                    )

                    key_stop = key_start + HASH_LENGTH

                    # Ensure we only capture one hash from the buffer,
                    # in case it is longer than that (i.e., if the seed
                    # is longer than 81 trytes).
                    key[key_start:key_stop] = buffer[0:HASH_LENGTH]

            private_key = PrivateKey.from_trits(
                    key_index=self.current,
                    security_level=self.security_level,
                    trits=key,
            )

            self.advance()

            return private_key
Example #3
0
  def __next__(self):
    # type: () -> PrivateKey
    while self.current >= 0:
      sponge = self._create_sponge(self.current)

      key     = [0] * (self.fragment_length * self.iterations)
      buffer  = [0] * HASH_LENGTH # type: MutableSequence[int]

      for fragment_seq in range(self.iterations):
        # Squeeze trits from the buffer and append them to the key, one
        # hash at a time.
        for hash_seq in range(self.hashes_per_fragment):
          sponge.squeeze(buffer)

          key_start =\
            (fragment_seq * self.fragment_length) + (hash_seq * HASH_LENGTH)

          key_stop = key_start + HASH_LENGTH

          key[key_start:key_stop] = buffer

      private_key = PrivateKey.from_trits(key)
      private_key.key_index = self.current

      self.current += self.step

      return private_key
Example #4
0
  def create_generator(self, start=0, step=1, iterations=1):
    # type: (int, int) -> Generator[PrivateKey]
    """
    Creates a generator that can be used to progressively generate new
    keys.

    :param start:
      Starting index.

      Warning: This method may take awhile to reset if ``start``
      is a large number!

    :param step:
      Number of indexes to advance after each key.

      This value can be negative; the generator will exit if it
      reaches an index < 0.

      Warning: The generator may take awhile to advance between
      iterations if ``step`` is a large number!

    :param iterations:
      Number of _transform iterations to apply to each key.
      Must be >= 1.

      Increasing this value makes key generation slower, but more
      resistant to brute-forcing.
    """
    if start < 0:
      raise with_context(
        exc = ValueError('``start`` cannot be negative.'),

        context = {
          'start':      start,
          'step':       step,
          'iterations': iterations,
        },
      )

    if iterations < 1:
      raise with_context(
        exc = ValueError('``iterations`` must be >= 1.'),

        context = {
          'start':      start,
          'step':       step,
          'iterations': iterations,
        },
      )

    current = start

    fragment_length     = FRAGMENT_LENGTH * TRITS_PER_TRYTE
    hashes_per_fragment = FRAGMENT_LENGTH // Hash.LEN

    while current >= 0:
      sponge = self._create_sponge(current)

      key     = [0] * (fragment_length * iterations)
      buffer  = [0] * HASH_LENGTH # type: MutableSequence[int]

      for fragment_seq in range(iterations):
        # Squeeze trits from the buffer and append them to the key, one
        # hash at a time.
        for hash_seq in range(hashes_per_fragment):
          sponge.squeeze(buffer)

          key_start =\
            (fragment_seq * fragment_length) + (hash_seq * HASH_LENGTH)

          key_stop = key_start + HASH_LENGTH

          key[key_start:key_stop] = buffer

      private_key = PrivateKey.from_trits(key)
      private_key.key_index = current
      yield private_key

      current += step