Odoo Help

Welcome!

This community 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.

0

How to encrypt a field before to record it?

By
Quentin
on 7/16/13, 9:30 AM 2,137 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!

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"

Quentin
on 7/16/13, 11:27 AM
0

AJ Schrafel Paper Corp

--AJ Schrafel Paper Corp--
2050
| 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!

Quentin
on 7/17/13, 7:43 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

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