コード例 #1
0
ファイル: test_bindings.py プロジェクト: xueyumusic/pynacl
def test_secretbox_wrong_length():
    with pytest.raises(ValueError):
        c.crypto_secretbox(b"", b"", b"")
    with pytest.raises(ValueError):
        c.crypto_secretbox(b"", b"", b"\x00" * c.crypto_secretbox_KEYBYTES)
    with pytest.raises(ValueError):
        c.crypto_secretbox_open(b"", b"", b"")
    with pytest.raises(ValueError):
        c.crypto_secretbox_open(
            b"", b"", b"\x00" * c.crypto_secretbox_KEYBYTES)
コード例 #2
0
def test_secretbox_wrong_length():
    with pytest.raises(ValueError):
        c.crypto_secretbox(b"", b"", b"")
    with pytest.raises(ValueError):
        c.crypto_secretbox(b"", b"", b"\x00" * c.crypto_secretbox_KEYBYTES)
    with pytest.raises(ValueError):
        c.crypto_secretbox_open(b"", b"", b"")
    with pytest.raises(ValueError):
        c.crypto_secretbox_open(b"", b"",
                                b"\x00" * c.crypto_secretbox_KEYBYTES)
コード例 #3
0
def test_secretbox():
    key = b"\x00" * c.crypto_secretbox_KEYBYTES
    msg = b"message"
    nonce = b"\x01" * c.crypto_secretbox_NONCEBYTES
    ct = c.crypto_secretbox(msg, nonce, key)
    assert len(ct) == len(msg) + c.crypto_secretbox_BOXZEROBYTES
    assert tohex(ct) == "3ae84dfb89728737bd6e2c8cacbaf8af3d34cc1666533a"
    msg2 = c.crypto_secretbox_open(ct, nonce, key)
    assert msg2 == msg

    with pytest.raises(CryptoError):
        c.crypto_secretbox_open(
            msg + b"!",
            nonce,
            key,
        )
コード例 #4
0
ファイル: test_bindings.py プロジェクト: xueyumusic/pynacl
def test_secretbox():
    key = b"\x00" * c.crypto_secretbox_KEYBYTES
    msg = b"message"
    nonce = b"\x01" * c.crypto_secretbox_NONCEBYTES
    ct = c.crypto_secretbox(msg, nonce, key)
    assert len(ct) == len(msg) + c.crypto_secretbox_BOXZEROBYTES
    assert tohex(ct) == "3ae84dfb89728737bd6e2c8cacbaf8af3d34cc1666533a"
    msg2 = c.crypto_secretbox_open(ct, nonce, key)
    assert msg2 == msg

    with pytest.raises(CryptoError):
        c.crypto_secretbox_open(
            msg + b"!",
            nonce,
            key,
        )
コード例 #5
0
def open_secretbox(private, ciphertext, nonce):
    try:
        plaintext = c.crypto_secretbox_open(base58.b58decode(ciphertext),
                                            base58.b58decode(nonce),
                                            base58.b58decode(private))
    except CryptoError:
        msg = "Decryption failed. Ciphertext failed verification.\n" + "(解密失败,用户将留言设置为私密)"
        return msg.encode('utf-8')
    return plaintext
コード例 #6
0
    async def _receive_encrypted(self) -> DecryptedMessage:
        """Receive an encrypted message."""

        e = NSTPMessage()
        e_length = struct.unpack(">H", await self.reader.readexactly(2))[0]
        e.ParseFromString(await self.reader.readexactly(e_length))
        e_type = e.WhichOneof("message_")
        if e_type != "encrypted_message":
            raise Exception(f"expected encrypted message, received {e_type}")

        m = DecryptedMessage()
        m.ParseFromString(
            crypto_secretbox_open(e.encrypted_message.ciphertext,
                                  e.encrypted_message.nonce, self.reader_key))
        return m
コード例 #7
0
def handleEncryptedMessage(s, nstp_msg):
    print("inside handleEncryptedMessage")
    if s in client_init:
        encr_msg = nstp_v3_pb2.EncryptedMessage()
        encr_msg.CopyFrom(nstp_msg.encrypted_message)
        print(encr_msg.ciphertext, encr_msg.nonce)
        decr_msg_b = crypto_secretbox_open(encr_msg.ciphertext, encr_msg.nonce,
                                           dict_session_keys[s][0])
        print(decr_msg_b)
        decr_msg = nstp_v3_pb2.DecryptedMessage()
        decr_msg.ParseFromString(decr_msg_b)
        switcher = {
            'auth_request': handleAuthReq,
            'ping_request': handlePingReq,
            'store_request': handleStoreReq,
            'load_request': handleLoadReq
        }
        func = switcher.get(decr_msg.WhichOneof("message_"))
        func(s, decr_msg)
    else:
        print("out of spec")
        errorRes(s, "out of spec")