index.d.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. declare namespace CryptoES {
  2. namespace lib {
  3. /**
  4. * Base class for inheritance.
  5. */
  6. export class Base {
  7. /**
  8. * Extends this object and runs the init method.
  9. * Arguments to create() will be passed to init().
  10. *
  11. * @return {Object} The new object.
  12. *
  13. * @static
  14. *
  15. * @example
  16. *
  17. * var instance = MyType.create();
  18. */
  19. static create(...args: Array<any>): Base;
  20. constructor(...args: Array<any>);
  21. /**
  22. * Copies properties into this object.
  23. *
  24. * @param {Object} properties The properties to mix in.
  25. *
  26. * @example
  27. *
  28. * MyType.mixIn({
  29. * field: 'value'
  30. * });
  31. */
  32. mixIn(properties?: object): Base;
  33. /**
  34. * Creates a copy of this object.
  35. *
  36. * @return {Object} The clone.
  37. *
  38. * @example
  39. *
  40. * var clone = instance.clone();
  41. */
  42. clone(): Base
  43. }
  44. /**
  45. * An array of 32-bit words.
  46. *
  47. * @property {Array} words The array of 32-bit words.
  48. * @property {number} sigBytes The number of significant bytes in this word array.
  49. */
  50. export class WordArray extends Base {
  51. words: Array<number>;
  52. sigBytes: number;
  53. /**
  54. * Initializes a newly created word array.
  55. *
  56. * @param {Array} words (Optional) An array of 32-bit words.
  57. * @param {number} sigBytes (Optional) The number of significant bytes in the words.
  58. *
  59. * @example
  60. *
  61. * var wordArray = CryptoJS.lib.WordArray.create();
  62. * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607]);
  63. * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607], 6);
  64. */
  65. static create(
  66. words?: Array<number> | ArrayBuffer | Uint8Array | Int8Array | Uint8ClampedArray
  67. | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array,
  68. sigBytes?: number,
  69. ): WordArray;
  70. constructor(
  71. words?: Array<number> | ArrayBuffer | Uint8Array | Int8Array | Uint8ClampedArray
  72. | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array,
  73. sigBytes?: number,
  74. );
  75. /**
  76. * Creates a word array filled with random bytes.
  77. *
  78. * @param {number} nBytes The number of random bytes to generate.
  79. *
  80. * @return {WordArray} The random word array.
  81. *
  82. * @static
  83. *
  84. * @example
  85. *
  86. * var wordArray = CryptoJS.lib.WordArray.random(16);
  87. */
  88. static random(nBytes?: number): WordArray;
  89. /**
  90. * Converts this word array to a string.
  91. *
  92. * @param {Encoder} encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
  93. *
  94. * @return {string} The stringified word array.
  95. *
  96. * @example
  97. *
  98. * var string = wordArray + '';
  99. * var string = wordArray.toString();
  100. * var string = wordArray.toString(CryptoJS.enc.Utf8);
  101. */
  102. toString(encoder?: enc.Encoder): string;
  103. /**
  104. * Concatenates a word array to this word array.
  105. *
  106. * @param {WordArray} wordArray The word array to append.
  107. *
  108. * @return {WordArray} This word array.
  109. *
  110. * @example
  111. *
  112. * wordArray1.concat(wordArray2);
  113. */
  114. concat(wordArray?: WordArray): WordArray;
  115. /**
  116. * Removes insignificant bits.
  117. *
  118. * @example
  119. *
  120. * wordArray.clamp();
  121. */
  122. clamp(): void;
  123. /**
  124. * Creates a copy of this word array.
  125. *
  126. * @return {WordArray} The clone.
  127. *
  128. * @example
  129. *
  130. * var clone = wordArray.clone();
  131. */
  132. clone(): WordArray;
  133. }
  134. /**
  135. * Abstract buffered block algorithm template.
  136. *
  137. * The property blockSize must be implemented in a concrete subtype.
  138. *
  139. * @property {number} _minBufferSize
  140. *
  141. * The number of blocks that should be kept unprocessed in the buffer. Default: 0
  142. */
  143. export class BufferedBlockAlgorithm extends Base {
  144. _minBufferSize: number;
  145. static create(): BufferedBlockAlgorithm;
  146. constructor();
  147. /**
  148. * Resets this block algorithm's data buffer to its initial state.
  149. *
  150. * @example
  151. *
  152. * bufferedBlockAlgorithm.reset();
  153. */
  154. reset(): void;
  155. /**
  156. * Adds new data to this block algorithm's buffer.
  157. *
  158. * @param {WordArray|string} data
  159. *
  160. * The data to append. Strings are converted to a WordArray using UTF-8.
  161. *
  162. * @example
  163. *
  164. * bufferedBlockAlgorithm._append('data');
  165. * bufferedBlockAlgorithm._append(wordArray);
  166. */
  167. _append(data?: WordArray | string): void;
  168. /**
  169. * Processes available data blocks.
  170. *
  171. * This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
  172. *
  173. * @param {boolean} doFlush Whether all blocks and partial blocks should be processed.
  174. *
  175. * @return {WordArray} The processed data.
  176. *
  177. * @example
  178. *
  179. * var processedData = bufferedBlockAlgorithm._process();
  180. * var processedData = bufferedBlockAlgorithm._process(!!'flush');
  181. */
  182. _process(doFlush?: boolean): WordArray;
  183. /**
  184. * Creates a copy of this object.
  185. *
  186. * @return {Object} The clone.
  187. *
  188. * @example
  189. *
  190. * var clone = bufferedBlockAlgorithm.clone();
  191. */
  192. clone(): BufferedBlockAlgorithm;
  193. }
  194. interface HasherCfg {
  195. // SHA3
  196. outputLength?: number
  197. }
  198. /**
  199. * Abstract hasher template.
  200. *
  201. * @property {number} blockSize
  202. *
  203. * The number of 32-bit words this hasher operates on. Default: 16 (512 bits)
  204. */
  205. export class Hasher extends BufferedBlockAlgorithm {
  206. blockSize: number;
  207. static create(cfg?: HasherCfg): Hasher;
  208. constructor(cfg?: HasherCfg);
  209. /**
  210. * Creates a shortcut function to a hasher's object interface.
  211. *
  212. * @param {Hasher} SubHasher The hasher to create a helper for.
  213. *
  214. * @return {Function} The shortcut function.
  215. *
  216. * @static
  217. *
  218. * @example
  219. *
  220. * var SHA256 = CryptoJS.lib.Hasher._createHelper(CryptoJS.algo.SHA256);
  221. */
  222. static _createHelper(SubHasher?: Hasher): HashFn;
  223. /**
  224. * Creates a shortcut function to the HMAC's object interface.
  225. *
  226. * @param {Hasher} SubHasher The hasher to use in this HMAC helper.
  227. *
  228. * @return {Function} The shortcut function.
  229. *
  230. * @static
  231. *
  232. * @example
  233. *
  234. * var HmacSHA256 = CryptoJS.lib.Hasher._createHmacHelper(CryptoJS.algo.SHA256);
  235. */
  236. static _createHmacHelper(SubHasher?: Hasher): HMACHashFn;
  237. /**
  238. * Resets this hasher to its initial state.
  239. *
  240. * @example
  241. *
  242. * hasher.reset();
  243. */
  244. reset(): void;
  245. /**
  246. * Updates this hasher with a message.
  247. *
  248. * @param {WordArray|string} messageUpdate The message to append.
  249. *
  250. * @return {Hasher} This hasher.
  251. *
  252. * @example
  253. *
  254. * hasher.update('message');
  255. * hasher.update(wordArray);
  256. */
  257. update(messageUpdate?: WordArray | string): Hasher;
  258. /**
  259. * Finalizes the hash computation.
  260. * Note that the finalize operation is effectively a destructive, read-once operation.
  261. *
  262. * @param {WordArray|string} messageUpdate (Optional) A final message update.
  263. *
  264. * @return {WordArray} The hash.
  265. *
  266. * @example
  267. *
  268. * var hash = hasher.finalize();
  269. * var hash = hasher.finalize('message');
  270. * var hash = hasher.finalize(wordArray);
  271. */
  272. finalize(messageUpdate?: WordArray | string): WordArray;
  273. }
  274. interface CipherCfg {
  275. // Cipher
  276. iv?: lib.WordArray;
  277. mode?: Function;
  278. padding?: pad.Padding;
  279. // SerializableCipher
  280. format?: format.Format;
  281. // PasswordBasedCipher
  282. kdf?: kdf.Kdf;
  283. }
  284. /**
  285. * Abstract base cipher template.
  286. *
  287. * @property {number} keySize This cipher's key size. Default: 4 (128 bits)
  288. * @property {number} ivSize This cipher's IV size. Default: 4 (128 bits)
  289. * @property {number} _ENC_XFORM_MODE A constant representing encryption mode.
  290. * @property {number} _DEC_XFORM_MODE A constant representing decryption mode.
  291. */
  292. export class Cipher extends BufferedBlockAlgorithm {
  293. static keySize: number;
  294. static ivSize: number;
  295. static _ENC_XFORM_MODE: number;
  296. static _DEC_XFORM_MODE: number;
  297. /**
  298. * Initializes a newly created cipher.
  299. *
  300. * @param {number} xformMode Either the encryption or decryption transormation mode constant.
  301. * @param {WordArray} key The key.
  302. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  303. *
  304. * @example
  305. *
  306. * const cipher = CryptoJS.algo.AES.create(
  307. * CryptoJS.algo.AES._ENC_XFORM_MODE, keyWordArray, { iv: ivWordArray }
  308. * );
  309. */
  310. static create(xformMode?: number, key?: WordArray, cfg?: CipherCfg): Cipher;
  311. constructor(xformMode?: number, key?: WordArray, cfg?: CipherCfg);
  312. /**
  313. * Creates this cipher in encryption mode.
  314. *
  315. * @param {WordArray} key The key.
  316. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  317. *
  318. * @return {Cipher} A cipher instance.
  319. *
  320. * @static
  321. *
  322. * @example
  323. *
  324. * const cipher = CryptoJS.algo.AES.createEncryptor(keyWordArray, { iv: ivWordArray });
  325. */
  326. static createEncryptor(key?: WordArray, cfg?: CipherCfg): Cipher;
  327. /**
  328. * Creates this cipher in decryption mode.
  329. *
  330. * @param {WordArray} key The key.
  331. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  332. *
  333. * @return {Cipher} A cipher instance.
  334. *
  335. * @static
  336. *
  337. * @example
  338. *
  339. * const cipher = CryptoJS.algo.AES.createDecryptor(keyWordArray, { iv: ivWordArray });
  340. */
  341. static createDecryptor(key?: WordArray, cfg?: CipherCfg): Cipher;
  342. /**
  343. * Creates shortcut functions to a cipher's object interface.
  344. *
  345. * @param {Cipher} cipher The cipher to create a helper for.
  346. *
  347. * @return {Object} An object with encrypt and decrypt shortcut functions.
  348. *
  349. * @static
  350. *
  351. * @example
  352. *
  353. * const AES = CryptoJS.lib.Cipher._createHelper(CryptoJS.algo.AES);
  354. */
  355. static _createHelper(SubCipher?: Function): CipherObj;
  356. /**
  357. * Resets this cipher to its initial state.
  358. *
  359. * @example
  360. *
  361. * cipher.reset();
  362. */
  363. reset(): void;
  364. /**
  365. * Adds data to be encrypted or decrypted.
  366. *
  367. * @param {WordArray|string} dataUpdate The data to encrypt or decrypt.
  368. *
  369. * @return {WordArray} The data after processing.
  370. *
  371. * @example
  372. *
  373. * const encrypted = cipher.process('data');
  374. * const encrypted = cipher.process(wordArray);
  375. */
  376. process(dataUpdate?: WordArray | string): WordArray;
  377. /**
  378. * Finalizes the encryption or decryption process.
  379. * Note that the finalize operation is effectively a destructive, read-once operation.
  380. *
  381. * @param {WordArray|string} dataUpdate The final data to encrypt or decrypt.
  382. *
  383. * @return {WordArray} The data after final processing.
  384. *
  385. * @example
  386. *
  387. * const encrypted = cipher.finalize();
  388. * const encrypted = cipher.finalize('data');
  389. * const encrypted = cipher.finalize(wordArray);
  390. */
  391. finalize(dataUpdate?: WordArray | string): WordArray;
  392. }
  393. /**
  394. * Abstract base stream cipher template.
  395. *
  396. * @property {number} blockSize
  397. *
  398. * The number of 32-bit words this cipher operates on. Default: 1 (32 bits)
  399. */
  400. export class StreamCipher extends Cipher {
  401. blockSize: number;
  402. static create(...args: Array<any>): StreamCipher;
  403. constructor(...args: Array<any>);
  404. _doFinalize(): WordArray;
  405. }
  406. /**
  407. * Abstract base block cipher mode template.
  408. */
  409. export class BlockCipherMode extends Base {
  410. /**
  411. * Initializes a newly created mode.
  412. *
  413. * @param {Cipher} cipher A block cipher instance.
  414. * @param {Array} iv The IV words.
  415. *
  416. * @example
  417. *
  418. * const mode = CryptoJS.mode.CBC.Encryptor.create(cipher, iv.words);
  419. */
  420. static create(cipher?: Cipher, iv?: Array<number>): BlockCipherMode;
  421. constructor(cipher?: Cipher, iv?: Array<number>);
  422. /**
  423. * Creates this mode for encryption.
  424. *
  425. * @param {Cipher} cipher A block cipher instance.
  426. * @param {Array} iv The IV words.
  427. *
  428. * @static
  429. *
  430. * @example
  431. *
  432. * const mode = CryptoJS.mode.CBC.createEncryptor(cipher, iv.words);
  433. */
  434. static createEncryptor(cipher?: Cipher, iv?: Array<number>): BlockCipherMode;
  435. /**
  436. * Creates this mode for decryption.
  437. *
  438. * @param {Cipher} cipher A block cipher instance.
  439. * @param {Array} iv The IV words.
  440. *
  441. * @static
  442. *
  443. * @example
  444. *
  445. * const mode = CryptoJS.mode.CBC.createDecryptor(cipher, iv.words);
  446. */
  447. static createDecryptor(cipher?: Cipher, iv?: Array<number>): BlockCipherMode;
  448. }
  449. /**
  450. * Abstract base block cipher template.
  451. *
  452. * @property {number} blockSize
  453. *
  454. * The number of 32-bit words this cipher operates on. Default: 4 (128 bits)
  455. */
  456. export class BlockCipher extends Cipher {
  457. static create(xformMode?: number, key?: WordArray, cfg?: CipherCfg): BlockCipher;
  458. }
  459. interface CipherParamsCfg {
  460. ciphertext?: WordArray;
  461. key?: WordArray;
  462. iv?: WordArray;
  463. salt?: WordArray;
  464. algorithm?: Cipher;
  465. mode?: Function;
  466. padding?: pad.Padding;
  467. blockSize?: number;
  468. formatter?: format.Format;
  469. }
  470. /**
  471. * A collection of cipher parameters.
  472. *
  473. * @property {WordArray} ciphertext The raw ciphertext.
  474. * @property {WordArray} key The key to this ciphertext.
  475. * @property {WordArray} iv The IV used in the ciphering operation.
  476. * @property {WordArray} salt The salt used with a key derivation function.
  477. * @property {Cipher} algorithm The cipher algorithm.
  478. * @property {Mode} mode The block mode used in the ciphering operation.
  479. * @property {Padding} padding The padding scheme used in the ciphering operation.
  480. * @property {number} blockSize The block size of the cipher.
  481. * @property {Format} formatter
  482. * The default formatting strategy to convert this cipher params object to a string.
  483. */
  484. export class CipherParams extends Base {
  485. ciphertext: WordArray;
  486. key: WordArray;
  487. iv: WordArray;
  488. salt: WordArray;
  489. algorithm: Cipher;
  490. mode: BlockCipherMode;
  491. padding: pad.Padding;
  492. blockSize: number;
  493. formatter: format.Format;
  494. /**
  495. * Initializes a newly created cipher params object.
  496. *
  497. * @param {Object} cipherParams An object with any of the possible cipher parameters.
  498. *
  499. * @example
  500. *
  501. * var cipherParams = CryptoJS.lib.CipherParams.create({
  502. * ciphertext: ciphertextWordArray,
  503. * key: keyWordArray,
  504. * iv: ivWordArray,
  505. * salt: saltWordArray,
  506. * algorithm: CryptoJS.algo.AES,
  507. * mode: CryptoJS.mode.CBC,
  508. * padding: CryptoJS.pad.PKCS7,
  509. * blockSize: 4,
  510. * formatter: CryptoJS.format.OpenSSL
  511. * });
  512. */
  513. static create(cipherParams?: CipherParamsCfg): CipherParams;
  514. constructor(cipherParams?: CipherParamsCfg);
  515. /**
  516. * Converts this cipher params object to a string.
  517. *
  518. * @param {Format} formatter (Optional) The formatting strategy to use.
  519. *
  520. * @return {string} The stringified cipher params.
  521. *
  522. * @throws Error If neither the formatter nor the default formatter is set.
  523. *
  524. * @example
  525. *
  526. * var string = cipherParams + '';
  527. * var string = cipherParams.toString();
  528. * var string = cipherParams.toString(CryptoJS.format.OpenSSL);
  529. */
  530. toString(formatter?: format.Format): string;
  531. }
  532. /**
  533. * A cipher wrapper that returns ciphertext as a serializable cipher params object.
  534. */
  535. export class SerializableCipher extends Base {
  536. cfg: object;
  537. /**
  538. * Encrypts a message.
  539. *
  540. * @param {Cipher} cipher The cipher algorithm to use.
  541. * @param {WordArray|string} message The message to encrypt.
  542. * @param {WordArray} key The key.
  543. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  544. *
  545. * @return {CipherParams} A cipher params object.
  546. *
  547. * @static
  548. *
  549. * @example
  550. *
  551. * var ciphertextParams = CryptoJS.lib.SerializableCipher
  552. * .encrypt(CryptoJS.algo.AES, message, key);
  553. * var ciphertextParams = CryptoJS.lib.SerializableCipher
  554. * .encrypt(CryptoJS.algo.AES, message, key, { iv: iv });
  555. * var ciphertextParams = CryptoJS.lib.SerializableCipher
  556. * .encrypt(CryptoJS.algo.AES, message, key, { iv: iv, format: CryptoJS.format.OpenSSL });
  557. */
  558. static encrypt(cipher?: Function, message?: WordArray | string, key?: WordArray | string, cfg?: CipherCfg): CipherParams;
  559. /**
  560. * Decrypts serialized ciphertext.
  561. *
  562. * @param {Cipher} cipher The cipher algorithm to use.
  563. * @param {CipherParams|string} ciphertext The ciphertext to decrypt.
  564. * @param {WordArray} key The key.
  565. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  566. *
  567. * @return {WordArray} The plaintext.
  568. *
  569. * @static
  570. *
  571. * @example
  572. *
  573. * var plaintext = CryptoJS.lib.SerializableCipher
  574. * .decrypt(CryptoJS.algo.AES, formattedCiphertext, key,
  575. * { iv: iv, format: CryptoJS.format.OpenSSL });
  576. * var plaintext = CryptoJS.lib.SerializableCipher
  577. * .decrypt(CryptoJS.algo.AES, ciphertextParams, key,
  578. * { iv: iv, format: CryptoJS.format.OpenSSL });
  579. */
  580. static decrypt(cipher?: Function, ciphertext?: CipherParams | string, key?: WordArray | string, cfg?: CipherCfg): WordArray;
  581. /**
  582. * Converts serialized ciphertext to CipherParams,
  583. * else assumed CipherParams already and returns ciphertext unchanged.
  584. *
  585. * @param {CipherParams|string} ciphertext The ciphertext.
  586. * @param {Formatter} format The formatting strategy to use to parse serialized ciphertext.
  587. *
  588. * @return {CipherParams} The unserialized ciphertext.
  589. *
  590. * @static
  591. *
  592. * @example
  593. *
  594. * var ciphertextParams = CryptoJS.lib.SerializableCipher
  595. * ._parse(ciphertextStringOrParams, format);
  596. */
  597. static _parse(ciphertext?: CipherParams | string, format?: format.Format): CipherParams;
  598. }
  599. /**
  600. * A serializable cipher wrapper that derives the key from a password,
  601. * and returns ciphertext as a serializable cipher params object.
  602. */
  603. export class PasswordBasedCipher extends SerializableCipher {
  604. cfg: object;
  605. /**
  606. * Encrypts a message using a password.
  607. *
  608. * @param {Cipher} cipher The cipher algorithm to use.
  609. * @param {WordArray|string} message The message to encrypt.
  610. * @param {string} password The password.
  611. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  612. *
  613. * @return {CipherParams} A cipher params object.
  614. *
  615. * @static
  616. *
  617. * @example
  618. *
  619. * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher
  620. * .encrypt(CryptoJS.algo.AES, message, 'password');
  621. * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher
  622. * .encrypt(CryptoJS.algo.AES, message, 'password', { format: CryptoJS.format.OpenSSL });
  623. */
  624. static encrypt(cipher?: Function, message?: WordArray | string, password?: string, cfg?: CipherCfg): CipherParams;
  625. /**
  626. * Decrypts serialized ciphertext using a password.
  627. *
  628. * @param {Cipher} cipher The cipher algorithm to use.
  629. * @param {CipherParams|string} ciphertext The ciphertext to decrypt.
  630. * @param {string} password The password.
  631. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  632. *
  633. * @return {WordArray} The plaintext.
  634. *
  635. * @static
  636. *
  637. * @example
  638. *
  639. * var plaintext = CryptoJS.lib.PasswordBasedCipher
  640. * .decrypt(CryptoJS.algo.AES, formattedCiphertext, 'password',
  641. * { format: CryptoJS.format.OpenSSL });
  642. * var plaintext = CryptoJS.lib.PasswordBasedCipher
  643. * .decrypt(CryptoJS.algo.AES, ciphertextParams, 'password',
  644. * { format: CryptoJS.format.OpenSSL });
  645. */
  646. static decrypt(cipher?: Function, ciphertext?: CipherParams | string, password?: string, cfg?: CipherCfg): WordArray;
  647. }
  648. }
  649. namespace x64 {
  650. /**
  651. * A 64-bit word.
  652. */
  653. export class Word extends lib.Base {
  654. high: number;
  655. low: number;
  656. /**
  657. * Initializes a newly created 64-bit word.
  658. *
  659. * @param {number} high The high 32 bits.
  660. * @param {number} low The low 32 bits.
  661. *
  662. * @example
  663. *
  664. * var x64Word = CryptoJS.x64.Word.create(0x00010203, 0x04050607);
  665. */
  666. static create(high?: number, low?: number): Word;
  667. constructor(high?: number, low?: number);
  668. }
  669. /**
  670. * An array of 64-bit words.
  671. *
  672. * @property {Array} words The array of CryptoJS.x64.Word objects.
  673. * @property {number} sigBytes The number of significant bytes in this word array.
  674. */
  675. export class WordArray extends lib.Base {
  676. words: Array<Word>;
  677. sigBytes: number;
  678. /**
  679. * Initializes a newly created word array.
  680. *
  681. * @param {Array} words (Optional) An array of CryptoJS.x64.Word objects.
  682. * @param {number} sigBytes (Optional) The number of significant bytes in the words.
  683. *
  684. * @example
  685. *
  686. * var wordArray = CryptoJS.x64.WordArray.create();
  687. *
  688. * var wordArray = CryptoJS.x64.WordArray.create([
  689. * CryptoJS.x64.Word.create(0x00010203, 0x04050607),
  690. * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
  691. * ]);
  692. *
  693. * var wordArray = CryptoJS.x64.WordArray.create([
  694. * CryptoJS.x64.Word.create(0x00010203, 0x04050607),
  695. * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
  696. * ], 10);
  697. */
  698. static create(words?: Array<Word>, sigBytes?: number): WordArray;
  699. constructor(words?: Array<Word>, sigBytes?: number);
  700. /**
  701. * Converts this 64-bit word array to a 32-bit word array.
  702. *
  703. * @return {CryptoJS.lib.WordArray} This word array's data as a 32-bit word array.
  704. *
  705. * @example
  706. *
  707. * var x32WordArray = x64WordArray.toX32();
  708. */
  709. toX32(): lib.WordArray;
  710. /**
  711. * Creates a copy of this word array.
  712. *
  713. * @return {X64WordArray} The clone.
  714. *
  715. * @example
  716. *
  717. * var clone = x64WordArray.clone();
  718. */
  719. clone(): WordArray;
  720. }
  721. }
  722. namespace enc {
  723. interface Encoder {
  724. stringify(wordArray?: lib.WordArray): string;
  725. parse(str?: string): lib.WordArray;
  726. }
  727. export const Hex: Encoder;
  728. export const Latin1: Encoder;
  729. export const Utf8: Encoder;
  730. export const Utf16: Encoder;
  731. export const Utf16BE: Encoder;
  732. export const Utf16LE: Encoder;
  733. export const Base64: Encoder;
  734. }
  735. namespace algo {
  736. /**
  737. * HMAC algorithm.
  738. */
  739. export class HMAC extends lib.Base {
  740. /**
  741. * Initializes a newly created HMAC.
  742. *
  743. * @param {Hasher} SubHasher The hash algorithm to use.
  744. * @param {WordArray|string} key The secret key.
  745. *
  746. * @example
  747. *
  748. * var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
  749. */
  750. static create(SubHasher?: lib.Hasher, key?: lib.WordArray | string): HMAC;
  751. constructor(SubHasher?: lib.Hasher, key?: lib.WordArray | string);
  752. /**
  753. * Resets this HMAC to its initial state.
  754. *
  755. * @example
  756. *
  757. * hmacHasher.reset();
  758. */
  759. reset(): void;
  760. /**
  761. * Updates this HMAC with a message.
  762. *
  763. * @param {WordArray|string} messageUpdate The message to append.
  764. *
  765. * @return {HMAC} This HMAC instance.
  766. *
  767. * @example
  768. *
  769. * hmacHasher.update('message');
  770. * hmacHasher.update(wordArray);
  771. */
  772. update(messageUpdate?: lib.WordArray | string): HMAC;
  773. /**
  774. * Finalizes the HMAC computation.
  775. * Note that the finalize operation is effectively a destructive, read-once operation.
  776. *
  777. * @param {WordArray|string} messageUpdate (Optional) A final message update.
  778. *
  779. * @return {WordArray} The HMAC.
  780. *
  781. * @example
  782. *
  783. * var hmac = hmacHasher.finalize();
  784. * var hmac = hmacHasher.finalize('message');
  785. * var hmac = hmacHasher.finalize(wordArray);
  786. */
  787. finalize(messageUpdate?: lib.WordArray | string): lib.WordArray;
  788. }
  789. export class MD5 extends lib.Hasher {}
  790. export class SHA1 extends lib.Hasher {}
  791. export class SHA224 extends lib.Hasher {}
  792. export class SHA256 extends lib.Hasher {}
  793. export class SHA384 extends lib.Hasher {}
  794. export class SHA512 extends lib.Hasher {}
  795. export class SHA3 extends lib.Hasher {}
  796. export class RIPEMD160 extends lib.Hasher {}
  797. export class PBKDF2 extends lib.Base {
  798. static create(cfg?: KDFCfg): PBKDF2;
  799. constructor(cfg?: KDFCfg);
  800. compute(password?: lib.WordArray | string, salt?: lib.WordArray | string): lib.WordArray;
  801. }
  802. export class EvpKDF extends lib.Base {
  803. static create(cfg?: KDFCfg): EvpKDF;
  804. constructor(cfg?: KDFCfg);
  805. compute(password?: lib.WordArray | string, salt?: lib.WordArray | string): lib.WordArray;
  806. }
  807. export class AES extends lib.BlockCipher {}
  808. export class DES extends lib.BlockCipher {}
  809. export class TripleDES extends lib.BlockCipher {}
  810. export class Rabbit extends lib.StreamCipher {}
  811. export class RabbitLegacy extends lib.StreamCipher {}
  812. export class RC4 extends lib.StreamCipher {}
  813. export class RC4Drop extends lib.StreamCipher {}
  814. }
  815. namespace mode {
  816. export class CBC extends lib.BlockCipherMode {}
  817. export class CFB extends lib.BlockCipherMode {}
  818. export class CTR extends lib.BlockCipherMode {}
  819. export class CTRGladman extends lib.BlockCipherMode {}
  820. export class ECB extends lib.BlockCipherMode {}
  821. export class OFB extends lib.BlockCipherMode {}
  822. }
  823. namespace pad {
  824. interface Padding {
  825. pad(data?: lib.WordArray, blockSize?: number): void;
  826. unpad(data?: lib.WordArray): void;
  827. }
  828. export const Pkcs7: Padding;
  829. export const AnsiX923: Padding;
  830. export const Iso10126: Padding;
  831. export const Iso97971: Padding;
  832. export const NoPadding: Padding;
  833. export const ZeroPadding: Padding;
  834. }
  835. namespace format {
  836. interface Format {
  837. stringify(cipherParams?: lib.CipherParams): string;
  838. parse(str?: string): lib.CipherParams;
  839. }
  840. export const OpenSSL: Format;
  841. export const Hex: Format;
  842. }
  843. namespace kdf {
  844. interface Kdf {
  845. execute(password?: string, keySize?: number, ivSize?: number, salt?: lib.WordArray | string): lib.CipherParams;
  846. }
  847. export const OpenSSL: Kdf;
  848. }
  849. type HashFn = (message?: lib.WordArray | string, cfg?: lib.HasherCfg) => lib.WordArray;
  850. type HMACHashFn = (message?: lib.WordArray | string, key?: lib.WordArray | string) => lib.WordArray;
  851. export const MD5: HashFn;
  852. export const HmacMD5: HMACHashFn;
  853. export const SHA1: HashFn;
  854. export const HmacSHA1: HMACHashFn;
  855. export const SHA224: HashFn;
  856. export const HmacSHA224: HMACHashFn;
  857. export const SHA256: HashFn;
  858. export const HmacSHA256: HMACHashFn;
  859. export const SHA384: HashFn;
  860. export const HmacSHA384: HMACHashFn;
  861. export const SHA512: HashFn;
  862. export const HmacSHA512: HMACHashFn;
  863. export const SHA3: HashFn;
  864. export const HmacSHA3: HMACHashFn;
  865. export const RIPEMD160: HashFn;
  866. export const HmacRIPEMD160: HMACHashFn;
  867. interface KDFCfg {
  868. // EvpKDF
  869. keySize?: number;
  870. hasher?: lib.Hasher;
  871. iterations?: number;
  872. }
  873. type KDFFn = (password?: lib.WordArray | string, salt?: lib.WordArray | string, cfg?: KDFCfg) => lib.WordArray;
  874. export const PBKDF2: KDFFn;
  875. export const EvpKDF: KDFFn;
  876. interface CipherObj {
  877. encrypt(message?: lib.WordArray | string, key?: lib.WordArray | string, cfg?: lib.CipherCfg): lib.CipherParams;
  878. decrypt(ciphertext?: lib.CipherParams | lib.CipherParamsCfg | string, key?: lib.WordArray | string, cfg?: lib.CipherCfg): lib.WordArray;
  879. }
  880. export const AES: CipherObj;
  881. export const DES: CipherObj;
  882. export const TripleDES: CipherObj;
  883. export const Rabbit: CipherObj;
  884. export const RabbitLegacy: CipherObj;
  885. export const RC4: CipherObj;
  886. export const RC4Drop: CipherObj;
  887. }
  888. export default CryptoES;