ops.mjs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /* eslint no-console: 0 */
  2. /**
  3. * nodeApi.js
  4. *
  5. * Test node api operations
  6. *
  7. * Aim of these tests is to ensure each arg type is
  8. * handled correctly by the wrapper.
  9. *
  10. * Generally just checking operations that use external dependencies to ensure
  11. * it behaves as expected in Node.
  12. *
  13. * @author d98762625 [d98762625@gmail.com]
  14. * @copyright Crown Copyright 2018
  15. * @license Apache-2.0
  16. */
  17. import assert from "assert";
  18. import it from "../assertionHandler";
  19. import {
  20. addLineNumbers,
  21. adler32Checksum,
  22. AESDecrypt,
  23. affineCipherDecode,
  24. affineCipherEncode,
  25. bifidCipherEncode,
  26. bitShiftRight,
  27. cartesianProduct,
  28. CSSMinify,
  29. toBase64,
  30. toHex,
  31. } from "../../../src/node/index";
  32. import chef from "../../../src/node/index";
  33. import TestRegister from "../../TestRegister";
  34. TestRegister.addApiTests([
  35. it("ADD: toggleString argument", () => {
  36. const result = chef.ADD("sample input", {
  37. key: {
  38. string: "some key",
  39. option: "Hex"
  40. }
  41. });
  42. assert.equal(result.toString(), "aO[^ZS\u000eW\\^cb");
  43. }),
  44. it("addLineNumbers: No arguments", () => {
  45. const result = addLineNumbers("sample input");
  46. assert.equal(result.toString(), "1 sample input");
  47. }),
  48. it("adler32Checksum: No args", () => {
  49. const result = adler32Checksum("sample input");
  50. assert.equal(result.toString(), "1f2304d3");
  51. }),
  52. it("AES decrypt: toggleString and option", () => {
  53. const result = AESDecrypt("812c34ae6af353244a63c6ce23b7c34286b60be28ea4645523d4494700e7", {
  54. key: {
  55. string: "some longer key1",
  56. option: "utf8",
  57. },
  58. iv: {
  59. string: "some iv",
  60. option: "utf8",
  61. },
  62. mode: "OFB",
  63. });
  64. assert.equal(result.toString(), "a slightly longer sampleinput?");
  65. }),
  66. it("AffineCipherDecode: number input", () => {
  67. const result = affineCipherDecode("some input", {
  68. a: 7,
  69. b: 4
  70. });
  71. assert.strictEqual(result.toString(), "cuqa ifjgr");
  72. }),
  73. it("affineCipherEncode: number input", () => {
  74. const result = affineCipherEncode("some input", {
  75. a: 11,
  76. b: 6
  77. });
  78. assert.strictEqual(result.toString(), "weiy qtpsh");
  79. }),
  80. it("analyzeHash", () => {
  81. const result = chef.analyseHash(chef.MD5("some input"));
  82. const expected = `Hash length: 32
  83. Byte length: 16
  84. Bit length: 128
  85. Based on the length, this hash could have been generated by one of the following hashing functions:
  86. MD5
  87. MD4
  88. MD2
  89. HAVAL-128
  90. RIPEMD-128
  91. Snefru
  92. Tiger-128`;
  93. assert.strictEqual(result.toString(), expected);
  94. }),
  95. it("AND", () => {
  96. const result = chef.AND("Scot-free", {
  97. key: "Raining Cats and Dogs",
  98. });
  99. assert.strictEqual(result.toString(), ".\"M$(D E.");
  100. }),
  101. it("atBash Cipher", () => {
  102. const result = chef.atbashCipher("Happy as a Clam");
  103. assert.strictEqual(result.toString(), "Szkkb zh z Xozn");
  104. }),
  105. it("Bcrypt", () => {
  106. const result = chef.bcrypt("Put a Sock In It");
  107. assert.strictEqual(result.toString(), "$2a$10$ODeP1.6fMsb.ENk2ngPUCO7qTGVPyHA9TqDVcyupyed8FjsiF65L6");
  108. }),
  109. it("bcryptCompare", () => {
  110. const result = chef.bcryptCompare("Put a Sock In It", {
  111. hash: "$2a$10$2rT4a3XnIecBsd1H33dMTuyYE1HJ1n9F.V2rjQtAH73rh1qvOf/ae",
  112. });
  113. assert.strictEqual(result.toString(), "Match: Put a Sock In It");
  114. }),
  115. it("Bcrypt Parse", () => {
  116. const result = chef.bcryptParse("$2a$10$ODeP1.6fMsb.ENk2ngPUCO7qTGVPyHA9TqDVcyupyed8FjsiF65L6");
  117. const expected = `Rounds: 10
  118. Salt: $2a$10$ODeP1.6fMsb.ENk2ngPUCO
  119. Password hash: 7qTGVPyHA9TqDVcyupyed8FjsiF65L6
  120. Full hash: $2a$10$ODeP1.6fMsb.ENk2ngPUCO7qTGVPyHA9TqDVcyupyed8FjsiF65L6`;
  121. assert.strictEqual(result.toString(), expected);
  122. }),
  123. it("bifid cipher decode", () => {
  124. const result = chef.bifidCipherDecode("Vhef Qnte Ke Xfhz Mxon Bmgf", {
  125. keyword: "Alpha",
  126. });
  127. assert.strictEqual(result.toString(), "What Goes Up Must Come Down");
  128. }),
  129. it("bifid cipher encode: string option", () => {
  130. const result = bifidCipherEncode("some input", {
  131. keyword: "mykeyword",
  132. });
  133. assert.strictEqual(result.toString(), "nmhs zmsdo");
  134. }),
  135. it("bit shift left", () => {
  136. const result = chef.bitShiftLeft("Keep Your Eyes Peeled");
  137. assert.strictEqual(result.toString(), ".ÊÊà@²Þêä@.òÊæ@ ÊÊØÊÈ");
  138. }),
  139. it("bitShiftRight: number and option", () => {
  140. const result = bitShiftRight("some bits to shift", {
  141. type: "Arithmetic shift",
  142. amount: 1,
  143. });
  144. assert.strictEqual(result.toString(), "9762\u001014:9\u0010:7\u00109443:");
  145. }),
  146. it("Blowfish encrypt", () => {
  147. const result = chef.blowfishEncrypt("Fool's Gold", {
  148. key: {
  149. string: "One",
  150. option: "hex",
  151. },
  152. iv: {
  153. string: "Two",
  154. option: "utf8"
  155. }
  156. });
  157. assert.strictEqual(result.toString(), "8999b513bf2ff064b2977dea7e05f1b5");
  158. }),
  159. it("Blowfish decrypt", () => {
  160. const result = chef.blowfishDecrypt("8999b513bf2ff064b2977dea7e05f1b5", {
  161. key: {
  162. string: "One",
  163. option: "hex",
  164. },
  165. iv: {
  166. string: "Two",
  167. option: "utf8",
  168. }
  169. });
  170. assert.strictEqual(result.toString(), "Fool's Gold");
  171. }),
  172. it("BSON Deserialise", () => {
  173. const result = chef.BSONDeserialise("....phrase.....Mouth-watering..");
  174. assert.strictEqual(result.toString(), "{\"phrase\": \"Mouth-watering\"}");
  175. }),
  176. it("BSON Serialise", () => {
  177. const result = chef.BSONSerialise({"phrase": "Mouth-watering"});
  178. assert.strictEqual(result.toString(), " ....phrase.....Mouth-watering..");
  179. }),
  180. it("Bzip2 Decompress", () => {
  181. const result = chef.bzip2Decompress(chef.fromBase64("QlpoOTFBWSZTWUdQlt0AAAIVgEAAAQAmJAwAIAAxBkxA0A2pTL6U2CozxdyRThQkEdQlt0A="));
  182. assert.strictEqual(result.toString(), "Fit as a Fiddle");
  183. }),
  184. it("cartesianProduct: binary string", () => {
  185. const result = cartesianProduct("1:2\\n\\n3:4", {
  186. itemDelimiter: ":",
  187. });
  188. assert.strictEqual(result.toString(), "(1,3):(1,4):(2,3):(2,4)");
  189. }),
  190. it("Change IP format", () => {
  191. const result = chef.changeIPFormat("172.20.23.54", {
  192. inputFormat: "Dotted Decimal",
  193. outputFormat: "Hex",
  194. });
  195. assert.strictEqual(result.toString(), "ac141736");
  196. }),
  197. it("Chi square", () => {
  198. const result = chef.chiSquare("Burst Your Bubble");
  199. assert.strictEqual(result.toString(), "433.55399816176475");
  200. }),
  201. it("Compare CTPH Hashes", () => {
  202. const result = chef.compareCTPHHashes("1234\n3456");
  203. assert.strictEqual(result.toString(), "0");
  204. }),
  205. it("Compare SSDEEPHashes", () => {
  206. const result = chef.compareCTPHHashes("1234\n3456");
  207. assert.strictEqual(result.toString(), "0");
  208. }),
  209. it("Convert area", () => {
  210. const result = chef.convertArea("12345", {
  211. inputUnits: "Square metre (sq m)",
  212. outputUnits: "Isle of Wight"
  213. });
  214. assert.strictEqual(result.toString(), "0.00003248684210526316");
  215. }),
  216. it("Convert data units", () => {
  217. const result = chef.convertDataUnits("12345", {
  218. inputUnits: "Bits (b)",
  219. outputUnits: "Kilobytes (KB)",
  220. });
  221. assert.strictEqual(result.toString(), "1.543125");
  222. }),
  223. it("Convert distance", () => {
  224. const result = chef.convertDistance("1234567", {
  225. inputUnits: "Nanometres (nm)",
  226. outputUnits: "Furlongs (fur)",
  227. });
  228. assert.strictEqual(result.toString(), "0.00000613699494949495");
  229. }),
  230. it("Convert mass", () => {
  231. const result = chef.convertMass("123", {
  232. inputUnits: "Earth mass (M⊕)",
  233. outputUnits: "Great Pyramid of Giza (6,000,000 tonnes)",
  234. });
  235. assert.strictEqual(result.toString(), "122429895000000000");
  236. }),
  237. it("Convert speed", () => {
  238. const result = chef.convertSpeed("123", {
  239. inputUnits: "Lunar escape velocity",
  240. outputUnits: "Jet airliner cruising speed",
  241. });
  242. assert.strictEqual(result.toString(), "1168.5");
  243. }),
  244. it("Count occurrences", () => {
  245. const result = chef.countOccurrences("Talk the Talk", {
  246. searchString: {
  247. string: "Tal",
  248. option: "Simple string",
  249. }
  250. });
  251. assert.strictEqual(result.toString(), "2");
  252. }),
  253. it("CRC16 Checksum", () => {
  254. const result = chef.CRC16Checksum("Rain on Your Parade");
  255. assert.strictEqual(result.toString(), "db1c");
  256. }),
  257. it("CRC32 Checksum", () => {
  258. const result = chef.CRC32Checksum("Rain on Your Parade");
  259. assert.strictEqual(result.toString(), "e902f76c");
  260. }),
  261. it("CSS Beautify", () => {
  262. const result = chef.CSSBeautify("header {color:black;padding:3rem;}");
  263. const expected = `header {
  264. \\tcolor:black;
  265. \\tpadding:3rem;
  266. }
  267. `;
  268. assert.strictEqual(result.toString(), expected);
  269. }),
  270. it("CSS minify: boolean", () => {
  271. const input = `header {
  272. // comment
  273. width: 100%;
  274. color: white;
  275. }`;
  276. const result = CSSMinify(input, {
  277. preserveComments: true,
  278. });
  279. assert.strictEqual(result.toString(), "header {// comment width: 100%;color: white;}");
  280. }),
  281. it("CSS Selector", () => {
  282. const result = chef.CSSSelector("<html><header><h1>Hello</h1></header></html>", {
  283. cssSelector: "h1",
  284. });
  285. assert.strictEqual(result.toString(), "<h1>Hello</h1>");
  286. }),
  287. it("CTPH", () => {
  288. const result = chef.CTPH("If You Can't Stand the Heat, Get Out of the Kitchen");
  289. assert.strictEqual(result.toString(), "A:+EgFgBKAA0V0UFfClEs6:+Qk0gUFse");
  290. }),
  291. it("Decode NetBIOS Name", () => {
  292. assert.strictEqual(chef.decodeNetBIOSName("EBGMGMCAEHHCGFGFGLCAFEGPCAENGFCA").toString(), "All Greek To Me");
  293. }),
  294. it("Decode text", () => {
  295. const encoded = chef.encodeText("Ugly Duckling", {
  296. encoding: "UTF16LE (1200)",
  297. });
  298. const result = chef.decodeText(encoded, {
  299. encoding: "UTF16LE (1200)",
  300. });
  301. assert.strictEqual(result.toString(), "Ugly Duckling");
  302. }),
  303. it("Derive EVP Key", () => {
  304. const result = chef.deriveEVPKey("", {
  305. passphrase: {
  306. string: "46 6c 65 61 20 4d 61 72 6b 65 74",
  307. option: "Hex",
  308. },
  309. salt: {
  310. string: "Market",
  311. option: "Hex",
  312. },
  313. });
  314. assert.strictEqual(result.toString(), "7c21a9f5063a4d62fb1050068245c181");
  315. }),
  316. it("Derive PBKDF2 Key", () => {
  317. const result = chef.derivePBKDF2Key("", {
  318. passphrase: {
  319. string: "Jack of All Trades Master of None",
  320. option: "utf8",
  321. },
  322. keySize: 256,
  323. iterations: 2,
  324. hashingFunction: "md5",
  325. salt: {
  326. string: "fruit",
  327. option: "utf8"
  328. }
  329. });
  330. assert.strictEqual(result.toString(), "728a885b209e8b19cbd7430ca32608ff09190f7ccb7ded204e1d4c50f87c47bf");
  331. }),
  332. it("DES Decrypt", () => {
  333. const result = chef.DESDecrypt("713081c66db781c323965ba8f166fd8c230c3bb48504a913", {
  334. key: {
  335. string: "onetwoth",
  336. option: "utf8",
  337. },
  338. iv: {
  339. string: "threetwo",
  340. option: "Hex",
  341. },
  342. mode: "ECB",
  343. });
  344. assert.strictEqual(result.toString(), "Put a Sock In It");
  345. }),
  346. it("DES Encrypt", () => {
  347. const result = chef.DESEncrypt("Put a Sock In It", {
  348. key: {
  349. string: "onetwoth",
  350. option: "utf8",
  351. },
  352. iv: {
  353. string: "threetwo",
  354. option: "Hex",
  355. },
  356. mode: "ECB",
  357. });
  358. assert.strictEqual(result.toString(), "713081c66db781c323965ba8f166fd8c230c3bb48504a913");
  359. }),
  360. it("Diff", () => {
  361. const result = chef.diff("one two\\n\\none two three");
  362. assert.strictEqual(result.toString(), "one two three");
  363. }),
  364. it("Disassemble x86", () => {
  365. const result = chef.disassembleX86(chef.toBase64("one two three"));
  366. const expected = `0000000000000000 0000 ADD BYTE PTR [RAX],AL\r
  367. 0000000000000002 0B250000000B OR ESP,DWORD PTR [000000000B000008]\r
  368. `;
  369. assert.strictEqual(result.toString(), expected);
  370. }),
  371. it("Divide", () => {
  372. assert.strictEqual(chef.divide("4\n7").toString(), "0.57142857142857142857");
  373. }),
  374. it("Drop bytes", () => {
  375. assert.strictEqual(chef.dropBytes("There's No I in Team").toString(), "'s No I in Team");
  376. }),
  377. it("Entropy", () => {
  378. const result = chef.entropy("Ride Him, Cowboy!");
  379. assert.strictEqual(result.toString(), "3.734521664779752");
  380. }),
  381. it("Escape string", () => {
  382. const result = chef.escapeString("Know the Ropes", {
  383. escapeLevel: "Everything",
  384. JSONCompatible: false,
  385. ES6Compatible: true,
  386. uppercaseHex: true,
  387. });
  388. assert.strictEqual(result.toString(), "\\x4B\\x6E\\x6F\\x77\\x20\\x74\\x68\\x65\\x20\\x52\\x6F\\x70\\x65\\x73");
  389. }),
  390. it("Escape unicode characters", () => {
  391. assert.strictEqual(chef.escapeUnicodeCharacters("σου").toString(), "\\u03C3\\u03BF\\u03C5");
  392. }),
  393. it("Expand alphabet range", () => {
  394. assert.strictEqual(
  395. chef.expandAlphabetRange("Fight Fire With Fire", {delimiter: "t"}).toString(),
  396. "Ftitgthttt tFtitrtet tWtitttht tFtitrte");
  397. }),
  398. it("Extract dates", () => {
  399. assert.strictEqual(chef.extractDates("Don't Look a Gift Horse In The Mouth 01/02/1992").toString(), "01/02/1992\n");
  400. }),
  401. it("Filter", () => {
  402. const result = chef.filter(
  403. `I Smell a Rat
  404. Every Cloud Has a Silver Lining
  405. Top Drawer`, {
  406. regex: "Every",
  407. });
  408. const expected = "Every Cloud Has a Silver Lining";
  409. assert.strictEqual(result.toString(), expected);
  410. }),
  411. it("Find / Replace", () => {
  412. assert.strictEqual(
  413. chef.findReplace(
  414. "Curiosity Killed The Cat",
  415. {
  416. find: {
  417. string: "l",
  418. option: "Regex",
  419. },
  420. replace: "s",
  421. }).toString(),
  422. "Curiosity Kissed The Cat");
  423. }),
  424. it("Fletcher8 Checksum", () => {
  425. assert.strictEqual(chef.fletcher8Checksum("Keep Your Eyes Peeled").toString(), "48");
  426. }),
  427. it("Format MAC addresses", () => {
  428. const result = chef.formatMACAddresses("00-01-02-03-04-05");
  429. const expected = `000102030405
  430. 000102030405
  431. 00-01-02-03-04-05
  432. 00-01-02-03-04-05
  433. 00:01:02:03:04:05
  434. 00:01:02:03:04:05
  435. `;
  436. assert.strictEqual(result.toString(), expected);
  437. }),
  438. it("Frequency distribution", () => {
  439. const result = chef.frequencyDistribution("Don't Count Your Chickens Before They Hatch");
  440. const expected = "{\"dataLength\":43,\"percentages\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,13.953488372093023,0,0,0,0,0,0,2.3255813953488373,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2.3255813953488373,4.651162790697675,2.3255813953488373,0,0,0,2.3255813953488373,0,0,0,0,0,0,0,0,0,0,0,2.3255813953488373,0,0,0,0,2.3255813953488373,0,0,0,0,0,0,0,2.3255813953488373,0,4.651162790697675,0,9.30232558139535,2.3255813953488373,0,6.976744186046512,2.3255813953488373,0,2.3255813953488373,0,0,6.976744186046512,9.30232558139535,0,0,4.651162790697675,2.3255813953488373,6.976744186046512,4.651162790697675,0,0,0,2.3255813953488373,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\"distribution\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,2,0,4,1,0,3,1,0,1,0,0,3,4,0,0,2,1,3,2,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\"bytesRepresented\":22}";
  441. assert.strictEqual(result.toString(), expected);
  442. }),
  443. it("From base", () => {
  444. assert.strictEqual(chef.fromBase("11", {radix: 13}).toString(), "14");
  445. }),
  446. it("From BCD", () => {
  447. assert.strictEqual(chef.fromBCD("1143", { inputFormat: "Raw", scheme: "7 4 2 1"}).toString(), "31313433");
  448. }),
  449. it("From binary", () => {
  450. assert.strictEqual(chef.fromBinary("010101011100101101011010").toString(), "UËZ");
  451. }),
  452. it("From Charcode", () => {
  453. assert.strictEqual(chef.fromCharcode("4c 6f 6e 67 20 49 6e 20 54 68 65 20 54 6f 6f 74 68 0a").toString(), "Long In The Tooth\n");
  454. }),
  455. it("From decimal", () => {
  456. assert.strictEqual(chef.fromDecimal("72 101 108 108 111").toString(), "Hello");
  457. }),
  458. it("From hex", () => {
  459. assert.strictEqual(chef.fromHex("52 69 6e 67 20 41 6e 79 20 42 65 6c 6c 73 3f").toString(), "Ring Any Bells?");
  460. }),
  461. it("From hex content", () => {
  462. assert.strictEqual(chef.fromHexContent("foo|3d|bar").toString(), "foo=bar");
  463. }),
  464. it("To and From hex dump", () => {
  465. assert.strictEqual(chef.fromHexdump(chef.toHexdump("Elephant in the Room")).toString(), "Elephant in the Room");
  466. }),
  467. it("From HTML entity", () => {
  468. assert.strictEqual(chef.fromHTMLEntity("&amp;").toString(), "&");
  469. }),
  470. it("To and From morse code", () => {
  471. assert.strictEqual(chef.fromMorseCode(chef.toMorseCode("Put a Sock In It")).toString(), "PUT A SOCK IN IT");
  472. }),
  473. it("From octal", () => {
  474. assert.strictEqual(chef.fromOctal("113 156 157 167 40 164 150 145 40 122 157 160 145 163").toString(), "Know the Ropes");
  475. }),
  476. it("To, From punycode", () => {
  477. assert.strictEqual(chef.fromPunycode(chef.toPunycode("münchen")).toString(), "münchen");
  478. }),
  479. it("From unix timestamp", () => {
  480. assert.strictEqual(chef.fromUNIXTimestamp("978346800").toString(), "Mon 1 January 2001 11:00:00 UTC");
  481. }),
  482. it("Generate HOTP", () => {
  483. const result = chef.generateHOTP("Cut The Mustard", {
  484. name: "colonel",
  485. });
  486. const expected = `URI: otpauth://hotp/colonel?secret=IN2XIICUNBSSATLVON2GC4TE
  487. Password: 034148`;
  488. assert.strictEqual(result.toString(), expected);
  489. }),
  490. it("Generate PGP Key Pair", () => {
  491. const result = chef.generatePGPKeyPair("Back To the Drawing Board", {
  492. keyType: "ECC-256",
  493. });
  494. const expected = `-----BEGIN PGP PRIVATE KEY BLOCK-----
  495. Version: Keybase OpenPGP v2.0.77
  496. Comment: https://keybase.io/crypto
  497. xYgEW3KciQEBAK96Lx9G0WZiw1yhC35IogdumoxEJXsLdAVIjmskXeAfABEBAAEA
  498. AP4wK+OZu3AqojwtRoyIK1pHKU93OAuam1iaLCOGCwCckQCA5PjU0aLNZqy/eKyX
  499. T3rpKQCAxDDT5hHGAUfFPUu73KWABwB/WKpeUp7KwurMSbYVhgr1TijszQDCVAQT
  500. AQoAHgUCW3KciQIbLwMLCQcDFQoIAh4BAheAAxYCAQIZAQAKCRD0VeyUMgmpz3OE
  501. AP9qsnhhoK85Tnu6VKwKm1iMiJAssDQnFztDaMmmVdrN/MeIBFtynIkBAQDDhjIw
  502. fxOprqVMYLk6aC45JyPAA2POzu0Zb/rx0tKeBwARAQABAAD/XAr66oiP9ZORHiT0
  503. XZH4m7vwZt7AHuq4pYtVlMQXk60AgPw2Mno/wStvE/SBa9R7AtsAgMZ2BkJjvNPZ
  504. 9YA6cl4lW0UAgI1+kJVLZ5VR9fPENfJR80EtncKDBBgBCgAPBQJbcpyJBQkPCZwA
  505. AhsuAEgJEPRV7JQyCanPPSAEGQEKAAYFAltynIkACgkQrewgWMQZ/b2blwD/dbwh
  506. /3F9xv+YGAwq8i1mzzswg4qBct6LoSIjGglULT9RIQD/cYd31YfKrEnbSBWD5PLi
  507. zcSsxtBGKphwXiPAlQJ1Q5DHiARbcpyJAQEAs8V418lf1T74PpAwdBTiViEUX9jB
  508. e+ZrAEVaq5nu1C8AEQEAAQAA/iPWhS23hnRTllealR4/H5OofZRwxvIQrxAJp6z1
  509. ICRxAIDayRpCAbK5EC3DzRU2z4VpAIDSWYSs9inI1VTfamJPMWHXAIC3aaukzCP4
  510. GEGeFobX/thnKhnCgwQYAQoADwUCW3KciQUJA8JnAAIbLgBICRD0VeyUMgmpzz0g
  511. BBkBCgAGBQJbcpyJAAoJEB4jzL1hmQIXamUA/0c1M6BSqVtxNowPcOAXKYIxMca1
  512. VFcRWolHnZqdZQ7k/J8A/3HvNLRS3p1HvjQEfXl/qKoRRn843Py09ptDHh+xpGKh
  513. =d+Vp
  514. -----END PGP PRIVATE KEY BLOCK-----
  515. -----BEGIN PGP PUBLIC KEY BLOCK-----
  516. Version: Keybase OpenPGP v2.0.77
  517. Comment: https://keybase.io/crypto
  518. xi0EW3KciQEBAK96Lx9G0WZiw1yhC35IogdumoxEJXsLdAVIjmskXeAfABEBAAHN
  519. AMJUBBMBCgAeBQJbcpyJAhsvAwsJBwMVCggCHgECF4ADFgIBAhkBAAoJEPRV7JQy
  520. CanPc4QA/2qyeGGgrzlOe7pUrAqbWIyIkCywNCcXO0NoyaZV2s38zi0EW3KciQEB
  521. AMOGMjB/E6mupUxguTpoLjknI8ADY87O7Rlv+vHS0p4HABEBAAHCgwQYAQoADwUC
  522. W3KciQUJDwmcAAIbLgBICRD0VeyUMgmpzz0gBBkBCgAGBQJbcpyJAAoJEK3sIFjE
  523. Gf29m5cA/3W8If9xfcb/mBgMKvItZs87MIOKgXLei6EiIxoJVC0/USEA/3GHd9WH
  524. yqxJ20gVg+Ty4s3ErMbQRiqYcF4jwJUCdUOQzi0EW3KciQEBALPFeNfJX9U++D6Q
  525. MHQU4lYhFF/YwXvmawBFWquZ7tQvABEBAAHCgwQYAQoADwUCW3KciQUJA8JnAAIb
  526. LgBICRD0VeyUMgmpzz0gBBkBCgAGBQJbcpyJAAoJEB4jzL1hmQIXamUA/0c1M6BS
  527. qVtxNowPcOAXKYIxMca1VFcRWolHnZqdZQ7k/J8A/3HvNLRS3p1HvjQEfXl/qKoR
  528. Rn843Py09ptDHh+xpGKh
  529. =ySwG
  530. -----END PGP PUBLIC KEY BLOCK-----`;
  531. assert.strictEqual(result.toString(), expected);
  532. }),
  533. it("Generate UUID", () => {
  534. const result = chef.generateUUID();
  535. assert.ok(result.toString());
  536. assert.strictEqual(result.toString().length, 36);
  537. }),
  538. it("Gzip, Gunzip", () => {
  539. assert.strictEqual(chef.gunzip(chef.gzip("Down To The Wire")).toString(), "Down To The Wire");
  540. }),
  541. it("Hex to Object Identifier", () => {
  542. assert.strictEqual(
  543. chef.hexToObjectIdentifier(chef.toHex("You Can't Teach an Old Dog New Tricks")).toString(),
  544. "2.9.111.117.32.67.97.110.39.116.32.84.101.97.99.104.32.97.110.32.79.108.100.32.68.111.103.32.78.101.119.32.84.114.105.99.107.115");
  545. }),
  546. it("Hex to PEM", () => {
  547. const result = chef.hexToPEM(chef.toHex("Yada Yada"));
  548. const expected = `-----BEGIN CERTIFICATE-----\r
  549. WWFkYSBZYWRh\r
  550. -----END CERTIFICATE-----\r\n`;
  551. assert.strictEqual(result.toString(), expected);
  552. }),
  553. it("HMAC", () => {
  554. assert.strictEqual(chef.HMAC("On Cloud Nine", {key: "idea"}).toString(), "b128b48ec0d6b0f1a27220c396d0f3e5");
  555. }),
  556. it("Javascript beautify", () => {
  557. const result = chef.javaScriptBeautify("const b = 2, g = 7;const fun = (a) => {return a*a};");
  558. const expected = `const b = 2, g = 7;
  559. const fun = a => {
  560. return a * a;'
  561. };`;
  562. assert.strictEqual(result.toString(), expected);
  563. }),
  564. it("JavaScript Minify", () => {
  565. const result = chef.javaScriptMinify(`const b = 2, g = 7;
  566. const fun = a => {
  567. return a * a;
  568. };`);
  569. const expected = "const b = 2, g = 7;const fun = (a) => {return a*a};";
  570. assert.strictEqual(result.toString(), expected);
  571. }),
  572. it("JavaScript Parse", () => {
  573. const result = chef.javaScriptParser("const b = 2;");
  574. const expected = `{
  575. "type": "Program",
  576. "body": [
  577. {
  578. "type": "VariableDeclaration",
  579. "declarations": [
  580. {
  581. "type": "VariableDeclarator",
  582. "id": {
  583. "type": "Identifier",
  584. "name": "b"
  585. },
  586. "init": {
  587. "type": "Literal",
  588. "value": 2,
  589. "raw": "2"
  590. }
  591. }
  592. ],
  593. "kind": "const"
  594. }
  595. ],
  596. "sourceType": "script"
  597. }`;
  598. assert.strictEqual(result.toString(), expected);
  599. }),
  600. it("JPathExpression", () => {
  601. assert.strictEqual(chef.JPathExpression("{\"key\" : \"value\"}", {query: "$.key"}).toString(), "\"value\"");
  602. }),
  603. it("JSON Beautify", () => {
  604. assert.strictEqual(
  605. chef.JSONBeautify("{\"key\" : \"value\"}").toString(),
  606. `{
  607. \\t"key": "value"
  608. }`);
  609. }),
  610. it("Keccak", () => {
  611. assert.strictEqual(chef.keccak("Flea Market").toString(), "c2a06880b19e453ee5440e8bd4c2024bedc15a6630096aa3f609acfd2b8f15f27cd293e1cc73933e81432269129ce954a6138889ce87831179d55dcff1cc7587");
  612. }),
  613. it("MD6", () => {
  614. assert.strictEqual(chef.MD6("Head Over Heels", {key: "arty"}).toString(), "d8f7fe4931fbaa37316f76283d5f615f50ddd54afdc794b61da522556aee99ad");
  615. }),
  616. it("toBase64: editableOption", () => {
  617. const result = toBase64("some input", {
  618. alphabet: {
  619. value: "0-9A-W"
  620. },
  621. });
  622. assert.strictEqual(result.toString(), "SPI1R1T0");
  623. }),
  624. it("toBase64: editableOptions key is value", () => {
  625. const result = toBase64("some input", {
  626. alphabet: "0-9A-W",
  627. });
  628. assert.strictEqual(result.toString(), "SPI1R1T0");
  629. }),
  630. it("toBase64: editableOptions default", () => {
  631. const result = toBase64("some input");
  632. assert.strictEqual(result.toString(), "c29tZSBpbnB1dA==");
  633. }),
  634. it("toHex: accepts args", () => {
  635. const result = toHex("some input", {
  636. delimiter: "Colon",
  637. });
  638. assert.strictEqual(result.toString(), "73:6f:6d:65:20:69:6e:70:75:74");
  639. }),
  640. ]);