Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Encrypted Fields

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1 2
Go to End
Author Topic: 2850 Views
  • Blackdog
    Avatar
    Community Member
    156 Posts

    Encrypted Fields Link to this post

    Hi All,

    Hoping someone can help me a with a little problem.

    How can I get the users to fill in a varchar field and have it stored in a encrypted format in the database?

    thanks.

  • Fuzz10
    Avatar
    Community Member
    787 Posts

    Re: Encrypted Fields Link to this post

    Don't know if this is functional , but I found an EncryptField in the API ?

    Did you try that ?

    http://api.silverstripe.com/default/EncryptField.html

  • Blackdog
    Avatar
    Community Member
    156 Posts

    Re: Encrypted Fields Link to this post

    thanks Fuzz

    I will check it out and return back with my findings.

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Encrypted Fields Link to this post

    EncyptField is just a deprecated duplicate of PasswordField.

    To do what you've requested, you should define an onBeforeWrite() method on your DataObject.

    This example will md5-encode the field "MyField".

    function onBeforeWrite() {
    if(!$this->ID || $this->changed['MyField']) {
    $this->MyField = md5($this->MyField);
    }
    }

    Alternatively, if you need it to get encrypted straight away and not once the dataobject is written, you could define setMyField(). This is the function that is called when you go $obj->MyField = 'value';

    function setMyField($value) {
    $this->setField('MyField', md5($value));
    }

  • Fuzz10
    Avatar
    Community Member
    787 Posts

    Re: Encrypted Fields Link to this post

    Ah...

    As expected, SS offers an an elegant solution. ;-)

  • Blackdog
    Avatar
    Community Member
    156 Posts

    Re: Encrypted Fields Link to this post

    that is fantastic.. thanks for the feedback.

  • Blackdog
    Avatar
    Community Member
    156 Posts

    Re: Encrypted Fields Link to this post

    Sam,

    Your suggestion worked great, thanks.

    My only problem now is I can't find where to decrypt the data for the Admin to view it.

    If anyone has any advice I would be greatful.

    thanks.

  • simon_w
    Avatar
    Forum Moderator
    473 Posts

    Re: Encrypted Fields Link to this post

    Assuming you used the md5 function, you can't reverse it.

    2850 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.