sjudson / paseto.js

PASETO: Platform-Agnostic Security Tokens
MIT License
269 stars 16 forks source link

Store key as string and convert back to Key instance #3

Closed katp4 closed 5 years ago

katp4 commented 5 years ago

Hi,

First of all thanks for this, it has been very useful.

I want to store the key generated by return encoder.symmetric() to a mongoDB database as a string, retrieve it and then use it to decrypt data. However, when storing it as string, it loses all of its attached properties i.e key.protocol().

Do you have any suggestions on how to do this? Is there a function i can use/expose i.e StringToV2?

katp4 commented 5 years ago

Scratch that, solved it with the inject function :)

Extarys commented 5 years ago

Hey @katp4, can you tell me how you did this? I thought the key was a string, but it's an object, quite hard to store easily, would prefer a simple string instead of this mess :cry:

sjudson commented 5 years ago

@Extarys - You can extract the raw key buffer using the corresponding .raw() method - https://github.com/sjudson/paseto.js/blob/master/lib/key/symmetric.js#L182-L195

The use of an object instead of a raw string is intentional - it allows additional validation to be required before a key may be used, and also reduces the propensity for mishaps with improper passing/use of keying material.

Extarys commented 5 years ago

Thank you @sjudson for that! I understand the why, I just find the documentation in the readme hard to understand when you are not to familiar with it. That's where answers like yours help me a lot.

Thanks again.