diff options
author | Marius David Wieschollek <passwords.public@mdns.eu> | 2024-01-04 17:25:56 +0300 |
---|---|---|
committer | Marius David Wieschollek <passwords.public@mdns.eu> | 2024-01-04 17:25:56 +0300 |
commit | 278e3db0ed742221caffc63ae0e75f1918f700c3 (patch) | |
tree | f96881d442e9f20afd362961dc21660886a5a024 | |
parent | 501ebf83509f88ba93aece92ed8d73b13d2c62af (diff) |
Add hashes api support to new client
Signed-off-by: Marius David Wieschollek <passwords.public@mdns.eu>
-rw-r--r-- | src/Classes/SimpleApi.js | 2 | ||||
-rw-r--r-- | src/Exception/Services/InvalidRangeError.js | 16 | ||||
-rw-r--r-- | src/Services/HashService.js | 36 |
3 files changed, 50 insertions, 4 deletions
diff --git a/src/Classes/SimpleApi.js b/src/Classes/SimpleApi.js index 407e22c..4271e12 100644 --- a/src/Classes/SimpleApi.js +++ b/src/Classes/SimpleApi.js @@ -639,7 +639,7 @@ export default class SimpleApi { /** * - * @returns {Promise} + * @returns {Promise<String[]>} */ getHashes(range) { return this._sendRequest('service.hashes', {range}); diff --git a/src/Exception/Services/InvalidRangeError.js b/src/Exception/Services/InvalidRangeError.js new file mode 100644 index 0000000..39f6214 --- /dev/null +++ b/src/Exception/Services/InvalidRangeError.js @@ -0,0 +1,16 @@ +export default class InvalidRangeError extends Error { + + /** + * @returns {String} + */ + get name() { + return 'InvalidRangeError'; + } + + /** + * + */ + constructor() { + super('The given range is invalid'); + } +}
\ No newline at end of file diff --git a/src/Services/HashService.js b/src/Services/HashService.js index 7942ae7..ee5f894 100644 --- a/src/Services/HashService.js +++ b/src/Services/HashService.js @@ -42,13 +42,43 @@ export default class HashService { return 'Argon2'; } - constructor(classLoader) { - this._ready = classLoader.getClass('state.boolean', false); + /** + * + * @param {BasicPasswordsClient} client + */ + constructor(client) { + this._ready = client.getClass('state.boolean', false); + this._client = client; + this._breachedHashesCache = {}; sodium.ready.then(() => {this._ready.set(true);}); } /** + * Retrieves breached hashes for the given SHA-1 hash range. + * + * @param {String} range The range to retrieve breached hashes for. At least 5, maximum 40 characters + * @throws {InvalidRangeError} If the length of the range is less than 5 or greater than 40. + * @returns {Promise<String[]>} A promise that resolves to an array of breached hashes. + */ + async getBreachedHashes(range) { + if(range.length < 5 || range.length > 40) throw this._client.getClass('exception.service.range'); + + if(this._breachedHashesCache.hasOwnProperty(range)) { + return this._breachedHashesCache[range]; + } + + let response = await this._client.getRequest() + .setPath('1.0/service/hashes') + .setData({range}) + .send(); + + this._breachedHashesCache[range] = response.getData(); + + return response.getData(); + } + + /** * Generate a hash of the given value with the given algorithm * * @param {String} value @@ -60,7 +90,7 @@ export default class HashService { if([this.HASH_SHA_1, this.HASH_SHA_256, this.HASH_SHA_384, this.HASH_SHA_512].indexOf(algorithm) !== -1) { return await this._makeShaHash(value, algorithm); - } else if(algorithm.substr(0, 7) === this.HASH_BLAKE2B) { + } else if(algorithm.substring(0, 7) === this.HASH_BLAKE2B) { return this._makeBlake2bHash(algorithm, value); } else if(algorithm === this.HASH_ARGON2) { return sodium.crypto_pwhash_str(value, sodium.crypto_pwhash_OPSLIMIT_MIN, sodium.crypto_pwhash_MEMLIMIT_MIN); |