ops.mjs 34 KB

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