def testFullSigning(): # stored securely/privately seed = randombytes(32) # generates key pair based on seed sk = SigningKey(seed=seed) # helper for signing signer = Signer(sk) # this is the public key used to verify signatures (securely shared before-hand with recipient) verkey = signer.verhex # the message to be signed msg = b'1234' # the signature sig = signer.signature(msg) # helper for verification vr = Verifier(verkey) # verification isVerified = vr.verify(sig, msg) assert isVerified
class SimpleSigner(Signer): """ A simple implementation of Signer. This signer creates a public key and a private key using the seed value provided in the constructor. It internally uses the NaclSigner to generate the signature and keys. """ # TODO: Do we need both alias and identifier? def __init__(self, identifier=None, seed=None, alias=None): """ Initialize the signer with an identifier and a seed. :param identifier: some identifier that directly or indirectly references this client :param seed: the seed used to generate a signing key. """ # should be stored securely/privately self.seed = seed if seed else randombytes(32) # generates key pair based on seed self.sk = SigningKey(seed=self.seed) # helper for signing self.naclSigner = NaclSigner(self.sk) # this is the public key used to verify signatures (securely shared # before-hand with recipient) self.verkey = hexToFriendly(hexlify(self.naclSigner.verraw)) self._identifier = identifier or self.verkey self._alias = alias @property def alias(self) -> str: return self._alias @property def identifier(self) -> str: return self._identifier @property def seedHex(self) -> bytes: return hexlify(self.seed) def sign(self, msg: Dict) -> Dict: """ Return a signature for the given message. """ ser = serializeMsg(msg) bsig = self.naclSigner.signature(ser) sig = base58.b58encode(bsig) return sig
class DidSigner(DidIdentity, Signer): """ A simple implementation of Signer for DIDs (Distributed Identifiers). This signer creates a public key and a private key using the seed value provided in the constructor. It internally uses the NaclSigner to generate the signature and keys. """ # TODO: Do we need both alias and identifier? def __init__(self, identifier=None, seed=None, alias=None): """ Initialize the signer with an identifier and a seed. :param identifier: some identifier that directly or indirectly references this client :param seed: the seed used to generate a signing key. """ # should be stored securely/privately self.seed = seed if seed else randombytes(32) # generates key pair based on seed self.sk = SigningKey(seed=self.seed) # helper for signing self.naclSigner = NaclSigner(self.sk) Signer.__init__(self) DidIdentity.__init__(self, identifier, rawVerkey=self.naclSigner.verraw) self._alias = alias @property def alias(self) -> str: return self._alias @alias.setter def alias(self, value): self._alias = value @property def seedHex(self) -> bytes: return hexlify(self.seed) def sign(self, msg: Dict) -> Dict: """ Return a signature for the given message. """ ser = serializeMsg(msg) bsig = self.naclSigner.signature(ser) sig = base58.b58encode(bsig) return sig
class SimpleSigner(Signer): """ A simple implementation of Signer. This signer creates a public key and a private key using the seed value provided in the constructor. It internally uses the NaclSigner to generate the signature and keys. """ def __init__(self, identifier=None, seed=None): """ Initialize the signer with an identifier and a seed. :param identifier: some identifier that directly or indirectly references this client :param seed: the seed used to generate a signing key. """ # should be stored securely/privately self.seed = seed if seed else randombytes(32) # generates key pair based on seed self.sk = SigningKey(seed=self.seed) # helper for signing self.naclSigner = NaclSigner(self.sk) # this is the public key used to verify signatures (securely shared # before-hand with recipient) self.verkey = self.naclSigner.verhex self.verstr = base64_encode(self.naclSigner.verraw).decode('utf-8') self._identifier = identifier or self.verstr @property def identifier(self) -> str: return self._identifier def sign(self, msg: Dict) -> Dict: """ Return a signature for the given message. """ ser = serializeForSig(msg) bsig = self.naclSigner.signature(ser) b64sig = base64_encode(bsig) sig = b64sig.decode('utf-8') return sig
class SimpleSigner(Signer): """ A simple implementation of Signer. This signer creates a public key and a private key using the seed value provided in the constructor. It internally uses the NaclSigner to generate the signature and keys. """ def __init__(self, identifier, seed=None): """ Initialize the signer with an identifier and a seed. :param identifier: some identifier that directly or indirectly references this client :param seed: the seed used to generate a signing key. """ self.identifier = identifier # should be stored securely/privately self.seed = seed if seed else randombytes(32) # generates key pair based on seed self.sk = SigningKey(seed=self.seed) # helper for signing self.naclSigner = NaclSigner(self.sk) # this is the public key used to verify signatures (securely shared # before-hand with recipient) self.verkey = self.naclSigner.verhex self.verstr = base64_encode(self.naclSigner.keyraw).decode('utf-8') def sign(self, msg: Mapping) -> Mapping: """ Return a signature for the given message. """ ser = serializeForSig(msg) bsig = self.naclSigner.signature(ser) b64sig = base64_encode(bsig) sig = b64sig.decode('utf-8') return sig
class DidSigner(Signer): """ A simple implementation of Signer for DIDs (Distributed Identifiers). This signer creates a public key and a private key using the seed value provided in the constructor. It internally uses the NaclSigner to generate the signature and keys. """ # TODO: Do we need both alias and identifier? def __init__(self, identifier=None, seed=None, alias=None): """ Initialize the signer with an identifier and a seed. :param identifier: some identifier that directly or indirectly references this client :param seed: the seed used to generate a signing key. """ # should be stored securely/privately self.seed = seed if seed else randombytes(32) # generates key pair based on seed self.sk = SigningKey(seed=self.seed) # helper for signing self.naclSigner = NaclSigner(self.sk) verraw = self.naclSigner.verraw if identifier: self._identifier = identifier self._verkey = rawToFriendly(verraw) self.abbreviated = False else: self._identifier = rawToFriendly(verraw[:16]) self._verkey = rawToFriendly(verraw[16:]) self.abbreviated = True self._alias = alias @property def alias(self) -> str: return self._alias @alias.setter def alias(self, value): self._alias = value @property def identifier(self) -> str: return self._identifier @property def verkey(self) -> str: if self.abbreviated: return '~' + self._verkey else: return self._verkey @property def seedHex(self) -> bytes: return hexlify(self.seed) def sign(self, msg: Dict) -> Dict: """ Return a signature for the given message. """ ser = serializeMsg(msg) bsig = self.naclSigner.signature(ser) sig = base58.b58encode(bsig) return sig