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.

Archive

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

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

Encrypted Fields


Go to End
Reply

13 Posts   2880 Views

Avatar
Blackdog

3 April 2008 at 12:24am Community Member, 156 Posts

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

3 April 2008 at 3:19am Community Member, 787 Posts

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

3 April 2008 at 12:50pm Community Member, 156 Posts

thanks Fuzz

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

Avatar
Sam

3 April 2008 at 9:29pm Administrator, 685 Posts

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

4 April 2008 at 1:06am Community Member, 787 Posts

Ah...

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

Avatar
Blackdog

4 April 2008 at 1:55am Community Member, 156 Posts

that is fantastic.. thanks for the feedback.

Avatar
Blackdog

22 April 2008 at 3:51pm (Last edited: 22 April 2008 4:10pm), Community Member, 156 Posts

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
simon_w

22 April 2008 at 4:31pm Forum Moderator, 474 Posts

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

Go to Top