Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

Alorel / personal-build-tools / 1844

23 Dec 2019 - 1:17 coverage increased (+0.3%) to 78.737%
1844

Pull #180

travis-ci-com

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
chore(package): update lockfile yarn.lock
Pull Request #180: Update nyc to the latest version 🚀

382 of 627 branches covered (60.93%)

Branch coverage included in aggregate %.

1488 of 1748 relevant lines covered (85.13%)

37.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.74
/src/lib/Crypt.ts
1
import * as crypto from 'crypto';
2
import {isObject} from 'lodash';
8×
3

8×
4
const enum Conf {
8×
5
  IV_RANDOM_BYTES = 16,
6
  SALT_RANDOM_BYTES = 64,
28×
7
  KEY_ITERATIONS = 16384,
28×
8
  KEY_LEN = 32,
28×
9
  CIPHER = 'aes-256-gcm',
28×
10
  HASH = 'sha512',
28×
11
  IV_START = 64,
28×
12
  TAG_START = 80,
28×
13
  TEXT_START = 96
28×
14
}
28×
15

28×
16
export interface Encrypted {
17
  __encrypted: string;
18
}
UNCOV
19

!
20
export class Crypt {
22×
21
  public static decrypt(encrypted: string, password: string): string {
2×
22
    const bData = Buffer.from(encrypted, 'base64');
24×
23

24×
24
    const salt = bData.slice(0, Conf.IV_START);
24×
25
    const iv = bData.slice(Conf.IV_START, Conf.TAG_START);
24×
26
    const tag = bData.slice(Conf.TAG_START, Conf.TEXT_START);
24×
27
    const text = bData.slice(Conf.TEXT_START);
24×
28

24×
29
    const key = crypto.pbkdf2Sync(password, salt, Conf.KEY_ITERATIONS, Conf.KEY_LEN, Conf.HASH);
4×
30
    const decipher = crypto.createDecipheriv(Conf.CIPHER, key, iv);
31
    decipher.setAuthTag(tag);
34×
32

33
    const out = decipher.update(<any>text, 'binary', 'utf8') + decipher.final('utf8');
34

2×
35
    if (!out) {
36
      throw new Error('Unable to decrypt');
UNCOV
37
    }
!
UNCOV
38

!
39
    return out;
!
UNCOV
40
  }
!
UNCOV
41

!
UNCOV
42
  public static decryptVar(v: Encrypted, password: string): string {
!
UNCOV
43
    return Crypt.decrypt(v.__encrypted, password);
!
44
  }
45

UNCOV
46
  public static encrypt(text: string, password: string): string {
!
47
    const iv = crypto.randomBytes(Conf.IV_RANDOM_BYTES);
48
    const salt = crypto.randomBytes(Conf.SALT_RANDOM_BYTES);
49
    const key = crypto.pbkdf2Sync(password, salt, Conf.KEY_ITERATIONS, Conf.KEY_LEN, Conf.HASH);
Branches [[2, 2]] missed. 2×
50
    const cipher = crypto.createCipheriv(Conf.CIPHER, key, iv);
51
    const encrypted = Buffer.concat([cipher.update(text, 'utf8'), cipher.final()]);
52
    const authTag = cipher.getAuthTag();
6×
53

54
    return Buffer.concat([salt, iv, authTag, encrypted]).toString('base64');
55
  }
56

57
  public static encryptVar(text: string, password: string): Encrypted {
58
    return {__encrypted: Crypt.encrypt(text, password)};
59
  }
60

61
  public static isEncrypted(v: any): v is Encrypted {
62
    return !!v && isObject(v) && typeof (<any>v).__encrypted === 'string';
63
  }
64
}
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2022 Coveralls, Inc