Mercurial > njs
changeset 1545:11da5c960017
Added TypeScript description for crypto module.
| author | Jakub Jirutka <jakub@jirutka.cz> |
|---|---|
| date | Fri, 09 Oct 2020 17:28:58 +0200 |
| parents | e7fb6e22acb7 |
| children | 3d8f6b9548c0 |
| files | src/ts/crypto.d.ts test/ts/test.ts |
| diffstat | 2 files changed, 90 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ts/crypto.d.ts Fri Oct 09 17:28:58 2020 +0200 @@ -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; +}
--- a/test/ts/test.ts Fri Oct 09 17:05:47 2020 +0200 +++ b/test/ts/test.ts Fri Oct 09 17:28:58 2020 +0200 @@ -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 @@ 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;
