hb-nb-tools 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/NbClient.js +8 -12
  2. package/package.json +3 -5
package/lib/NbClient.js CHANGED
@@ -8,11 +8,7 @@
8
8
  const crypto = require('crypto')
9
9
  const hbLibTools = require('hb-lib-tools')
10
10
  const { HttpClient, JsonFormatter, OptionParser } = hbLibTools
11
- let sodiumPlus
12
- try {
13
- sodiumPlus = require('sodium-plus')
14
- } catch (error) {}
15
- const { CryptographyKey, SodiumPlus } = sodiumPlus || {}
11
+ const { secretbox, randomBytes } = require('tweetnacl')
16
12
 
17
13
  class NbClient extends HttpClient {
18
14
  static get DeviceTypes () {
@@ -106,7 +102,7 @@ class NbClient extends HttpClient {
106
102
 
107
103
  constructor (params = {}) {
108
104
  const _params = {
109
- encryption: sodiumPlus == null ? 'hashedToken' : 'encryptedToken',
105
+ encryption: 'encryptedToken',
110
106
  port: 8080,
111
107
  timeout: 5
112
108
  }
@@ -198,15 +194,15 @@ class NbClient extends HttpClient {
198
194
  const date = new Date().toISOString()
199
195
  const ts = date.slice(0, 19) + 'Z'
200
196
  if (this._params.encryption === 'encryptedToken') {
201
- if (this._sodium == null) {
197
+ if (this._key == null) {
202
198
  const hash = crypto.createHash('sha256')
203
199
  hash.update(this._params.token)
204
- this._key = new CryptographyKey(hash.digest())
205
- this._sodium = await SodiumPlus.auto()
200
+ this._key = hash.digest()
206
201
  }
207
- const rnr = await this._sodium.randombytes_uniform(10000)
208
- const nonce = await this._sodium.randombytes_buf(24)
209
- const ctoken = await this._sodium.crypto_secretbox([ts, rnr].join(','), nonce, this._key)
202
+ const rnr = crypto.randomInt(10000)
203
+ const nonce = Buffer.from(randomBytes(secretbox.nonceLength))
204
+ const message = Buffer.from([ts, rnr].join(','), 'utf-8')
205
+ const ctoken = Buffer.from(secretbox(message, nonce, this._key))
210
206
  suffix = separator + 'ctoken=' + ctoken.toString('hex') + '&nonce=' + nonce.toString('hex')
211
207
  } else if (this._params.encryption === 'hashedToken') { // deprecated
212
208
  const hash = crypto.createHash('sha256')
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Homebridge NB Tools",
4
4
  "author": "Erik Baauw",
5
5
  "license": "Apache-2.0",
6
- "version": "1.1.1",
6
+ "version": "1.1.2",
7
7
  "keywords": [
8
8
  "nuki",
9
9
  "smart-lock",
@@ -17,10 +17,8 @@
17
17
  "node": "^18.16.0"
18
18
  },
19
19
  "dependencies": {
20
- "hb-lib-tools": "~1.0.9"
21
- },
22
- "optionalDependencies": {
23
- "sodium-plus": "~0.9.0"
20
+ "hb-lib-tools": "~1.0.9",
21
+ "tweetnacl": "~1.0.3"
24
22
  },
25
23
  "scripts": {
26
24
  "prepare": "standard",