5htp-core 0.2.7-2 → 0.2.7-3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "5htp-core",
3
3
  "description": "Convenient TypeScript framework designed for Performance and Productivity.",
4
- "version": "0.2.7-2",
4
+ "version": "0.2.7-3",
5
5
  "author": "Gaetan Le Gac (https://github.com/gaetanlegac)",
6
6
  "repository": "git://github.com/gaetanlegac/5htp-core.git",
7
7
  "license": "MIT",
@@ -3,7 +3,7 @@
3
3
  ----------------------------------*/
4
4
 
5
5
  // Nodejs
6
- import crypto from 'crypto';
6
+ import crypto, { Encoding } from 'crypto';
7
7
 
8
8
  // Core
9
9
  import Application, { Service } from '@server/app';
@@ -20,7 +20,11 @@ import { Forbidden } from '@common/errors';
20
20
 
21
21
  export type Config = {
22
22
  debug?: boolean,
23
+ // Initialisation vector
24
+ // Generate one here: https://www.allkeysgenerator.com/Random/Security-Encryption-Key-Generator.aspx
23
25
  iv: string,
26
+ // Define usage-specific keys
27
+ // You can also generate one via the link upper
24
28
  keys: {[keyName: string]: string}
25
29
  }
26
30
 
@@ -28,6 +32,14 @@ export type Hooks = {
28
32
 
29
33
  }
30
34
 
35
+ type TEncryptOptions = {
36
+ encoding: Encoding
37
+ }
38
+
39
+ type TDecryptOptions = {
40
+ encoding: Encoding
41
+ }
42
+
31
43
  /*----------------------------------
32
44
  - SERVICE
33
45
  ----------------------------------*/
@@ -41,27 +53,31 @@ export default class AES<TConfig extends Config = Config> extends Service<TConfi
41
53
 
42
54
  }
43
55
 
44
- public encrypt( keyName: keyof TConfig["keys"], data: any ) {
56
+ public encrypt( keyName: keyof TConfig["keys"], data: any, options: TEncryptOptions = {
57
+ encoding: 'base64url'
58
+ }) {
45
59
 
46
60
  const encKey = this.config.keys[ keyName as keyof typeof this.config.keys ];
47
61
 
48
62
  data = JSON.stringify(data);
49
63
 
50
64
  let cipher = crypto.createCipheriv('aes-256-cbc', encKey, this.config.iv);
51
- let encrypted = cipher.update(data, 'utf8', 'base64');
52
- encrypted += cipher.final('base64');
65
+ let encrypted = cipher.update(data, 'utf8', options.encoding);
66
+ encrypted += cipher.final(options.encoding);
53
67
  return encrypted;
54
68
 
55
69
  }
56
70
 
57
- public decrypt( keyName: keyof TConfig["keys"], data: string ) {
71
+ public decrypt( keyName: keyof TConfig["keys"], data: string, options: TDecryptOptions = {
72
+ encoding: 'base64url'
73
+ }) {
58
74
 
59
75
  const encKey = this.config.keys[ keyName as keyof typeof this.config.keys ];
60
76
 
61
77
  try {
62
78
 
63
79
  let decipher = crypto.createDecipheriv('aes-256-cbc', encKey, this.config.iv);
64
- let decrypted = decipher.update(data, 'base64', 'utf8');
80
+ let decrypted = decipher.update(data, options.encoding, 'utf8');
65
81
  return JSON.parse(decrypted + decipher.final('utf8'));
66
82
 
67
83
  } catch (error) {