Пример #1
0
 def get_asymmetric_key():
     """
     Generate a new asymmetric key and set it on the server
     """
     private_key, public_key = Asymmetric.generate_key()
     self.private_key, self.public_key = private_key, public_key
     return {"private key": private_key, "public key": public_key}
Пример #2
0
 def verify(signature: str, text: str):
     """
     Verify the text
     """
     if Asymmetric.verify(signature=signature, text=text, public_key=self.public_key):
         return {"detail": "verified"}
     else:
         raise HTTPException(status_code=400, detail="The text is not verified")
Пример #3
0
 def test_generate_key(self):
     private_key, public_key = Asymmetric.generate_key()
     self.assertNotEqual(private_key, '')
     self.assertNotEqual(public_key, '')
     self.assertNotEqual(private_key, public_key)
     try:
         int(private_key, 16)
         int(public_key, 16)
     except ValueError:
         self.fail()
Пример #4
0
    def test_asymmetric_test(self):
        private_key, public_key = Asymmetric.generate_key()
        try:
            Validator.asymmetric_key(private_key=private_key,
                                     public_key=public_key)
        except CryptoError:
            self.fail()

        with self.assertRaises(CryptoError):
            Validator.asymmetric_key('aaaa', 'aaaa')

        with self.assertRaises(CryptoError):
            Validator.asymmetric_key('asdf', 'asdf')
Пример #5
0
 def test_encryption(self):
     private_key, public_key = Asymmetric.generate_key()
     encrypted = Asymmetric.encrypt('my secret', public_key)
     decrypted = Asymmetric.decrypt(encrypted, private_key)
     self.assertEqual(decrypted, 'my secret')
Пример #6
0
 def test_sign_verify_different_key(self):
     private_key, _ = Asymmetric.generate_key()
     _, public_key = Asymmetric.generate_key()
     text = 'my text'
     signature = Asymmetric.sign(text, private_key)
     self.assertFalse(Asymmetric.verify(signature, text, public_key))
Пример #7
0
 def test_sign_verify(self):
     private_key, public_key = Asymmetric.generate_key()
     text = 'my text'
     signature = Asymmetric.sign(text, private_key)
     self.assertTrue(Asymmetric.verify(signature, text, public_key))
Пример #8
0
 def asymmetric_encode(text: str):
     """
     Encrypt text
     """
     return {"text": Asymmetric.encrypt(text=text, public_key=self.public_key)}
Пример #9
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.private_key, self.public_key = Asymmetric.generate_key()
        self.symmetric_key = Symmetric.generate_key()

        @self.get("/symmetric/key")
        def get_symmetric_key():
            """
            Generate a new symmetric key
            """
            return {"key": Symmetric.generate_key()}

        @self.post("/symmetric/key")
        def set_symmetric_key(key: str):
            """
            Set the hex-encoded symmetric key on the server
            """
            try:
                Validator.symmetric_key(key)
            except CryptoError as e:
                raise HTTPException(status_code=400, detail=str(e))
            else:
                self.symmetric_key = key

        @self.post("/symmetric/encode")
        def symmetric_encode(text: str):
            """
            Encrypt text
            """
            return {"text": Symmetric.encrypt(text=text, key=self.symmetric_key)}

        @self.post("/symmetric/decode")
        def symmetric_decode(text: str):
            """
            Decrypt text
            """
            return {"text": Symmetric.decrypt(text=text, key=self.symmetric_key)}

        @self.get("/asymmetric/key")
        def get_asymmetric_key():
            """
            Generate a new asymmetric key and set it on the server
            """
            private_key, public_key = Asymmetric.generate_key()
            self.private_key, self.public_key = private_key, public_key
            return {"private key": private_key, "public key": public_key}

        @self.get("/asymmetric/key/ssh")
        def get_asymmetric_key_ssh():
            """
            Generate a new asymmetric key in OpenSSH format
            """
            private_key, public_key = Asymmetric.generate_key_openssh()
            return {"private key": private_key, "public key": public_key}

        @self.post("/asymmetric/key")
        def set_asymmetric_key(private_key: str, public_key: str):
            """
            Set the hex-encoded asymmetric key on the server
            """
            try:
                Validator.asymmetric_key(private_key=private_key, public_key=public_key)
            except CryptoError as e:
                raise HTTPException(status_code=400, detail=str(e))
            else:
                self.private_key, self.public_key = private_key, public_key

        @self.post("/asymmetric/sign")
        def sign(text: str):
            """
            Sign the text
            """
            return {"signature": Asymmetric.sign(text=text, private_key=self.private_key)}

        @self.post("/asymmetric/verify")
        def verify(signature: str, text: str):
            """
            Verify the text
            """
            if Asymmetric.verify(signature=signature, text=text, public_key=self.public_key):
                return {"detail": "verified"}
            else:
                raise HTTPException(status_code=400, detail="The text is not verified")

        @self.post("/asymmetric/encode")
        def asymmetric_encode(text: str):
            """
            Encrypt text
            """
            return {"text": Asymmetric.encrypt(text=text, public_key=self.public_key)}

        @self.post("/asymmetric/decode")
        def asymmetric_decode(text: str):
            """
            Decrypt text
            """
            return {"text": Asymmetric.decrypt(text=text, private_key=self.private_key)}
Пример #10
0
 def sign(text: str):
     """
     Sign the text
     """
     return {"signature": Asymmetric.sign(text=text, private_key=self.private_key)}
Пример #11
0
 def get_asymmetric_key_ssh():
     """
     Generate a new asymmetric key in OpenSSH format
     """
     private_key, public_key = Asymmetric.generate_key_openssh()
     return {"private key": private_key, "public key": public_key}
Пример #12
0
 def asymmetric_decode(text: str):
     """
     Decrypt text
     """
     return {"text": Asymmetric.decrypt(text=text, private_key=self.private_key)}