]> git.kaiwu.me - njs.git/commitdiff
Added TypeScript description for crypto module.
authorJakub Jirutka <jakub@jirutka.cz>
Fri, 9 Oct 2020 15:28:58 +0000 (17:28 +0200)
committerJakub Jirutka <jakub@jirutka.cz>
Fri, 9 Oct 2020 15:28:58 +0000 (17:28 +0200)
src/ts/crypto.d.ts [new file with mode: 0644]
test/ts/test.ts

diff --git a/src/ts/crypto.d.ts b/src/ts/crypto.d.ts
new file mode 100644 (file)
index 0000000..5895b3e
--- /dev/null
@@ -0,0 +1,76 @@
+/// <reference path="njs_core.d.ts" />
+
+declare module "crypto" {
+
+    export type Algorithm = "md5" | "sha1" | "sha256";
+
+    export type DigestEncoding = Exclude<BufferEncoding, "utf8">;
+
+    export interface Hash {
+        /**
+         * Updates the hash content with the given `data` and returns self.
+         */
+        update(data: NjsStringLike | Buffer | DataView | TypedArray): Hash;
+
+        /**
+         * Calculates the digest of all of the data passed using `hash.update()`.
+         *
+         * @example
+         *   import cr from 'crypto'
+         *   cr.createHash('sha1').update('A').update('B').digest('base64url')  // => 'BtlFlCqiamG-GMPiK_GbvKjdK10'
+         *
+         * @param encoding The encoding of the return value. If not provided, a `Buffer` object
+         *   (or a byte string before version 0.4.4) is returned.
+         * @return A calculated digest.
+         */
+        digest(): Buffer;
+        digest(encoding: DigestEncoding): string;
+    }
+
+    export interface Hmac {
+        /**
+         * Updates the HMAC content with the given `data` and returns self.
+         */
+        update(data: NjsStringLike | Buffer | DataView | TypedArray): Hmac;
+
+        /**
+         * Calculates the HMAC digest of all of the data passed using `hmac.update()`.
+         *
+         * @example
+         *   import cr from 'crypto'
+         *   cr.createHmac('sha1', 'secret.key').update('AB').digest('base64url')  // => 'Oglm93xn23_MkiaEq_e9u8zk374'
+         *
+         * @param encoding The encoding of the return value. If not provided, a `Buffer` object
+         *   (or a byte string before version 0.4.4) is returned.
+         * @return The calculated HMAC digest.
+         */
+        digest(): Buffer;
+        digest(encoding: DigestEncoding): string;
+    }
+
+    interface Crypto {
+        /**
+         * Creates and returns a `Hash` object that can be used to generate hash digests using
+         * the given `algorithm`.
+         *
+         * @param algorithm `'md5'`, `'sha1'`, or `'sha256'`
+         * @returns A `Hash` object.
+         */
+        createHash(algorithm: Algorithm): Hash;
+
+        /**
+         * Creates and returns an HMAC object that uses the given `algorithm` and secret `key`.
+         *
+         * @param algorithm `'md5'`, `'sha1'`, or `'sha256'`
+         * @param key The secret key.
+         * @returns An `HMAC` object.
+         */
+        createHmac(algorithm: Algorithm, key: NjsStringLike): Hmac;
+    }
+
+    const crypto: Crypto;
+
+    // It's exported like this because njs doesn't support named imports.
+    // TODO: Replace NjsFS with individual named exports as soon as njs supports named imports.
+    export default crypto;
+}
index 56b1d7403f67530261a3ed346235b3e7c3a7d8a3..dbb8df7b7bda051a0b29b54b50e7cf45735b42e7 100644 (file)
@@ -1,9 +1,11 @@
 /// <reference path="../../build/ts/ngx_http_js_module.d.ts" />
 /// <reference path="../../build/ts/fs.d.ts" />
 /// <reference path="../../build/ts/querystring.d.ts" />
+/// <reference path="../../build/ts/crypto.d.ts" />
 
 import fs from 'fs';
 import qs from 'querystring';
+import crypto from 'crypto';
 
 function http_module(r: NginxHTTPRequest) {
     var bs: NjsByteString;
@@ -81,6 +83,18 @@ function qs_module(str: NjsByteString) {
     s = qs.stringify(o);
 }
 
+function crypto_module(str: NjsByteString) {
+    var h;
+    var b:Buffer;
+    var s:string;
+
+    h = crypto.createHash("sha1");
+    h = h.update(str).update(Buffer.from([0]));
+    b = h.digest();
+
+    s = crypto.createHash("sha256").digest("hex");
+}
+
 function buffer(b: Buffer) {
     var s:string;