Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug #19

Closed
rubiin opened this issue Jun 29, 2020 · 15 comments
Closed

bug #19

rubiin opened this issue Jun 29, 2020 · 15 comments

Comments

@rubiin
Copy link

rubiin commented Jun 29, 2020

The example in readme for transformers doesnot work. getting invalid iv length

@generalpiston
Copy link
Owner

@rubiin could you share more? There's a test for the config in the README and it's passing.

@rubiin
Copy link
Author

rubiin commented Jun 29, 2020

i am using transformer: new EncryptionTransformer({ key: 'e41c966f21f9e1577802463f8924e6a3fe3e9751f201304213b2f845d8841d61', algorithm: 'aes-256-cbc', ivLength: 16, iv: 'ff5ac19190424b1d88f9419ef949ae56' })

in one of the entities column and saving the entity with .save . However i get


Error: Invalid IV length
    at Decipheriv.createCipherBase (internal/crypto/cipher.js:103:19)
    at Decipheriv.createCipherWithIV (internal/crypto/cipher.js:121:20)
    at new Decipheriv (internal/crypto/cipher.js:262:22)
    at Object.createDecipheriv (crypto.js:127:10)
    at Object.decryptData (/home/devina/Desktop/Projects/nodejs/Triple-A/node_modules/typeorm-encrypted/lib/crypto.js:22:29)
    at EncryptionTransformer.from (/home/devina/Desktop/Projects/nodejs/Triple-A/node_modules/typeorm-encrypted/lib/transformer.js:12:25)
    at Function.ApplyValueTransformers.transformFrom (/home/devina/Desktop/Projects/nodejs/Triple-A/node_modules/typeorm/util/ApplyValueTransformers.js:13:28)
    at PostgresDriver.prepareHydratedValue (/home/devina/Desktop/Projects/nodejs/Triple-A/node_modules/typeorm/driver/postgres/PostgresDriver.js:576:69)
    at /home/devina/Desktop/Projects/nodejs/Triple-A/node_modules/typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer.js:121:56
    at Array.forEach (<anonymous>)


@rubiin
Copy link
Author

rubiin commented Jun 29, 2020

@generalpiston

@generalpiston
Copy link
Owner

@rubiin what version of node are you using?

@rubiin
Copy link
Author

rubiin commented Jun 29, 2020

node 12.18.0 @generalpiston

@rubiin
Copy link
Author

rubiin commented Jun 29, 2020

also do i need to migrate after adding the transformers? its not mentioned in the readme

@generalpiston
Copy link
Owner

Transformers do not require database schema changes. If you're adding or removing tables, columns, etc. you'll need to synchronize the DB. If you're adding the transformer to an existing table, then you'll need to migrate the data. It's best to do this programmatically.

@rubiin
Copy link
Author

rubiin commented Jun 29, 2020

i am adding a transformer to an existing table

@rubiin
Copy link
Author

rubiin commented Jun 29, 2020

also i am still getting that error

@generalpiston
Copy link
Owner

I see. I'm guessing the problem is that the pre-existing data isn't encrypted. When the data is encrypted, the first 16 bytes (configurable) of the data is the IV. Here's a way to migrate your data:

  1. Add a new column (col B) to the table. Configure the column to be encrypted. Remove the transformer from the original column (col A).
  2. Write a script that queries all of the entries in the table. Set the value of col B to col A.
  3. Save all the records.
  4. Rename col A to something else manually.
  5. Rename col B to the original name of col A manually.
  6. Remove the typeorm configuration for col A.
  7. Rename the typeorm configuration for col B to col A's name.
  8. Remove col A (unencrypted column) from the table manually.

@rubiin
Copy link
Author

rubiin commented Jun 29, 2020

i just created a new column and added the transformer and it worked. what could be the problem . Existing datas

@generalpiston
Copy link
Owner

@rubiin the wrong IV is being used. When decrypting, the IV is derived from the encrypted contents found in the database. If the contents aren't encrypted, then it will use the wrong IV.

I've created #20 to follow up on migrating data when adding the transformer to an existing table / column.

@rubiin
Copy link
Author

rubiin commented Jun 29, 2020

also can you tell me how can i migrate an existing column. A code sample would be very helpful

@generalpiston
Copy link
Owner

const entities = repository.find();
entities.each(e => e.colB = e.colA);
repository.save(entities);

@rubiin
Copy link
Author

rubiin commented Jun 29, 2020

thanks for the quick response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants