Exemple #1
0
def evenCompare(a: str, b: str) -> bool:
    """
    A deterministic but more evenly distributed comparator than simple alphabetical.
    Useful when comparing consecutive strings and an even distribution is needed.
    Provides an even chance of returning true as often as false
    """
    ab = a.encode('utf-8')
    bb = b.encode('utf-8')
    ac = crypto_hash_sha256(ab)
    bc = crypto_hash_sha256(bb)
    return ac < bc
Exemple #2
0
def evenCompare(a: str, b: str) -> bool:
    """
    A deterministic but more evenly distributed comparator than simple alphabetical.
    Useful when comparing consecutive strings and an even distribution is needed.
    Provides an even chance of returning true as often as false
    """
    ab = a.encode('utf-8')
    bb = b.encode('utf-8')
    ac = crypto_hash_sha256(ab)
    bc = crypto_hash_sha256(bb)
    return ac < bc
Exemple #3
0
 def new(cls, prev, seq, counterparty, m, vk, sk, nonce=None):
     # type: (str, int, str, str, str, str, str) -> pb.TxBlock
     if nonce is None:
         nonce = libnacl.randombytes(32)
     inner = pb.TxBlock.Inner(prev=prev, seq=seq, counterparty=counterparty, nonce=nonce, m=m)
     s = Signature.new(vk, sk, libnacl.crypto_hash_sha256(inner.SerializeToString()))
     return cls(pb.TxBlock(inner=inner, s=s.pb))
Exemple #4
0
    def new(cls, prev, seq, cons, p, vk, sk, ss, vks, t):
        # type: (str, int, Cons, int, str, str, List[Signature], List[str], int) -> pb.CpBlock
        """

        :param prev: hash pointer to the previous block
        :param cons: type Cons
        :param seq: height
        :param p: promoter flag
        :param vk: my verification key
        :param sk: my secret key
        :param ss: signatures of the promoters, at least t-1 of them must be valid
        :param vks: all verification keys of promoters
        :param t:
        """
        assert p in (0, 1)
        inner = pb.CpBlock.Inner(prev=prev, seq=seq, round=cons.round, cons_hash=cons.hash, ss=[s.pb for s in ss], p=p)

        if cons.round != 0 or len(ss) != 0 or len(vks) != 0 or inner.seq != 0:
            _verify_signatures(inner.cons_hash, ss, vks, t)
        else:
            # if this is executed, it means this is a genesis block
            pass
        s = Signature.new(vk, sk, libnacl.crypto_hash_sha256(inner.SerializeToString()))

        return cls(pb.CpBlock(inner=inner, s=s.pb))
Exemple #5
0
 def add_other_half(self, other_half):
     # type: (TxBlock) -> ()
     assert self.inner.nonce == other_half.inner.nonce
     assert self.inner.m == other_half.inner.m
     other_half.s.verify(self.inner.counterparty,
                         libnacl.crypto_hash_sha256(other_half.inner.SerializeToString()))
     self.other_half = other_half
Exemple #6
0
 def hash(self):
     # type: () -> str
     """
     Override the normal hash, we only need the inner part
     :return: 
     """
     if self._hash is None:
         self._hash = libnacl.crypto_hash_sha256(self.pb.inner.SerializeToString())
     return self._hash
Exemple #7
0
    def _upon_n_minus_t_echo(self):
        v = self._decode_fragments()
        assert libnacl.crypto_hash_sha256(v) == self._root
        self._v = v
        logging.debug("Bracha: erasure decoded msg v {}".format(b64encode(v)))

        if not self._sent_ready:
            logging.debug("Bracha: broadcast ready 1, root = {}".format(b64encode(self._root)))
            self.bcast(pb.Bracha(ty=_READY, digest=self._root))
            self._sent_ready = True
Exemple #8
0
    def test_hash(self):
        msg1 = b'Are you suggesting coconuts migrate?'
        msg2 = b'Not at all, they could be carried.'
        chash1 = libnacl.crypto_hash(msg1)
        chash2 = libnacl.crypto_hash(msg2)
        self.assertNotEqual(msg1, chash1)
        self.assertNotEqual(msg2, chash2)
        self.assertNotEqual(chash2, chash1)

        ref256 = sha256(msg1)
        self.assertEqual(ref256.digest_size, libnacl.crypto_hash_sha256_BYTES)
        self.assertEqual(ref256.digest(), libnacl.crypto_hash_sha256(msg1))

        ref512 = sha512(msg1)
        self.assertEqual(ref512.digest_size, libnacl.crypto_hash_sha512_BYTES)
        self.assertEqual(ref512.digest(), libnacl.crypto_hash_sha512(msg1))
Exemple #9
0
    def _bcast_init_fragments(self, msg):
        """
        
        :param msg: some bytes
        :return: 
        """
        fragments = self._ec_driver.encode(msg)
        digest = libnacl.crypto_hash_sha256(msg)

        logging.info("Bracha: initiate erasure code with {} fragments, digest {}"
                     .format(len(fragments), b64encode(digest)))

        assert len(fragments) == len(self._factory.promoters)
        for fragment, promoter in zip(fragments, self._factory.promoters):
            m = pb.Bracha(ty=_INIT, digest=digest, fragment=fragment)
            self._factory.send(promoter, self._msg_wrapper_f(m))
Exemple #10
0
 def key_get_id(key):
     if isinstance(key, Signer): key = key_get_vk(key)
     return '{{:>{}s}}'.format(key_id_len).format(
         b64_encode(libnacl.crypto_hash_sha256(key.vk))[:key_id_len])
	def key_get_id(key):
		if isinstance(key, Signer): key = key_get_vk(key)
		return '{{:>{}s}}'.format(key_id_len).format(
			b64_encode(libnacl.crypto_hash_sha256(key.vk))[:key_id_len] )
Exemple #12
0
def Sha256Sum(data):
    return libnacl.crypto_hash_sha256(data)
Exemple #13
0
 def hashit(s):
     b = s.encode('utf-8')
     c = crypto_hash_sha256(b)
     return c.hex()
Exemple #14
0
 def luck(self):
     # type: () -> str
     return libnacl.crypto_hash_sha256(self.hash + self.s.vk)
Exemple #15
0
def Sha256Sum(data):
    return libnacl.crypto_hash_sha256(data)
Exemple #16
0
def dictionary_hash(d):
    digest = ''
    for key in sorted(d):
        digest = libnacl.crypto_hash_sha256(digest + key + d[key])
    return digest
Exemple #17
0
def generate_genesis_block(vk, sk):
    # type: (str, str) -> CpBlock
    prev = libnacl.crypto_hash_sha256('0')
    cons = Cons.new(0, [])
    return CpBlock.new(prev, 0, cons, 1, vk, sk, [], [], 0)
Exemple #18
0
 def hash(self):
     # type: () -> str
     if self._hash is None:
         self._hash = libnacl.crypto_hash_sha256(self.SerializeToString())
     return self._hash
Exemple #19
0
 def hashit(s):
     b = s.encode('utf-8')
     c = crypto_hash_sha256(b)
     return c.hex()