本文整理汇总了Python中nacl.public.PrivateKey.encode方法的典型用法代码示例。如果您正苦于以下问题:Python PrivateKey.encode方法的具体用法?Python PrivateKey.encode怎么用?Python PrivateKey.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nacl.public.PrivateKey
的用法示例。
在下文中一共展示了PrivateKey.encode方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from nacl.public import PrivateKey [as 别名]
# 或者: from nacl.public.PrivateKey import encode [as 别名]
class IDPrivateKey:
def __init__(self, password = None, email = None):
if email is None:
self.salt = defaultsalt
else:
self.salt = email
if password is not None:
self.generate_key(password)
def generate_key(self, password):
self.pri = PrivateKey(scrypt(sha512(password,encoder = RawEncoder),
self.salt,
N = 1<<17,
r = 8,
p = 1,
buflen = 32),
encoder = RawEncoder)
pub = PublicID()
pub.salt = self.salt
pub.key = self.pri.public_key.encode()
self.pub = pub
def to_private_key(self):
return self.pri
def to_public_key(self):
return self.pub
def pri_base58(self):
return b58encode(self.pri.encode(encoder = RawEncoder))
def pub_base58(self):
return self.pub.base58()
示例2: loads
# 需要导入模块: from nacl.public import PrivateKey [as 别名]
# 或者: from nacl.public.PrivateKey import encode [as 别名]
# PKp2_full = loads('{"kty":"EC", "crv":"Curve25519", "x":""}', \
# object_pairs_hook=OrderedDict)
PKp_full['x'] = PKp_b64
PKs_full['x'] = PKs_b64
#PKs2_full['x'] = PKs2_b64
#PKp2_full['x'] = PKp2_b64
## Load peer and server private keys
SK_peer = PrivateKey(bytes.fromhex(SKp))
SK_server = PrivateKey(bytes.fromhex(SKs))
#SK2_peer = PrivateKey(bytes.fromhex(SKp2))
#SK2_server = PrivateKey(bytes.fromhex(SKs2))
## Derive shared secret
Z = scalarmult(SK_peer.encode(), PK_server.encode())
assert(Z == scalarmult(SK_server.encode(), PK_peer.encode()))
#Z2 = scalarmult(SK2_peer.encode(), PK2_server.encode())
#assert(Z2 == scalarmult(SK2_server.encode(), PK2_peer.encode()))
## KDF for completion exchange. Uses NIST Concat KDF.
KDF_input = b'EAP-NOOB' + base64url_decode(Np_b64) + base64url_decode(Ns_b64) +\
base64url_decode(Noob_b64)
KDF_out = KDF(algorithm=SHA256(), length=320, otherinfo=KDF_input,
backend=default_backend()).derive(Z)
Kms = KDF_out[224:256]
Kmp = KDF_out[256:288]
Kz = KDF_out[288:320]
# KDF - for reconnect exchange. Uses NIST Concat KDF. This sample script does
# not exchange new keys in the reconnect exchange and uses the Kz from the
示例3: PrivateKeyProvider
# 需要导入模块: from nacl.public import PrivateKey [as 别名]
# 或者: from nacl.public.PrivateKey import encode [as 别名]
class PrivateKeyProvider(object):
"""
Decrypts private keys.
"""
def __init__(self, key, passphrase: bytes):
self.key = key
# Secure!
self.passphrase = passphrase
self.sign = None
self.encrypt = None
def __enter__(self) -> typing.Tuple[PrivateKey, PrivateKey]:
"""
Provides a pair of private keys.
"""
# Derive the key from the passphrase.
derived = util.derive_passphrase(self.passphrase)
sign_box = SecretBox(derived)
enc_box = SecretBox(derived)
# Decrypt, using the two nonces.
s_d = sign_box.decrypt(self.key._private_signing_seed, self.key._private_signing_nonce)
e_d = enc_box.decrypt(self.key._private_key_raw, self.key._private_nonce)
# Generate a SigningKey out of the seed.
self.sign = SigningKey(s_d)
self.encrypt = PrivateKey(e_d)
# Update the key's public keys.
if self.key._public_key is None:
self.key._public_key = self.encrypt.public_key
if self.key._public_signing_key is None:
self.key._public_signing_key = self.sign.verify_key
return self.encrypt, self.sign
def __exit__(self, exc_type, exc_val, exc_tb):
"""
Re-encrypt.
"""
# Derive the key from the passphrase.
derived = util.derive_passphrase(self.passphrase)
# Generate two random nonces.
nonce1 = random(SecretBox.NONCE_SIZE)
nonce2 = random(SecretBox.NONCE_SIZE)
sign_box = SecretBox(derived)
enc_box = SecretBox(derived)
s_p = self.sign.encode()
e_p = self.encrypt.encode()
s_e = sign_box.encrypt(s_p, nonce1)
e_e = enc_box.encrypt(e_p, nonce2)
# Update `self.key`.
self.key._private_key_raw = e_e.ciphertext
self.key._private_signing_key_raw = s_e.ciphertext
# Bit of a mixed up name.
self.key._private_nonce = e_e.nonce
self.key._private_signing_nonce = s_e.nonce
if exc_type is not None:
raise exc_type(exc_val)
示例4: HTTPMailboxServer
# 需要导入模块: from nacl.public import PrivateKey [as 别名]
# 或者: from nacl.public.PrivateKey import encode [as 别名]
class HTTPMailboxServer(BaseServer):
"""I am a local HTTP-based server, attached to our webapi port. I don't
persist anything myself, but expect my creator to provide me with our
persistent state. I can deliver messages to a local transport (endpoints
inside our same process), or write messages to disk for later retrieval
by remote clients.
"""
def __init__(self, web, enable_retrieval, desc):
BaseServer.__init__(self)
self.web = web
self.privkey = PrivateKey(desc["transport_privkey"].decode("hex"))
self.TID_privkey = desc["TID_private_key"].decode("hex")
# If we feed a local transport, it will have just one TID. If we
# queue messages for any other transports, they'll each have their
# own TID and handler (e.g. a queue and some retrieval credentials).
self.local_TID0 = desc["local_TID0"].decode("hex")
self.local_TID_tokenid = desc["local_TID_tokenid"].decode("hex")
# this is how we get messages from senders
web.get_root().putChild("mailbox", ServerResource(self.handle_msgA))
if enable_retrieval:
# add a second resource for clients to retrieve messages
raise NotImplementedError()
def get_retrieval_descriptor(self):
return { "type": "local",
"transport_privkey": self.privkey.encode().encode("hex"),
"TID_private_key": self.TID_privkey.encode("hex"),
"TID": self.local_TID0.encode("hex"),
"TID_tokenid": self.local_TID_tokenid.encode("hex"),
}
def get_sender_descriptor(self):
baseurl = self.web.get_baseurl()
assert baseurl.endswith("/")
pubkey = self.privkey.public_key
return { "type": "http",
# TODO: we must learn our local ipaddr and the webport
"url": baseurl + "mailbox",
"transport_pubkey": pubkey.encode().encode("hex"),
}
def register_local_transport_handler(self, handler):
self.local_transport_handler = handler
def handle_msgA(self, msgA):
pubkey1_s, boxed = parseMsgA(msgA)
msgB = Box(self.privkey, PublicKey(pubkey1_s)).decrypt(boxed)
# this ends the observable errors
eventually(self.handle_msgB, msgB)
def handle_msgB(self, msgB):
MSTID, msgC = parseMsgB(msgB)
TID = rrid.decrypt(self.TID_privkey, MSTID)
if TID == self.local_TID_tokenid:
self.local_transport_handler(msgC)
else:
# TODO: look up registered transports, queue message
self.signal_unrecognized_TID(TID)
def signal_unrecognized_TID(self, TID):
# this can be overridden by unit tests
raise KeyError("unrecognized transport identifier")