Skip to content

Commit e16082b

Browse files
authored
Merge pull request #2 from mikermcneil/patch-1
write up some additional reference docs in README
2 parents f002f38 + ba16f84 commit e16082b

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,82 @@ async function storeSomeSecrets (doc) {
129129
}
130130
```
131131

132+
133+
### Standalone usage
134+
135+
136+
After requiring and invoking this module as shown above, you'll be able to call any of several available methods on the hydrated "encryptedAttributes" object. Those methods are documented below.
137+
138+
> Alternatively, you can simply chain:
139+
>
140+
> ```js
141+
> const EncryptedAttributes = require('encrypted-attr');
142+
>
143+
> var rawSocialSecurityNumber = '555-55-5555';
144+
>
145+
> var encryptedSSN = EncryptedAttributes(['ssn'], {
146+
> keyId: 'default',
147+
> keys: { default: 'keyboardcat' }
148+
> })
149+
> .encryptAttribute(undefined, '555-55-5555');
150+
> ```
151+
152+
153+
##### encryptAttribute()
154+
155+
Encrypt a string using one of your keys (specifically, the key indicated by the configured `keyId`).
156+
157+
```js
158+
var encryptedString = ea.encryptAttribute(optionalSourceObj, rawString);
159+
```
160+
161+
> `optionalSourceObj` is only relevant if the `verifyId` option is enabled. If you don't have that option enabled, you can simply pass in `undefined`.
162+
163+
##### decryptAttribute()
164+
165+
Decrypt a value.
166+
167+
```js
168+
var rawString = ea.encryptAttribute(optionalSourceObj, encryptedString)
169+
```
170+
171+
> `optionalSourceObj` is only relevant if the `verifyId` option is enabled. If you don't have that option enabled, you can simply pass in `undefined`.
172+
173+
174+
##### encryptAll()
175+
176+
Encrypt a subset of the provided dictionary (i.e. plain JavaScript object) of raw, unencrypted values.
177+
178+
```js
179+
var partiallyEncryptedObj = ea.encryptAll(rawObj)
180+
```
181+
182+
> Only fields identified in the array of keypaths you passed in during initial setup of this module will actually be encrypted.
183+
184+
185+
##### decryptAll()
186+
187+
Decrypt a subset of the provided dictionary of encrypted values.
188+
189+
```js
190+
var rawObj = ea.decryptAll(partiallyEncryptedObj)
191+
```
192+
193+
> Only fields identified in the array of keypaths you passed in during initial setup of this module will actually be decrypted.
194+
195+
196+
197+
198+
# Options
199+
200+
| Option | Type | Required? | Details |
201+
|:-----------|----------------|:---------------|:---------------------------------------------------------------------|
202+
| keyId | ((string)) | Required(ish) | The id of the key to use for all **new encryptions**. This is _not_ necessarily the only key that will be used for decryptions though, because the key id you choose gets embedded into the encrypted string itself. Then before that string is decrypted, this module simply unpacks that key id and uses it to determine the appropriate decryption key. This approach allows for using multiple keys. (Note that this option is only _technically_ required if you need to encrypt new data. If you are only decrypting existing data, you needn't pass it in.) |
203+
| keys | ((dictionary)) | Required | A dictionary of all relevant data encryption keys (DEKs). Since encrypted strings _contain the key id that was used to encrypt them_, it's important that `keys` contain the appropriate keys for any past key ids it might run across when attempting to decrypt those strings.
204+
| verifyId | ((boolean)) | _Optional._ | Whether or not to (A) use the `id` property of a provided source object as an additional piece of metadata during encryption, and (B) expect that metadata to be embedded in encrypted strings during decryption, and throw an error if the expected idea does not match. Defaults to `false`.
205+
206+
207+
132208
# License
133209

134210
[MIT](LICENSE)

0 commit comments

Comments
 (0)