ops.mjs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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 fs from "fs";
  20. import {
  21. addLineNumbers,
  22. adler32Checksum,
  23. AESDecrypt,
  24. affineCipherDecode,
  25. affineCipherEncode,
  26. bifidCipherEncode,
  27. bitShiftRight,
  28. cartesianProduct,
  29. CSSMinify,
  30. toBase64,
  31. toHex,
  32. } from "../../../src/node/index";
  33. import chef from "../../../src/node/index";
  34. import TestRegister from "../../TestRegister";
  35. TestRegister.addApiTests([
  36. it("ADD: toggleString argument", () => {
  37. const result = chef.ADD("sample input", {
  38. key: {
  39. string: "some key",
  40. option: "Hex"
  41. }
  42. });
  43. assert.equal(result.toString(), "aO[^ZS\u000eW\\^cb");
  44. }),
  45. it("ADD: default option toggleString argument", () => {
  46. const result = chef.ADD(3, {
  47. key: "4",
  48. });
  49. assert.strictEqual(result.toString(), "7");
  50. }),
  51. it("addLineNumbers: No arguments", () => {
  52. const result = addLineNumbers("sample input");
  53. assert.equal(result.toString(), "1 sample input");
  54. }),
  55. it("adler32Checksum: No args", () => {
  56. const result = adler32Checksum("sample input");
  57. assert.equal(result.toString(), "1f2304d3");
  58. }),
  59. it("AES decrypt: toggleString and option", () => {
  60. const result = AESDecrypt("812c34ae6af353244a63c6ce23b7c34286b60be28ea4645523d4494700e7", {
  61. key: {
  62. string: "some longer key1",
  63. option: "utf8",
  64. },
  65. iv: {
  66. string: "some iv",
  67. option: "utf8",
  68. },
  69. mode: "OFB",
  70. });
  71. assert.equal(result.toString(), "a slightly longer sampleinput?");
  72. }),
  73. it("AffineCipherDecode: number input", () => {
  74. const result = affineCipherDecode("some input", {
  75. a: 7,
  76. b: 4
  77. });
  78. assert.strictEqual(result.toString(), "cuqa ifjgr");
  79. }),
  80. it("affineCipherEncode: number input", () => {
  81. const result = affineCipherEncode("some input", {
  82. a: 11,
  83. b: 6
  84. });
  85. assert.strictEqual(result.toString(), "weiy qtpsh");
  86. }),
  87. it("analyzeHash", () => {
  88. const result = chef.analyseHash(chef.MD5("some input"));
  89. const expected = `Hash length: 32
  90. Byte length: 16
  91. Bit length: 128
  92. Based on the length, this hash could have been generated by one of the following hashing functions:
  93. MD5
  94. MD4
  95. MD2
  96. HAVAL-128
  97. RIPEMD-128
  98. Snefru
  99. Tiger-128`;
  100. assert.strictEqual(result.toString(), expected);
  101. }),
  102. it("AND", () => {
  103. const result = chef.AND("Scot-free", {
  104. key: {
  105. string: "Raining Cats and Dogs",
  106. option: "Hex",
  107. }
  108. });
  109. assert.strictEqual(result.toString(), "\u0000\"M$(D E");
  110. }),
  111. it("atBash Cipher", () => {
  112. const result = chef.atbashCipher("Happy as a Clam");
  113. assert.strictEqual(result.toString(), "Szkkb zh z Xozn");
  114. }),
  115. it("Bcrypt", async () => {
  116. const result = await chef.bcrypt("Put a Sock In It");
  117. assert.strictEqual(result.toString(), "$2a$10$ODeP1.6fMsb.ENk2ngPUCO7qTGVPyHA9TqDVcyupyed8FjsiF65L6");
  118. }),
  119. it("bcryptCompare", async() => {
  120. const result = await chef.bcryptCompare("Put a Sock In It", {
  121. hash: "$2a$10$2rT4a3XnIecBsd1H33dMTuyYE1HJ1n9F.V2rjQtAH73rh1qvOf/ae",
  122. });
  123. assert.strictEqual(result.toString(), "Match: Put a Sock In It");
  124. }),
  125. it("Bcrypt Parse", async () => {
  126. const result = await chef.bcryptParse("$2a$10$ODeP1.6fMsb.ENk2ngPUCO7qTGVPyHA9TqDVcyupyed8FjsiF65L6");
  127. const expected = `Rounds: 10
  128. Salt: $2a$10$ODeP1.6fMsb.ENk2ngPUCO
  129. Password hash: 7qTGVPyHA9TqDVcyupyed8FjsiF65L6
  130. Full hash: $2a$10$ODeP1.6fMsb.ENk2ngPUCO7qTGVPyHA9TqDVcyupyed8FjsiF65L6`;
  131. assert.strictEqual(result.toString(), expected);
  132. }),
  133. it("bifid cipher decode", () => {
  134. const result = chef.bifidCipherDecode("Vhef Qnte Ke Xfhz Mxon Bmgf", {
  135. keyword: "Alpha",
  136. });
  137. assert.strictEqual(result.toString(), "What Goes Up Must Come Down");
  138. }),
  139. it("bifid cipher encode: string option", () => {
  140. const result = bifidCipherEncode("some input", {
  141. keyword: "mykeyword",
  142. });
  143. assert.strictEqual(result.toString(), "nmhs zmsdo");
  144. }),
  145. it("bit shift left", () => {
  146. const result = chef.bitShiftLeft("Keep Your Eyes Peeled");
  147. assert.strictEqual(result.toString(), "–ÊÊà@²Þêä@ŠòÊæ@ ÊÊØÊÈ");
  148. }),
  149. it("bitShiftRight: number and option", () => {
  150. const result = bitShiftRight("some bits to shift", {
  151. type: "Arithmetic shift",
  152. amount: 1,
  153. });
  154. assert.strictEqual(result.toString(), "9762\u001014:9\u0010:7\u00109443:");
  155. }),
  156. it("Blowfish encrypt", () => {
  157. const result = chef.blowfishEncrypt("Fool's Gold", {
  158. key: {
  159. string: "One",
  160. option: "hex",
  161. },
  162. iv: {
  163. string: "Two",
  164. option: "utf8"
  165. }
  166. });
  167. assert.strictEqual(result.toString(), "8999b513bf2ff064b2977dea7e05f1b5");
  168. }),
  169. it("Blowfish decrypt", () => {
  170. const result = chef.blowfishDecrypt("8999b513bf2ff064b2977dea7e05f1b5", {
  171. key: {
  172. string: "One",
  173. option: "hex",
  174. },
  175. iv: {
  176. string: "Two",
  177. option: "utf8",
  178. }
  179. });
  180. assert.strictEqual(result.toString(), "Fool's Gold");
  181. }),
  182. it("BSON Serialise / Deserialise", () => {
  183. const result = chef.BSONDeserialise(chef.BSONSerialise("{\"phrase\": \"Mouth-watering\"}"));
  184. assert.strictEqual(result.toString(), `{
  185. "phrase": "Mouth-watering"
  186. }`);
  187. }),
  188. it("Bzip2 Decompress", () => {
  189. const result = chef.bzip2Decompress(chef.fromBase64("QlpoOTFBWSZTWUdQlt0AAAIVgEAAAQAmJAwAIAAxBkxA0A2pTL6U2CozxdyRThQkEdQlt0A="));
  190. assert.strictEqual(result.toString(), "Fit as a Fiddle");
  191. }),
  192. it("cartesianProduct: binary string", () => {
  193. const result = cartesianProduct("1:2\\n\\n3:4", {
  194. itemDelimiter: ":",
  195. });
  196. assert.strictEqual(result.toString(), "(1,3):(1,4):(2,3):(2,4)");
  197. }),
  198. it("Change IP format", () => {
  199. const result = chef.changeIPFormat("172.20.23.54", {
  200. inputFormat: "Dotted Decimal",
  201. outputFormat: "Hex",
  202. });
  203. assert.strictEqual(result.toString(), "ac141736");
  204. }),
  205. it("Chi square", () => {
  206. const result = chef.chiSquare("Burst Your Bubble");
  207. assert.strictEqual(result.toString(), "433.55399816176475");
  208. }),
  209. it("Compare CTPH Hashes", () => {
  210. const result = chef.compareCTPHHashes("1234\n3456");
  211. assert.strictEqual(result.toString(), "0");
  212. }),
  213. it("Compare SSDEEPHashes", () => {
  214. const result = chef.compareCTPHHashes("1234\n3456");
  215. assert.strictEqual(result.toString(), "0");
  216. }),
  217. it("Convert area", () => {
  218. const result = chef.convertArea("12345", {
  219. inputUnits: "Square metre (sq m)",
  220. outputUnits: "Isle of Wight"
  221. });
  222. assert.strictEqual(result.toString(), "0.00003248684210526316");
  223. }),
  224. it("Convert data units", () => {
  225. const result = chef.convertDataUnits("12345", {
  226. inputUnits: "Bits (b)",
  227. outputUnits: "Kilobytes (KB)",
  228. });
  229. assert.strictEqual(result.toString(), "1.543125");
  230. }),
  231. it("Convert distance", () => {
  232. const result = chef.convertDistance("1234567", {
  233. inputUnits: "Nanometres (nm)",
  234. outputUnits: "Furlongs (fur)",
  235. });
  236. assert.strictEqual(result.toString(), "0.00000613699494949495");
  237. }),
  238. it("Convert mass", () => {
  239. const result = chef.convertMass("123", {
  240. inputUnits: "Earth mass (M⊕)",
  241. outputUnits: "Great Pyramid of Giza (6,000,000 tonnes)",
  242. });
  243. assert.strictEqual(result.toString(), "122429895000000000");
  244. }),
  245. it("Convert speed", () => {
  246. const result = chef.convertSpeed("123", {
  247. inputUnits: "Lunar escape velocity",
  248. outputUnits: "Jet airliner cruising speed",
  249. });
  250. assert.strictEqual(result.toString(), "1168.5");
  251. }),
  252. it("Count occurrences", () => {
  253. const result = chef.countOccurrences("Talk the Talk", {
  254. searchString: {
  255. string: "Tal",
  256. option: "Simple string",
  257. }
  258. });
  259. assert.strictEqual(result.toString(), "2");
  260. }),
  261. it("CRC16 Checksum", () => {
  262. const result = chef.CRC16Checksum("Rain on Your Parade");
  263. assert.strictEqual(result.toString(), "db1c");
  264. }),
  265. it("CRC32 Checksum", () => {
  266. const result = chef.CRC32Checksum("Rain on Your Parade");
  267. assert.strictEqual(result.toString(), "e902f76c");
  268. }),
  269. it("CSS Beautify", () => {
  270. const result = chef.CSSBeautify("header {color:black;padding:3rem;}");
  271. const expected = `header {
  272. \\tcolor:black;
  273. \\tpadding:3rem;
  274. }
  275. `;
  276. assert.strictEqual(result.toString(), expected);
  277. }),
  278. it("CSS minify: boolean", () => {
  279. const input = `header {
  280. // comment
  281. width: 100%;
  282. color: white;
  283. }`;
  284. const result = CSSMinify(input, {
  285. preserveComments: true,
  286. });
  287. assert.strictEqual(result.toString(), "header {// comment width: 100%;color: white;}");
  288. }),
  289. it("CSS Selector", () => {
  290. const result = chef.CSSSelector("<html><header><h1>Hello</h1></header></html>", {
  291. cssSelector: "h1",
  292. });
  293. assert.strictEqual(result.toString(), "<h1>Hello</h1>");
  294. }),
  295. it("CTPH", () => {
  296. const result = chef.CTPH("If You Can't Stand the Heat, Get Out of the Kitchen");
  297. assert.strictEqual(result.toString(), "A:+EgFgBKAA0V0UFfClEs6:+Qk0gUFse");
  298. }),
  299. it("Decode NetBIOS Name", () => {
  300. assert.strictEqual(chef.decodeNetBIOSName("EBGMGMCAEHHCGFGFGLCAFEGPCAENGFCA").toString(), "All Greek To Me");
  301. }),
  302. it("Decode text", () => {
  303. const encoded = chef.encodeText("Ugly Duckling", {
  304. encoding: "UTF16LE (1200)",
  305. });
  306. const result = chef.decodeText(encoded, {
  307. encoding: "UTF16LE (1200)",
  308. });
  309. assert.strictEqual(result.toString(), "Ugly Duckling");
  310. }),
  311. it("Derive EVP Key", () => {
  312. const result = chef.deriveEVPKey("", {
  313. passphrase: {
  314. string: "46 6c 65 61 20 4d 61 72 6b 65 74",
  315. option: "Hex",
  316. },
  317. salt: {
  318. string: "Market",
  319. option: "Hex",
  320. },
  321. });
  322. assert.strictEqual(result.toString(), "7c21a9f5063a4d62fb1050068245c181");
  323. }),
  324. it("Derive PBKDF2 Key", () => {
  325. const result = chef.derivePBKDF2Key("", {
  326. passphrase: {
  327. string: "Jack of All Trades Master of None",
  328. option: "utf8",
  329. },
  330. keySize: 256,
  331. iterations: 2,
  332. hashingFunction: "md5",
  333. salt: {
  334. string: "fruit",
  335. option: "utf8"
  336. }
  337. });
  338. assert.strictEqual(result.toString(), "728a885b209e8b19cbd7430ca32608ff09190f7ccb7ded204e1d4c50f87c47bf");
  339. }),
  340. it("DES Decrypt", () => {
  341. const result = chef.DESDecrypt("713081c66db781c323965ba8f166fd8c230c3bb48504a913", {
  342. key: {
  343. string: "onetwoth",
  344. option: "utf8",
  345. },
  346. iv: {
  347. string: "threetwo",
  348. option: "Hex",
  349. },
  350. mode: "ECB",
  351. });
  352. assert.strictEqual(result.toString(), "Put a Sock In It");
  353. }),
  354. it("DES Encrypt", () => {
  355. const result = chef.DESEncrypt("Put a Sock In It", {
  356. key: {
  357. string: "onetwoth",
  358. option: "utf8",
  359. },
  360. iv: {
  361. string: "threetwo",
  362. option: "Hex",
  363. },
  364. mode: "ECB",
  365. });
  366. assert.strictEqual(result.toString(), "713081c66db781c323965ba8f166fd8c230c3bb48504a913");
  367. }),
  368. it("Diff", () => {
  369. const result = chef.diff("one two\\n\\none two three");
  370. assert.strictEqual(result.toString(), "one two three");
  371. }),
  372. it("Disassemble x86", () => {
  373. const result = chef.disassembleX86(chef.toBase64("one two three"));
  374. const expected = `0000000000000000 0000 ADD BYTE PTR [RAX],AL\r
  375. 0000000000000002 0B250000000B OR ESP,DWORD PTR [000000000B000008]\r
  376. `;
  377. assert.strictEqual(result.toString(), expected);
  378. }),
  379. it("Divide", () => {
  380. assert.strictEqual(chef.divide("4\n7").toString(), "0.57142857142857142857");
  381. }),
  382. it("Drop bytes", () => {
  383. assert.strictEqual(chef.dropBytes("There's No I in Team").toString(), "'s No I in Team");
  384. }),
  385. it("Entropy", () => {
  386. const result = chef.entropy("Ride Him, Cowboy!");
  387. assert.strictEqual(result.toString(), "3.734521664779752");
  388. }),
  389. it("Escape string", () => {
  390. const result = chef.escapeString("Know the Ropes", {
  391. escapeLevel: "Everything",
  392. JSONCompatible: false,
  393. ES6Compatible: true,
  394. uppercaseHex: true,
  395. });
  396. assert.strictEqual(result.toString(), "\\x4B\\x6E\\x6F\\x77\\x20\\x74\\x68\\x65\\x20\\x52\\x6F\\x70\\x65\\x73");
  397. }),
  398. it("Escape unicode characters", () => {
  399. assert.strictEqual(chef.escapeUnicodeCharacters("σου").toString(), "\\u03C3\\u03BF\\u03C5");
  400. }),
  401. it("Expand alphabet range", () => {
  402. assert.strictEqual(
  403. chef.expandAlphabetRange("Fight Fire With Fire", {delimiter: "t"}).toString(),
  404. "Ftitgthttt tFtitrtet tWtitttht tFtitrte");
  405. }),
  406. it("Extract dates", () => {
  407. assert.strictEqual(chef.extractDates("Don't Look a Gift Horse In The Mouth 01/02/1992").toString(), "01/02/1992\n");
  408. }),
  409. it("Filter", () => {
  410. const result = chef.filter(
  411. `I Smell a Rat
  412. Every Cloud Has a Silver Lining
  413. Top Drawer`, {
  414. regex: "Every",
  415. });
  416. const expected = "Every Cloud Has a Silver Lining";
  417. assert.strictEqual(result.toString(), expected);
  418. }),
  419. it("Find / Replace", () => {
  420. assert.strictEqual(
  421. chef.findReplace(
  422. "Curiosity Killed The Cat",
  423. {
  424. find: {
  425. string: "l",
  426. option: "Regex",
  427. },
  428. replace: "s",
  429. }).toString(),
  430. "Curiosity Kissed The Cat");
  431. }),
  432. it("Fletcher8 Checksum", () => {
  433. assert.strictEqual(chef.fletcher8Checksum("Keep Your Eyes Peeled").toString(), "48");
  434. }),
  435. it("Format MAC addresses", () => {
  436. const result = chef.formatMACAddresses("00-01-02-03-04-05");
  437. const expected = `000102030405
  438. 000102030405
  439. 00-01-02-03-04-05
  440. 00-01-02-03-04-05
  441. 00:01:02:03:04:05
  442. 00:01:02:03:04:05
  443. `;
  444. assert.strictEqual(result.toString(), expected);
  445. }),
  446. it("Frequency distribution", () => {
  447. const result = chef.frequencyDistribution("Don't Count Your Chickens Before They Hatch");
  448. 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}";
  449. assert.strictEqual(result.toString(), expected);
  450. }),
  451. it("From base", () => {
  452. assert.strictEqual(chef.fromBase("11", {radix: 13}).toString(), "14");
  453. }),
  454. it("From BCD", () => {
  455. assert.strictEqual(chef.fromBCD("1143", { inputFormat: "Raw", scheme: "7 4 2 1"}).toString(), "31313433");
  456. }),
  457. it("From binary", () => {
  458. assert.strictEqual(chef.fromBinary("010101011100101101011010").toString(), "UËZ");
  459. }),
  460. it("From Charcode", () => {
  461. 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");
  462. }),
  463. it("From decimal", () => {
  464. assert.strictEqual(chef.fromDecimal("72 101 108 108 111").toString(), "Hello");
  465. }),
  466. it("From hex", () => {
  467. assert.strictEqual(chef.fromHex("52 69 6e 67 20 41 6e 79 20 42 65 6c 6c 73 3f").toString(), "Ring Any Bells?");
  468. }),
  469. it("From hex content", () => {
  470. assert.strictEqual(chef.fromHexContent("foo|3d|bar").toString(), "foo=bar");
  471. }),
  472. it("To and From hex dump", () => {
  473. assert.strictEqual(chef.fromHexdump(chef.toHexdump("Elephant in the Room")).toString(), "Elephant in the Room");
  474. }),
  475. it("From HTML entity", () => {
  476. assert.strictEqual(chef.fromHTMLEntity("&amp;").toString(), "&");
  477. }),
  478. it("To and From morse code", () => {
  479. assert.strictEqual(chef.fromMorseCode(chef.toMorseCode("Put a Sock In It")).toString(), "PUT A SOCK IN IT");
  480. }),
  481. it("From octal", () => {
  482. assert.strictEqual(chef.fromOctal("113 156 157 167 40 164 150 145 40 122 157 160 145 163").toString(), "Know the Ropes");
  483. }),
  484. it("To, From punycode", () => {
  485. assert.strictEqual(chef.fromPunycode(chef.toPunycode("münchen")).toString(), "münchen");
  486. }),
  487. it("From unix timestamp", () => {
  488. assert.strictEqual(chef.fromUNIXTimestamp("978346800").toString(), "Mon 1 January 2001 11:00:00 UTC");
  489. }),
  490. it("Generate HOTP", () => {
  491. const result = chef.generateHOTP("Cut The Mustard", {
  492. name: "colonel",
  493. });
  494. const expected = `URI: otpauth://hotp/colonel?secret=IN2XIICUNBSSATLVON2GC4TE
  495. Password: 034148`;
  496. assert.strictEqual(result.toString(), expected);
  497. }),
  498. it("Generate PGP Key Pair", async () => {
  499. const result = await chef.generatePGPKeyPair("Back To the Drawing Board", {
  500. keyType: "ECC-256",
  501. });
  502. const expected = `-----BEGIN PGP PRIVATE KEY BLOCK-----
  503. Version: Keybase OpenPGP v2.0.77
  504. Comment: https://keybase.io/crypto
  505. xYgEW3KciQEBAK96Lx9G0WZiw1yhC35IogdumoxEJXsLdAVIjmskXeAfABEBAAEA
  506. AP4wK+OZu3AqojwtRoyIK1pHKU93OAuam1iaLCOGCwCckQCA5PjU0aLNZqy/eKyX
  507. T3rpKQCAxDDT5hHGAUfFPUu73KWABwB/WKpeUp7KwurMSbYVhgr1TijszQDCVAQT
  508. AQoAHgUCW3KciQIbLwMLCQcDFQoIAh4BAheAAxYCAQIZAQAKCRD0VeyUMgmpz3OE
  509. AP9qsnhhoK85Tnu6VKwKm1iMiJAssDQnFztDaMmmVdrN/MeIBFtynIkBAQDDhjIw
  510. fxOprqVMYLk6aC45JyPAA2POzu0Zb/rx0tKeBwARAQABAAD/XAr66oiP9ZORHiT0
  511. XZH4m7vwZt7AHuq4pYtVlMQXk60AgPw2Mno/wStvE/SBa9R7AtsAgMZ2BkJjvNPZ
  512. 9YA6cl4lW0UAgI1+kJVLZ5VR9fPENfJR80EtncKDBBgBCgAPBQJbcpyJBQkPCZwA
  513. AhsuAEgJEPRV7JQyCanPPSAEGQEKAAYFAltynIkACgkQrewgWMQZ/b2blwD/dbwh
  514. /3F9xv+YGAwq8i1mzzswg4qBct6LoSIjGglULT9RIQD/cYd31YfKrEnbSBWD5PLi
  515. zcSsxtBGKphwXiPAlQJ1Q5DHiARbcpyJAQEAs8V418lf1T74PpAwdBTiViEUX9jB
  516. e+ZrAEVaq5nu1C8AEQEAAQAA/iPWhS23hnRTllealR4/H5OofZRwxvIQrxAJp6z1
  517. ICRxAIDayRpCAbK5EC3DzRU2z4VpAIDSWYSs9inI1VTfamJPMWHXAIC3aaukzCP4
  518. GEGeFobX/thnKhnCgwQYAQoADwUCW3KciQUJA8JnAAIbLgBICRD0VeyUMgmpzz0g
  519. BBkBCgAGBQJbcpyJAAoJEB4jzL1hmQIXamUA/0c1M6BSqVtxNowPcOAXKYIxMca1
  520. VFcRWolHnZqdZQ7k/J8A/3HvNLRS3p1HvjQEfXl/qKoRRn843Py09ptDHh+xpGKh
  521. =d+Vp
  522. -----END PGP PRIVATE KEY BLOCK-----
  523. -----BEGIN PGP PUBLIC KEY BLOCK-----
  524. Version: Keybase OpenPGP v2.0.77
  525. Comment: https://keybase.io/crypto
  526. xi0EW3KciQEBAK96Lx9G0WZiw1yhC35IogdumoxEJXsLdAVIjmskXeAfABEBAAHN
  527. AMJUBBMBCgAeBQJbcpyJAhsvAwsJBwMVCggCHgECF4ADFgIBAhkBAAoJEPRV7JQy
  528. CanPc4QA/2qyeGGgrzlOe7pUrAqbWIyIkCywNCcXO0NoyaZV2s38zi0EW3KciQEB
  529. AMOGMjB/E6mupUxguTpoLjknI8ADY87O7Rlv+vHS0p4HABEBAAHCgwQYAQoADwUC
  530. W3KciQUJDwmcAAIbLgBICRD0VeyUMgmpzz0gBBkBCgAGBQJbcpyJAAoJEK3sIFjE
  531. Gf29m5cA/3W8If9xfcb/mBgMKvItZs87MIOKgXLei6EiIxoJVC0/USEA/3GHd9WH
  532. yqxJ20gVg+Ty4s3ErMbQRiqYcF4jwJUCdUOQzi0EW3KciQEBALPFeNfJX9U++D6Q
  533. MHQU4lYhFF/YwXvmawBFWquZ7tQvABEBAAHCgwQYAQoADwUCW3KciQUJA8JnAAIb
  534. LgBICRD0VeyUMgmpzz0gBBkBCgAGBQJbcpyJAAoJEB4jzL1hmQIXamUA/0c1M6BS
  535. qVtxNowPcOAXKYIxMca1VFcRWolHnZqdZQ7k/J8A/3HvNLRS3p1HvjQEfXl/qKoR
  536. Rn843Py09ptDHh+xpGKh
  537. =ySwG
  538. -----END PGP PUBLIC KEY BLOCK-----`;
  539. assert.strictEqual(result.toString(), expected);
  540. }),
  541. it("Generate UUID", () => {
  542. const result = chef.generateUUID();
  543. assert.ok(result.toString());
  544. assert.strictEqual(result.toString().length, 36);
  545. }),
  546. it("Gzip, Gunzip", () => {
  547. assert.strictEqual(chef.gunzip(chef.gzip("Down To The Wire")).toString(), "Down To The Wire");
  548. }),
  549. it("Hex to Object Identifier", () => {
  550. assert.strictEqual(
  551. chef.hexToObjectIdentifier(chef.toHex("You Can't Teach an Old Dog New Tricks")).toString(),
  552. "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");
  553. }),
  554. it("Hex to PEM", () => {
  555. const result = chef.hexToPEM(chef.toHex("Yada Yada"));
  556. const expected = `-----BEGIN CERTIFICATE-----\r
  557. WWFkYSBZYWRh\r
  558. -----END CERTIFICATE-----\r\n`;
  559. assert.strictEqual(result.toString(), expected);
  560. }),
  561. it("HMAC", () => {
  562. assert.strictEqual(chef.HMAC("On Cloud Nine", {key: "idea"}).toString(), "78206a67f34bfc073243c3c89148c21e");
  563. }),
  564. it("JPathExpression", () => {
  565. assert.strictEqual(chef.JPathExpression("{\"key\" : \"value\"}", {query: "$.key"}).toString(), "\"value\"");
  566. }),
  567. it("JSON Beautify", () => {
  568. assert.strictEqual(
  569. chef.JSONBeautify("{\"key\" : \"value\"}").toString(),
  570. `{
  571. "key": "value"
  572. }`);
  573. }),
  574. it("Keccak", () => {
  575. assert.strictEqual(chef.keccak("Flea Market").toString(), "c2a06880b19e453ee5440e8bd4c2024bedc15a6630096aa3f609acfd2b8f15f27cd293e1cc73933e81432269129ce954a6138889ce87831179d55dcff1cc7587");
  576. }),
  577. it("MD6", () => {
  578. assert.strictEqual(chef.MD6("Head Over Heels", {key: "arty"}).toString(), "d8f7fe4931fbaa37316f76283d5f615f50ddd54afdc794b61da522556aee99ad");
  579. }),
  580. it("Parse ASN.1 Hex string", () => {
  581. assert.strictEqual(chef.parseASN1HexString(chef.toHex("Mouth-watering")).toString(), "UNKNOWN(4d) 7574682d7761746572696e67\n");
  582. }),
  583. it("Parse DateTime", () => {
  584. const result = chef.parseDateTime("06/07/2001 01:59:30");
  585. const expected = `Date: Friday 6th July 2001
  586. Time: 01:59:30
  587. Period: AM
  588. Timezone: UTC
  589. UTC offset: +0000
  590. Daylight Saving Time: false
  591. Leap year: false
  592. Days in this month: 31
  593. Day of year: 187
  594. Week number: 2001
  595. Quarter: 3`;
  596. assert.strictEqual(result.toString(), expected);
  597. }),
  598. it("Parse IPV6 address", () => {
  599. const result = chef.parseIPv6Address("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
  600. const expected = `Longhand: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
  601. Shorthand: 2001:db8:85a3::8a2e:370:7334
  602. This is a documentation IPv6 address. This range should be used whenever an example IPv6 address is given or to model networking scenarios. Corresponds to 192.0.2.0/24, 198.51.100.0/24, and 203.0.113.0/24 in IPv4.
  603. Documentation range: 2001:db8::/32`;
  604. assert.strictEqual(result.toString(), expected);
  605. }),
  606. it("Parse URI", () => {
  607. const result = chef.parseURI("https://www.google.co.uk/search?q=almonds");
  608. const expected = `Protocol: https:
  609. Hostname: www.google.co.uk
  610. Path name: /search
  611. Arguments:
  612. \tq = almonds
  613. `;
  614. assert.strictEqual(result.toString(), expected);
  615. }),
  616. it("Parse user agent", () => {
  617. const result = chef.parseUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0 ");
  618. const expected = `Browser
  619. Name: Mozilla
  620. Version: 5.0
  621. Device
  622. Model: unknown
  623. Type: unknown
  624. Vendor: unknown
  625. Engine
  626. Name: Gecko
  627. Version: 47.0
  628. OS
  629. Name: Windows
  630. Version: 7
  631. CPU
  632. Architecture: amd64`;
  633. assert.strictEqual(result.toString(), expected);
  634. }),
  635. it("PGP Encrypt", async () => {
  636. const pbkey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
  637. Version: GnuPG v1
  638. mI0EVWOihAEEALzwFAVWTrD0KiWCH5tX6q6QsGjlRn4IP2uj/xWsJZDNbCKm+JAe
  639. 1RvIootpW1+PNNMJlIInwUgtCjtJ9gZbGBpXeqwdSn0oYuj9X86ekXOUnZsRoPCj
  640. RwS8kpbrvRVfhWN8hYuXFcXK2J2Ld0ZpVyJzkncpFdpAgzTPMfrO1HS5ABEBAAG0
  641. GmdwZyBuYW1lIChjb21tZW50KSA8ZW1AaWw+iLgEEwECACIFAlVjooQCGwMGCwkI
  642. BwMCBhUIAgkKCwQWAgMBAh4BAheAAAoJEBg8dTRwi4g5I40D/2+uUuQxa3uMrAeI
  643. dXLaJWz3V0cl1rotfBP47apDUGbkm1HVgULJUo8Bo15Ii83ST8TUsyja3XcLutHb
  644. IwYSWo41gEV48+NKoN6Oy3HwqBoHfH06bu0If75vdSjnZpB2dO/Ph7L9kz78gc4y
  645. tZx4bE64MTlL2AYghZxyYpFyydjXuI0EVWOihAEEANE4UU+4iB2hMAXq93hiBzIh
  646. AMtn/DlWbJkpdUgrjKOG6tILs28mrw9rI4PivmT7grkyNW8sa3ATsmWC1xChxGTN
  647. T1guyh0Hhbc3Otfng2BFSWcBkPwUoNaOdrVFpP9J51IYrsQHsjbZlY45ghDBzM6t
  648. sISfkmmFCsp0l7w/XAcvABEBAAGInwQYAQIACQUCVWOihAIbDAAKCRAYPHU0cIuI
  649. OQ2BA/9KWqOhXZW75ac7CuJMfileZR7vRy9CkKyNG21cZtAlqftAX+m8FGdG0duU
  650. jKHiPvjXhSfP3lmrQ7brja9LgSzkiBqQzvPW55G67nGQdUC+mqZNJNlRh+8atf9I
  651. 5nxg2i8zn6F5cLaNWz7cl27m1/mXKcH3gult1PLR4PiYLiC9aw==
  652. =xw3e
  653. -----END PGP PUBLIC KEY BLOCK-----`;
  654. const result = await chef.PGPEncrypt("A Fool and His Money are Soon Parted", {
  655. publicKeyOfRecipient: pbkey,
  656. });
  657. const expected = `-----BEGIN PGP MESSAGE-----
  658. Version: Keybase OpenPGP v2.0.77
  659. Comment: https://keybase.io/crypto
  660. wYwDv1kIXPPNwmABA/4syW+oO+S/mfpjdp83/MZJiKh6XNQoPr/N5/1Is/QXYu9V
  661. /v8/b+eReOpUVC6cVrJ8U5cB19y1Az3NQWHXLEC0jND2wL3cUM4sv87hlvv2PLhc
  662. okv8OHNCitRiweo7NZHVygHGdFvY082G47e1PkyPAuVynvzdD450ta/s/KOxZdJg
  663. ARbZIrC6WmjYNLwhbpRYawKD+3N4I5qliRpU2POKRi9UROAW9dth6egy60TTCvyO
  664. jmPGsv1elXxVzqs58UZLD2c3vBhGkU2BV6kRKh+lj/EcVrzsFhGCz/7DKxPoDHLS
  665. =IBYt
  666. -----END PGP MESSAGE-----
  667. `;
  668. assert.strictEqual(result.toString(), expected);
  669. }),
  670. it("Raw deflate", () => {
  671. assert.strictEqual(chef.rawInflate(chef.rawDeflate("Like Father Like Son", { compressionType: "Fixed Huffman Coding"})).toString(), "Like Father Like Son");
  672. }),
  673. it("RC4", () => {
  674. assert.strictEqual(
  675. chef.RC4("Go Out On a Limb", {passphrase: {string: "Under Your Nose", option: "UTF8"}, inputFormat: "UTF8", outputFormat: "Hex"}).toString(),
  676. "7d17e60d9bc94b7f4095851c729e69a2");
  677. }),
  678. it("RC4 Drop", () => {
  679. assert.strictEqual(
  680. chef.RC4Drop("Go Out On a Limb", {passphrase: {string: "Under Your Nose", option: "UTF8"}, inputFormat: "UTF8", outputFormat: "Hex"}).toString(),
  681. "8fa5f2751d34476a0c857439f43816cf");
  682. }),
  683. it("Regular Expression", () => {
  684. assert.strictEqual(chef.regularExpression("Wouldn't Harm a Fly", {regex: "\\'[a-z]"}).toString(), "Wouldn't Harm a Fly");
  685. }),
  686. it("Remove EXIF", () => {
  687. const result = chef.removeEXIF(fs.readFileSync("test/tests/nodeApi/sampleData/pic.jpg"));
  688. assert.strictEqual(result.toString().length, 4582);
  689. }),
  690. it("Scan for embedded files", () => {
  691. const result = chef.scanForEmbeddedFiles(fs.readFileSync("src/web/static/images/cook_male-32x32.png"));
  692. const expected = "Scanning data for 'magic bytes' which may indicate embedded files.";
  693. assert.ok(result.toString().indexOf(expected) === 0);
  694. }),
  695. it("Scrypt", () => {
  696. assert.strictEqual(
  697. chef.scrypt("Playing For Keeps", {salt: {string: "salty", option: "Hex"}}).toString(),
  698. "5446b6d86d88515894a163201765bceed0bc39610b1506cdc4d939ffc638bc46e051bce756e2865165d89d955a43a7eb5504502567dea8bfc9e7d49aaa894c07");
  699. }),
  700. it("SHA3", () => {
  701. assert.strictEqual(
  702. chef.SHA3("benign gravel").toString(),
  703. "2b1e36e0dbe151a89887be08da3bad141908cce62327f678161bcf058627e87abe57e3c5fce6581678714e6705a207acbd5c1f37f7a812280bc2cc558f00bed9");
  704. }),
  705. it("Shake", () => {
  706. assert.strictEqual(
  707. chef.shake("murderous bloodshed").toString(),
  708. "b79b3bb88099330bc6a15122f8dfaededf57a33b51c748d5a94e8122ff18d21e12f83412926b7e4a77a85ba6f36aa4841685e78296036337175e40096b5ac000");
  709. }),
  710. it("Snefru", () => {
  711. assert.strictEqual(
  712. chef.snefru("demeaning milestone").toString(),
  713. "a671b48770fe073ce49e9259cc2f47d345a53712639f8ae23c5ad3fec19540a5");
  714. }),
  715. it("SQL Beautify", () => {
  716. const result = chef.SQLBeautify(`SELECT MONTH, ID, RAIN_I, TEMP_F
  717. FROM STATS;`);
  718. const expected = `SELECT MONTH,
  719. ID,
  720. RAIN_I,
  721. TEMP_F
  722. FROM STATS;`;
  723. assert.strictEqual(result.toString(), expected);
  724. }),
  725. it("SSDEEP", () => {
  726. assert.strictEqual(
  727. chef.SSDEEP("shotgun tyranny snugly").toString(),
  728. "3:DLIXzMQCJc:XERKc");
  729. }),
  730. it("strings", () => {
  731. const result = chef.strings("smothering ampersand abreast", {displayTotal: true});
  732. const expected = `Total found: 1
  733. smothering ampersand abreast
  734. `;
  735. assert.strictEqual(result.toString(), expected);
  736. }),
  737. it("toBase64: editableOption", () => {
  738. const result = toBase64("some input", {
  739. alphabet: {
  740. value: "0-9A-W"
  741. },
  742. });
  743. assert.strictEqual(result.toString(), "SPI1R1T0");
  744. }),
  745. it("toBase64: editableOptions key is value", () => {
  746. const result = toBase64("some input", {
  747. alphabet: "0-9A-W",
  748. });
  749. assert.strictEqual(result.toString(), "SPI1R1T0");
  750. }),
  751. it("toBase64: editableOptions default", () => {
  752. const result = toBase64("some input");
  753. assert.strictEqual(result.toString(), "c29tZSBpbnB1dA==");
  754. }),
  755. it("To BCD", () => {
  756. assert.strictEqual(chef.toBCD("443").toString(), "0100 0100 0011");
  757. }),
  758. it("To CamelCase", () => {
  759. assert.strictEqual(chef.toCamelCase("Quickest Wheel").toString(), "quickestWheel");
  760. }),
  761. it("toHex: accepts args", () => {
  762. const result = toHex("some input", {
  763. delimiter: "Colon",
  764. });
  765. assert.strictEqual(result.toString(), "73:6f:6d:65:20:69:6e:70:75:74");
  766. }),
  767. it("To Kebab case", () => {
  768. assert.strictEqual(chef.toKebabCase("Elfin Gold").toString(), "elfin-gold");
  769. }),
  770. it("To punycode", () => {
  771. assert.strictEqual(chef.toPunycode("♠ ♣ ♥ ♦ ← ↑ ‍ →").toString(), " -m06cw7klao368lfb3aq");
  772. }),
  773. it("to snake case", () => {
  774. assert.strictEqual(chef.toSnakeCase("Abhorrent Grass").value, "abhorrent_grass");
  775. }),
  776. it("to unix timestamp", () => {
  777. assert.strictEqual(chef.toUNIXTimestamp("04-01-2001").toString(), "986083200 (Sun 1 April 2001 00:00:00 UTC)");
  778. }),
  779. it("Translate DateTime format", () => {
  780. assert.strictEqual(chef.translateDateTimeFormat("01/04/1999 22:33:01").toString(), "Thursday 1st April 1999 22:33:01 +00:00 UTC");
  781. }),
  782. it("Triple DES encrypt / decrypt", () => {
  783. assert.strictEqual(
  784. chef.tripleDESDecrypt(
  785. chef.tripleDESEncrypt("Destroy Money", {key: {string: "30 31 2f 30 34 2f 31 39 39 39 20 32 32 3a 33 33 3a 30 3130 31 2f 30 34", option: "Hex"}}),
  786. {key: {string: "30 31 2f 30 34 2f 31 39 39 39 20 32 32 3a 33 33 3a 30 3130 31 2f 30 34", option: "Hex"}}).toString(),
  787. "Destroy Money");
  788. }),
  789. it("UNIX Timestamp to Windows Filetime", () => {
  790. assert.strictEqual(chef.UNIXTimestampToWindowsFiletime("2020735").toString(), "116464943350000000");
  791. }),
  792. it("XML Beautify", () => {
  793. assert.strictEqual(
  794. chef.XMLBeautify("<contact-info><company>abc</company></contact-info>").toString(),
  795. `<contact-info>
  796. \\t<company>abc</company>
  797. </contact-info>`);
  798. }),
  799. it("XOR: toggleString with default option", () => {
  800. assert.strictEqual(chef.XOR("fe023da5", {
  801. key: "73 6f 6d 65"
  802. }).toString(),
  803. "\u0015\n]W@\u000b\fP");
  804. }),
  805. it("XOR: toggleString with custom option", () => {
  806. assert.strictEqual(chef.XOR("fe023da5", {
  807. key: {
  808. string: "73 6f 6d 65",
  809. option: "utf8",
  810. }
  811. }).toString(),
  812. "QV\u0010\u0004UDWQ");
  813. }),
  814. it("XPath expression", () => {
  815. assert.strictEqual(
  816. chef.XPathExpression("<contact-info><company>abc</company></contact-info>", {xPath: "contact-info/company"}).toString(),
  817. "<company>abc</company>");
  818. }),
  819. it("Zlib deflate / inflate", () => {
  820. assert.strictEqual(chef.zlibInflate(chef.zlibDeflate("cut homer wile rooky grits dizen")).toString(), "cut homer wile rooky grits dizen");
  821. }),
  822. it("extract EXIF", () => {
  823. assert.strictEqual(
  824. chef.extractEXIF(fs.readFileSync("test/tests/nodeApi/sampleData/pic.jpg")).toString(),
  825. `Found 7 tags.
  826. Orientation: 1
  827. XResolution: 72
  828. YResolution: 72
  829. ResolutionUnit: 2
  830. ColorSpace: 1
  831. ExifImageWidth: 57
  832. ExifImageHeight: 57`);
  833. }),
  834. ]);