format-hex.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. ;(function (root, factory, undef) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"), require("./cipher-core"));
  5. } else if (typeof define === "function" && define.amd) {
  6. // AMD
  7. define(["./core", "./cipher-core"], factory);
  8. } else {
  9. // Global (browser)
  10. factory(root.CryptoJS);
  11. }
  12. }(this, function (CryptoJS) {
  13. (function (undefined) {
  14. // Shortcuts
  15. var C = CryptoJS;
  16. var C_lib = C.lib;
  17. var CipherParams = C_lib.CipherParams;
  18. var C_enc = C.enc;
  19. var Hex = C_enc.Hex;
  20. var C_format = C.format;
  21. var HexFormatter = C_format.Hex = {
  22. /**
  23. * Converts the ciphertext of a cipher params object to a hexadecimally encoded string.
  24. *
  25. * @param {CipherParams} cipherParams The cipher params object.
  26. *
  27. * @return {string} The hexadecimally encoded string.
  28. *
  29. * @static
  30. *
  31. * @example
  32. *
  33. * var hexString = CryptoJS.format.Hex.stringify(cipherParams);
  34. */
  35. stringify: function (cipherParams) {
  36. return cipherParams.ciphertext.toString(Hex);
  37. },
  38. /**
  39. * Converts a hexadecimally encoded ciphertext string to a cipher params object.
  40. *
  41. * @param {string} input The hexadecimally encoded string.
  42. *
  43. * @return {CipherParams} The cipher params object.
  44. *
  45. * @static
  46. *
  47. * @example
  48. *
  49. * var cipherParams = CryptoJS.format.Hex.parse(hexString);
  50. */
  51. parse: function (input) {
  52. var ciphertext = Hex.parse(input);
  53. return CipherParams.create({ciphertext: ciphertext});
  54. }
  55. };
  56. }());
  57. return CryptoJS.format.Hex;
  58. }));