aboutsummaryrefslogtreecommitdiffstats
path: root/library/sodium-plus/lib/cryptography-key.js
blob: 8aed3a7868bc4001b625904a7feca0162dfb756a (plain) (blame)
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
"use strict";

/* istanbul ignore if */
if (typeof (Buffer) === 'undefined') {
    let Buffer = require('buffer/').Buffer;
}
module.exports = class CryptographyKey {
    /**
     * Note: We use Object.defineProperty() to hide the buffer inside of the
     * CryptographyKey object to prevent accidental leaks.
     *
     * @param {Buffer} buf
     */
    constructor(buf) {
        if (!Buffer.isBuffer(buf)) {
            throw new TypeError('Argument 1 must be an instance of Buffer.');
        }
        Object.defineProperty(this, 'buffer', {
            enumerable: false,
            value: buf.slice()
        });
    }

    /**
     * @return {CryptographyKey}
     */
    static from() {
        return new CryptographyKey(Buffer.from(...arguments));
    }

    /**
     * @return {boolean}
     */
    isEd25519Key() {
        return false;
    }

    /**
     * @return {boolean}
     */
    isX25519Key() {
        return false;
    }

    /**
     * @return {boolean}
     */
    isPublicKey() {
        return false;
    }

    /**
     * @return {Number}
     */
    getLength() {
        return this.buffer.length;
    }

    /**
     * @return {Buffer}
     */
    getBuffer() {
        return this.buffer;
    }

    /**
     * @param {string} encoding
     */
    toString(encoding = 'utf-8') {
        /* istanbul ignore if */
        return this.getBuffer().toString(encoding);
    }

    /**
     * @return {Buffer}
     */
    slice() {
        return this.buffer.slice(...arguments);
    }
};