aboutsummaryrefslogtreecommitdiffstats
path: root/library/sodium-plus/lib/cryptography-key.js
diff options
context:
space:
mode:
authorMario <mario@mariovavti.com>2024-03-10 22:38:21 +0000
committerMario <mario@mariovavti.com>2024-03-10 22:38:21 +0000
commit360713c6896d18a95dd3ca541ea477bf44b98d0c (patch)
tree8acaa683b277023aa4842f25b04623ae196fa1ae /library/sodium-plus/lib/cryptography-key.js
parentee8aba3221f995b265c3196505a1c7c26b76f116 (diff)
downloadvolse-hubzilla-360713c6896d18a95dd3ca541ea477bf44b98d0c.tar.gz
volse-hubzilla-360713c6896d18a95dd3ca541ea477bf44b98d0c.tar.bz2
volse-hubzilla-360713c6896d18a95dd3ca541ea477bf44b98d0c.zip
add sodium-plus js crypto library
Diffstat (limited to 'library/sodium-plus/lib/cryptography-key.js')
-rw-r--r--library/sodium-plus/lib/cryptography-key.js80
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);
+ }
+};