Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Archive /

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

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

Encrypted Fields


Go to End


13 Posts   3505 Views

Avatar
Blackdog

Community Member, 156 Posts

3 April 2008 at 12:24am

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.

Avatar
Fuzz10

Community Member, 791 Posts

3 April 2008 at 3:19am

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

Avatar
Blackdog

Community Member, 156 Posts

3 April 2008 at 12:50pm

thanks Fuzz

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

Avatar
Sam

Administrator, 690 Posts

3 April 2008 at 9:29pm

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));
}

Avatar
Fuzz10

Community Member, 791 Posts

4 April 2008 at 1:06am

Ah...

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

Avatar
Blackdog

Community Member, 156 Posts

4 April 2008 at 1:55am

that is fantastic.. thanks for the feedback.

Avatar
Blackdog

Community Member, 156 Posts

22 April 2008 at 3:51pm

Edited: 22/04/2008 4:10pm

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.

Avatar
(deleted)

Community Member, 473 Posts

22 April 2008 at 4:31pm

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

Go to Top