@anonymous/

BlueViolentBookmark

Nodejs

No description

fork
loading
Files
  • index.js

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.319121320718986","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Y29uc3QgX2NyeXB0byA9IHJlcXVpcmUoJ2NyeXB0bycpOwoKLy8gZW5jcnlwdC9kZWNyeXB0IGZ1bmN0aW9ucwptY2lwaGVyID0gewoKICAgIC8qKgogICAgICogRW5jcnlwdHMgdGV4dCBieSBnaXZlbiBrZXkKICAgICAqIEBwYXJhbSBTdHJpbmcgdGV4dCB0byBlbmNyeXB0CiAgICAgKiBAcGFyYW0gQnVmZmVyIG1hc3RlcmtleQogICAgICogQHJldHVybnMgU3RyaW5nIGVuY3J5cHRlZCB0ZXh0LCBiYXNlNjQgZW5jb2RlZAogICAgICovCiAgICBlbmNyeXB0OiBmdW5jdGlvbiAodGV4dCwgbWFzdGVya2V5KXsKICAgICAgICAvLyByYW5kb20gaW5pdGlhbGl6YXRpb24gdmVjdG9yCiAgICAgICAgY29uc3QgaXYgPSBfY3J5cHRvLnJhbmRvbUJ5dGVzKDE2KTsKCiAgICAgICAgLy8gcmFuZG9tIHNhbHQKICAgICAgICBjb25zdCBzYWx0ID0gX2NyeXB0by5yYW5kb21CeXRlcyg2NCk7CgogICAgICAgIC8vIGRlcml2ZSBrZXk6IDMyIGJ5dGUga2V5IGxlbmd0aCAtIGluIGFzc3VtcHRpb24gdGhlIG1hc3RlcmtleSBpcyBhIGNyeXB0b2dyYXBoaWMgYW5kIE5PVCBhIHBhc3N3b3JkIHRoZXJlIGlzIG5vIG5lZWQgZm9yCiAgICAgICAgLy8gYSBsYXJnZSBudW1iZXIgb2YgaXRlcmF0aW9ucy4gSXQgbWF5IGNhbiByZXBsYWNlZCBieSBIS0RGCiAgICAgICAgY29uc3Qga2V5ID0gX2NyeXB0by5wYmtkZjJTeW5jKG1hc3RlcmtleSwgc2FsdCwgMjE0NSwgMzIsICdzaGE1M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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const _crypto = require('crypto');

// encrypt/decrypt functions
mcipher = {

    /**
     * Encrypts text by given key
     * @param String text to encrypt
     * @param Buffer masterkey
     * @returns String encrypted text, base64 encoded
     */
    encrypt: function (text, masterkey){
        // random initialization vector
        const iv = _crypto.randomBytes(16);

        // random salt
        const salt = _crypto.randomBytes(64);

        // derive key: 32 byte key length - in assumption the masterkey is a cryptographic and NOT a password there is no need for
        // a large number of iterations. It may can replaced by HKDF
        const key = _crypto.pbkdf2Sync(masterkey, salt, 2145, 32, 'sha512');

        // AES 256 GCM Mode
        const cipher = _crypto.createCipheriv('aes-256-gcm', key, iv);

        // encrypt the given text
        const encrypted = Buffer.concat([cipher.update(text, 'utf8'), cipher.final()]);

        // extract the auth tag
        const tag = cipher.getAuthTag();

        // generate output
        return Buffer.concat([salt, iv, tag, encrypted]).toString('base64');
    },

    /**
     * Decrypts text by given key
     * @param String base64 encoded input data
     * @param Buffer masterkey
     * @returns String decrypted (original) text
     */
    decrypt: function (data, masterkey){
        // base64 decoding
        const bData = new Buffer(data, 'base64');

        // convert data to buffers
        const salt = bData.slice(0, 64);
        const iv = bData.slice(64, 80);
        const tag = bData.slice(80, 96);
        const text = bData.slice(96);

        // derive key using; 32 byte key length
        const key = _crypto.pbkdf2Sync(masterkey, salt , 2145, 32, 'sha512');

        // AES 256 GCM Mode
        const decipher = _crypto.createDecipheriv('aes-256-gcm', key, iv);
        decipher.setAuthTag(tag);

        // encrypt the given text
        const decrypted = decipher.update(text, 'binary', 'utf8') + decipher.final('utf8');

        return decrypted;
    }
};




const enc = mcipher.encrypt("text", "secret")
console.log("CIPHERTEXT = ", enc)


// Now we append "ddd" just to miss with the ciphertext and we see that it decrypts successfully to text again!
console.log("PLAINTEXT  = ", mcipher.decrypt(enc+"ddd", "secret"))
node v10.16.0