From 82e18b07a0e4a7512e9f79d85b806d952cc528f0 Mon Sep 17 00:00:00 2001 From: lancegin Date: Fri, 30 Jun 2017 13:11:18 +0800 Subject: [PATCH] add: Base32.random_gen() function --- src/base32.js | 16 ++++++++++++++-- test/b32_test.js | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/base32.js b/src/base32.js index ece722c..944862c 100644 --- a/src/base32.js +++ b/src/base32.js @@ -20,7 +20,19 @@ export class Base32 { return nibbler.b32decode(secret); } - static random_gen() { - return "static Base32.random_gen"; + /** + * Base32 generate random b32 encoded string function + * + * @param {length} + * @type {int} + * @desc the length of random b32 encoded string + * + * @return {String} + */ + static random_gen(length=16) { + let random_str = Math.random().toString(36); + random_str = nibbler.b32encode(random_str); + + return random_str.substring(0, length); } } \ No newline at end of file diff --git a/test/b32_test.js b/test/b32_test.js index 1f9a4df..0af33ee 100644 --- a/test/b32_test.js +++ b/test/b32_test.js @@ -13,7 +13,8 @@ describe('Base32 module test', function() { describe('static random_gen() function', function() { it("should print 'static Base32.random_gen'", function() { - assert.equal("static Base32.random_gen", a.random_gen()); + let length = 16; + assert(length, a.random_gen(length).length); }) }) });