~serpent-consulting-services/openerp-usa/shipping_api_6-1

« back to all changes in this revision

Viewing changes to account_payment_creditcard/rsa_encrypt.py

  • Committer: npgllc
  • Date: 2012-09-13 17:23:36 UTC
  • mfrom: (78.1.3 openerp-usa)
  • Revision ID: npgllc-20120913172336-tom9sako8yops2gx
[IMP]: account_payment_cim_authodotnet, account_payment_creditcard

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2011 NovaPoint Group LLC (<http://www.novapointgroup.com>)
 
6
#    Copyright (C) 2004-2010 OpenERP SA (<http://www.openerp.com>)
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>
 
20
#
 
21
##############################################################################
 
22
 
 
23
from Crypto.PublicKey import RSA
 
24
import base64
 
25
import tools
 
26
 
 
27
def rsa_enc():
 
28
    """
 
29
    Generate a key for RSA Encryption
 
30
    @return : key
 
31
    """
 
32
    key = RSA.generate(1024)
 
33
    return key
 
34
 
 
35
def encrypt(value, key=False):
 
36
    """
 
37
    Encryption using RSA
 
38
    @params value: Value to Encrypt
 
39
    @key: The Key used to Encrypt
 
40
    @return:Dictionary containing the encrypted data and key
 
41
    """
 
42
    res = {}
 
43
    if not key:
 
44
        pub_key = rsa_enc()
 
45
        public_key = pub_key.publickey()
 
46
        enc_data = public_key.encrypt(str(value), 32)
 
47
        res['key'] = base64.encodestring(pub_key.exportKey('DER'))
 
48
    else:
 
49
        privatekey = RSA.importKey(base64.decodestring(key))
 
50
        enc_data = privatekey.encrypt(str(value), 32)
 
51
    res['enc_value'] = base64.encodestring(enc_data[0])
 
52
    return res
 
53
 
 
54
def decrypt(value, key):
 
55
    """
 
56
    Decryption using RSA
 
57
    @params value: Value to Decrypt
 
58
    @key: The Key used to Decrypt
 
59
    @return:The decrypted data 
 
60
    """
 
61
    privatekey = RSA.importKey(base64.decodestring(key))
 
62
    return privatekey.decrypt(base64.decodestring(value))