1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
6
|
|
|
# the License. You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
import binascii |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def symmetric_encrypt(encrypt_key, message): |
20
|
|
|
""" |
21
|
|
|
Encrypt the given message using the encrypt_key. Returns a UTF-8 str |
22
|
|
|
ready to be stored in database. Note that we convert the hex notation |
23
|
|
|
to a ASCII notation to produce a UTF-8 friendly string. |
24
|
|
|
|
25
|
|
|
Also, this method will not return the same output on multiple invocations |
26
|
|
|
of same method. The reason is that the Encrypt method uses a different |
27
|
|
|
'Initialization Vector' per run and the IV is part of the output. |
28
|
|
|
|
29
|
|
|
:param encrypt_key: Symmetric AES key to use for encryption. |
30
|
|
|
:type encrypt_key: :class:`keyczar.keys.AesKey` |
31
|
|
|
|
32
|
|
|
:param message: Message to be encrypted. |
33
|
|
|
:type message: ``str`` |
34
|
|
|
|
35
|
|
|
:rtype: ``str`` |
36
|
|
|
""" |
37
|
|
|
return binascii.hexlify(encrypt_key.Encrypt(message)).upper() |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def symmetric_decrypt(decrypt_key, crypto): |
41
|
|
|
""" |
42
|
|
|
Decrypt the given crypto text into plain text. Returns the original |
43
|
|
|
string input. Note that we first convert the string to hex notation |
44
|
|
|
and then decrypt. This is reverse of the encrypt operation. |
45
|
|
|
|
46
|
|
|
:param decrypt_key: Symmetric AES key to use for decryption. |
47
|
|
|
:type decrypt_key: :class:`keyczar.keys.AesKey` |
48
|
|
|
|
49
|
|
|
:param crypto: Crypto text to be decrypted. |
50
|
|
|
:type crypto: ``str`` |
51
|
|
|
|
52
|
|
|
:rtype: ``str`` |
53
|
|
|
""" |
54
|
|
|
return decrypt_key.Decrypt(binascii.unhexlify(crypto)) |
55
|
|
|
|