No announcement yet.

How to encrypt data

  • Filter
  • Time
  • Show
Clear All
new posts

  • How to encrypt data

    I am trying to encrypt some data that I need to store in the database, but I don't know exactly which classes are responsible for the process of storing for example the field description for contact entity.

    Could you help me please?​​​​​​


  • #2
    to save look for hooks in github documentation
    to read override get method in service class for your record


    • #3
      I would suggest to create an beforeSave hook for Contact entity like this:

      namespace Espo\Custom\Hooks\Contact;
      use Espo\ORM\Entity;
      class ContactHooks extends \Espo\Core\Hooks\Base
          public function beforeSave(Entity $entity, array $options=array())
              // get the un​​​​​​-encrypted description from the model
              $description = $entity->get('description');
              // encrypt the description field prior to storing in database
      $fileM = new \Espo\Core\Utils\File\Manager();
      $config = new \Espo\Core\Utils\Config($fileM);
              $descriptionHash = new \Espo\Core\Utils\PasswordHash($config);
              $encodedDescription = $descriptionHash->hash($description,true);
              // store the encoded description in the model
              $entity->set('description', $encodedDescription);
      Please note that this is similar to the code that I use to encode passwords for new users but haven't tested it actually encrypting the description field, so this code is good for writing encrypted data but you will need to follow tothewine suggestion and overwrite the get method in the service class for Contact to be able to read the field, but since I haven't done that myself, I can't provide any guidance about it.

      Please correspond to our efforts to help by posting your solution after you implement it, so other participants can benefit also.

      Last edited by telecastg; 01-11-2020, 04:11 AM.


      • #4
        nice thanks

        the best is in entityManager -> fieldAttribute -> checkbox : isEncrypted ..
        so it's outOfBox and we can decide witch is encrypted or not.

        i think it's not difficult to implement this



        • #5
          Thanks item, it sounds a lot easier that having to code hooks !

          Do you know if this setting also takes care of the mechanism for entering the password and reading the encrypted file ?


          • #6
            Hello telecastg,
            sorry i have not skill for this kind of .. and not understand :s ... but for me it's easy for someone have skill :

            1) common hook (orm entity maybe)
            2) check if field have attribute isEncrypted (like personalData or required or.... )
            3) then crypt data ..

            i have make search for "personalData" in openFiles in sublimeText.
            fieldManager and/or entity are i think where the solution can be ok.