This question has been flagged
1 Reply
12261 Views

I would like record a field (wich is a password) encrypted in my database. I have to encrypt it directly inside my module not in my PostgreSQL database. I did the following :

import md5
import os
from openerp.osv import osv, fields


###### Encrypton of passwords ######

CRYPT64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

def make_salt():
    sbytes = os.urandom(8)
    return ''.join([CRYPT64[ord(b) & 0x3f] for b in sbytes])

def md5crypt(password, salt, magic='$1$'):
    if salt is None:
        salt = make_salt()

    m = md5.new()
    m.update(password + magic + salt)

    mixin = md5.new(password + salt + password).digest()
    for i in range(0, len(password)):
        m.update(mixin[i % 16])

    # Then something really weird...
    # Also really broken, as far as I can tell.  -m
    i = len(password)
    while i:
        if i & 1:
            m.update('\x00')
        else:
            m.update(password[0])
        i >>= 1

    final = m.digest()
    return final

md5crypt(pass_pid)

###### Creation of password table ######
class password(osv.osv):

    _inherit = 'res.partner'

    _columns = {
        'passwords': fields.one2many('password.table','name_pid','user_pid')

   }
password()

class password_table(osv.osv):
    _name="password.table"

    _columns = {
            'name_pid':fields.many2one('res.partner', 'name', required=True),
            'user_pid':fields.char("UserName", size=128, required=True),
            'host_pid':fields.char("IP/HostName", size=128, required=True),
            'pass_pid':fields.function(md5crypt, type="text", string="password", size=128, required=True),
            'com_pid':fields.text("Comment")
        }
password_table()

The error is about my parameters but I don't know at all how to modify that!

Avatar
Discard

can you please post the error you are getting? That would help give you a better answer.

Author

I'm getting this error: "File "/home/integration/oe7_1/addons/password_module/password.py", line 59, in <module> md5crypt(pass_pid) NameError: name 'pass_pid' is not defined"

Best Answer

This is a python programming question, not an openerp issue/ error/ question. I will try to point you in the right direction, but you are going to have to get someone that knows how to program python to do this programming for you, this forum is for openERP questions.

First you should move the encryption function into class password_table(osv.osv):

Second, you want to use this function to encode and store the password, so you have to create a function that will encode when saving the field and another for viewing it.

read how to use functions here: https://doc.openerp.com/6.0/developer/2_5_Objects_Fields_Methods/field_type/#functional-fields

'pass_pid':fields.function( _view_function, fnct_inv=_store_function, type="text", string="password", size=128, required=True),

Third you cannot just cut and paste code from other sources, you have to massage it into openERP so you will have to rewrite the function accordingly. Your functions will have to start something like this:

def _store_function(self, cr, uid, id, name, value, args=None, context=None):

If you need help beyond this you should contact an openERP partner to write the module for you. Custom coding is a service they are happy to provide.

Avatar
Discard
Author

Thanks a million Patently, I will see what can I do with that!