diff options
Diffstat (limited to 'library/sodium-plus/lib/cryptography-key.js')
-rw-r--r-- | library/sodium-plus/lib/cryptography-key.js | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/library/sodium-plus/lib/cryptography-key.js b/library/sodium-plus/lib/cryptography-key.js new file mode 100644 index 000000000..8aed3a786 --- /dev/null +++ b/library/sodium-plus/lib/cryptography-key.js @@ -0,0 +1,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); + } +}; |