From d8b1e3359a405f4282f8a4df9f5d20debd5d44fa Mon Sep 17 00:00:00 2001 From: Ismael Bejarano Date: Fri, 12 Aug 2016 00:22:59 -0300 Subject: [PATCH] Result of wrapping a session key for ECDH key --- src/index.js | 6 ++++ src/type/ecdh_symkey.js | 71 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/type/ecdh_symkey.js diff --git a/src/index.js b/src/index.js index 64c95c8c..e701837b 100644 --- a/src/index.js +++ b/src/index.js @@ -77,6 +77,12 @@ export { default as S2K } from './type/s2k'; */ export { default as Keyid } from './type/keyid'; +/** + * @see module:type/ecdh_symkey + * @name module:openpgp.EcdhSymmetricKey + */ +export { default as EcdhSymmetricKey } from './type/ecdh_symkey'; + /** * @see module:type/oid * @name module:openpgp.Oid diff --git a/src/type/ecdh_symkey.js b/src/type/ecdh_symkey.js new file mode 100644 index 00000000..ef32b68a --- /dev/null +++ b/src/type/ecdh_symkey.js @@ -0,0 +1,71 @@ +// OpenPGP.js - An OpenPGP implementation in javascript +// Copyright (C) 2015-2016 Decentral +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3.0 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +/** + * Encoded symmetric key for ECDH
+ *
+ * @requires util + * @module type/ecdh_symkey + */ + +'use strict'; + +import util from '../util.js'; + +module.exports = EcdhSymmetricKey; + +/** + * @constructor + */ +function EcdhSymmetricKey(data) { + if (typeof data === 'undefined') { + data = new Uint8Array([]); + } else if (typeof data === 'string') { + data = util.str2Uint8Array(data); + } else { + data = new Uint8Array(data); + } + this.data = data; +} + +/** + * Read an EcdhSymmetricKey from an Uint8Array + * @param {Uint8Array} input Where to read the encoded symmetric key from + * @return {Number} Number of read bytes + */ +EcdhSymmetricKey.prototype.read = function (input) { + if (input.length >= 1) + { + var length = input[0]; + if (input.length >= 1+length) + { + this.data = input.subarray(1, 1+length); + return 1+this.data.length; + } + } + throw new Error('Invalid symmetric key'); +}; + +/** + * Write an EcdhSymmetricKey as an Uint8Array + * @return {Uint8Array} An array containing the value + */ +EcdhSymmetricKey.prototype.write = function () { + return util.concatUint8Array([ + new Uint8Array([this.data.length]), + this.data]); +};