From: Jakub Jirutka Date: Fri, 9 Oct 2020 15:28:58 +0000 (+0200) Subject: Added TypeScript description for crypto module. X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=ef46a05f2e2888a1b07433640579db25655d1376;p=njs.git Added TypeScript description for crypto module. --- diff --git a/src/ts/crypto.d.ts b/src/ts/crypto.d.ts new file mode 100644 index 00000000..5895b3e7 --- /dev/null +++ b/src/ts/crypto.d.ts @@ -0,0 +1,76 @@ +/// + +declare module "crypto" { + + export type Algorithm = "md5" | "sha1" | "sha256"; + + export type DigestEncoding = Exclude; + + 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; +} diff --git a/test/ts/test.ts b/test/ts/test.ts index 56b1d740..dbb8df7b 100644 --- a/test/ts/test.ts +++ b/test/ts/test.ts @@ -1,9 +1,11 @@ /// /// /// +/// 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;