summaryrefslogtreecommitdiff
path: root/alarm/node_modules/pronote-api/src/cipher.js
blob: 350de5b70736666626498158dacf150c863b286a (plain)
1
2
3
4
5
6
7
8
9
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const forge = require('node-forge');
const pako = require('pako');

function initCipher(session, keyModulus, keyExponent)
{
    session.aesIV = generateIV();

    session.publicKey = forge.pki.rsa.setPublicKey(
        new forge.jsbn.BigInteger(keyModulus, 16),
        new forge.jsbn.BigInteger(keyExponent, 16)
    );
}

function cipher(session, data, { key, compress, disableIV } = {})
{
    data = forge.util.encodeUtf8('' + data);
    if (compress && !session.disableCompress) {
        data = deflate(data);
    }

    const cipher = createCipher(session, key, false, disableIV);
    cipher.update(new forge.util.ByteBuffer(data));

    return cipher.finish() && cipher.output.toHex();
}

function decipher(session, data, { compress, scrambled, key, asBytes } = {})
{
    const cipher = createCipher(session, key, true);
    cipher.update(new forge.util.ByteBuffer(forge.util.hexToBytes(data)));

    let result = cipher.finish() && cipher.output.bytes();
    if (compress && !session.disableCompress) {
        result = inflate(result);
    }

    result = forge.util.decodeUtf8(result);

    if (scrambled) {
        const unscrambled = new Array(result.length);
        for (let i = 0; i < result.length; i += 1) {
            if (i % 2 === 0) {
                unscrambled.push(result.charAt(i));
            }
        }

        return unscrambled.join('');
    }

    if (asBytes) {
        const buffer = new forge.util.ByteBuffer();
        const split = result.split(',');

        for (let i = 0; i < split.length; i++) {
            buffer.putInt(parseInt(split[i]));
        }

        return buffer;
    }

    return result;
}

function createCipher(session, key, decipher, disableIV = false)
{
    if (!key) {
        key = session.aesKey || new forge.util.ByteBuffer();
    }

    const cipher = forge.cipher[decipher ? 'createDecipher' : 'createCipher']('AES-CBC', md5(key));
    const iv = disableIV ? new forge.util.ByteBuffer() : md5(session.aesIV);

    cipher.start({ iv });

    return cipher;
}

function md5(buffer)
{
    return forge.md.md5.create().update(buffer.bytes()).digest();
}

function deflate(data)
{
    return pako.deflateRaw(new forge.util.ByteBuffer(data).toHex(), { level: 6, to: 'string' });
}

function inflate(data)
{
    return pako.inflateRaw(data, { to: 'string' });
}

function generateIV()
{
    return new forge.util.ByteBuffer(forge.random.generate(16));
}

function getUUID(session, iv)
{
    return forge.util.encode64(session.publicKey.encrypt(iv.bytes()), 64);
}

function getLoginKey(username, password, scramble, fromCas)
{
    const hash = forge.md.sha256.create().update(scramble || '').update(forge.util.encodeUtf8(password)).digest();
    const key = (fromCas ? '' : username.toLowerCase()) + hash.toHex().toUpperCase();

    return new forge.util.ByteBuffer(forge.util.encodeUtf8(key));
}

module.exports = {
    initCipher,

    cipher,
    decipher,
    getUUID,
    getLoginKey
};