Odoo Help


How to encrypt a field before to record it?

on 7/16/13, 9:30 AM 3,616 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:
        i >>= 1

    final = m.digest()
    return final


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

    _inherit = 'res.partner'

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


class password_table(osv.osv):

    _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),

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

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

AJ Schrafel Paper Corp
on 7/16/13, 11:08 AM

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"

on 7/16/13, 11:27 AM

AJ Schrafel Paper Corp

--AJ Schrafel Paper Corp--
| 4 5 8
Bronx, United States
--AJ Schrafel Paper Corp--

Exploring OpenERP for business

AJ Schrafel Paper Corp
On 7/16/13, 12:01 PM

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.

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

on 7/17/13, 7:43 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 7/16/13, 9:30 AM
Seen: 3616 times
Last updated: 3/16/15, 8:10 AM