Beispiel #1
0
    def _Decrypt(self, value):
        # Attempt to load the contents as json (the new blob format) and, if that
        # fails, assume the result is encoded using KeyCzar's non-standard base64
        # format.
        try:
            description = json.loads(value)
            if not isinstance(description, dict):
                raise ValueError
        except ValueError:
            # old format, contains base64 data
            return crypto.Decrypt(value)
        else:
            backend = description.get('backend')
            assert backend and backend in _CRYPTO_BACKEND

            return _CRYPTO_BACKEND[backend].Decrypt(description['value'],
                                                    key_name=self._key_name)
  def testDecrypt(self, crypter, cauliflowervest_reader):
    encrypted_data = 'foodata'
    key_type = 'footype'

    mock_reader = mock.MagicMock()
    cauliflowervest_reader.return_value = mock_reader

    mock_crypter = mock.MagicMock()
    mock_crypter.Decrypt.return_value = 'result'
    crypter.return_value = mock_crypter

    r = crypto.Decrypt(encrypted_data, key_type=key_type)
    self.assertEqual(r, 'result')

    mock_reader.LoadKeys.assert_called_once_with(key_type)
    crypter.assert_called_once_with(reader=mock_reader)
    mock_crypter.Decrypt.assert_called_once_with(encrypted_data)
Beispiel #3
0
    def testDecrypt(self):
        self.mox.StubOutWithMock(crypto, 'CauliflowerVestReader', True)
        self.mox.StubOutWithMock(crypto.CauliflowerVestReader, 'LoadKeys')
        self.mox.StubOutWithMock(crypto.keyczar, 'Crypter', True)

        encrypted_data = 'foodata'
        key_type = 'footype'

        mock_reader = self.mox.CreateMockAnything()
        mock_crypter = self.mox.CreateMockAnything()

        crypto.CauliflowerVestReader().AndReturn(mock_reader)
        mock_reader.LoadKeys(key_type).AndReturn(None)
        crypto.keyczar.Crypter(reader=mock_reader).AndReturn(mock_crypter)
        mock_crypter.Decrypt(encrypted_data).AndReturn('result')

        self.mox.ReplayAll()
        r = crypto.Decrypt(encrypted_data, encryption_key_type=key_type)
        self.assertEqual(r, 'result')
        self.mox.VerifyAll()
Beispiel #4
0
 def make_value_from_datastore(self, value):
     """Decrypts the blob value coming from Datastore."""
     return super(EncryptedBlobProperty,
                  self).make_value_from_datastore(crypto.Decrypt(value))