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   2963 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, 787 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, 685 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, 787 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
simon_w

Forum Moderator, 474 Posts

22 April 2008 at 4:31pm

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

Go to Top