generalpiston / typeorm-encrypted

Encrypted field for typeorm.
MIT License
75 stars 19 forks source link

Feature: Adds support for JSON Encryption #45

Closed Theo- closed 2 years ago

Theo- commented 2 years ago

Adds support for a JSON column encryption. Does exactly the same thing as the string column encryption and works the same way.

Can be used in a similar way:

import { Entity, Column } from "typeorm";
import { JSONEncryptionTransformer } from "typeorm-encrypted";

@Entity()
class User {
  ...

  @Column({
    type: "json",
    nullable: false,
    transformer: new JSONEncryptionTransformer({
      key: 'e41c966f21f9e1577802463f8924e6a3fe3e9751f201304213b2f845d8841d61',
      algorithm: 'aes-256-cbc',
      ivLength: 16,
      iv: 'ff5ac19190424b1d88f9419ef949ae56'
    })
  })
  secret: string;

  ...
}

Details: This will store the json value as { encrypted: '<encrypted jsonified string>' } and automatically transformed back to the origin json when read from the database.

generalpiston commented 2 years ago

@Theo- thanks for putting this PR together.

I have a question: can we reuse the other transformer? We could pass an argument that tells the transformer to parse and stringify the object.

Usage might look like this:

@Column({
    type: "json",
    nullable: false,
    transformer: new EncryptionTransformer({
      key: 'e41c966f21f9e1577802463f8924e6a3fe3e9751f201304213b2f845d8841d61',
      algorithm: 'aes-256-cbc',
      ivLength: 16,
      iv: 'ff5ac19190424b1d88f9419ef949ae56',
      json: true
    })
  })
  secretObject: object;

Let me know if you have any questions or thoughts!