@node-cli/secret 1.2.2 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib.js.map +1 -1
- package/dist/parse.js +3 -3
- package/dist/parse.js.map +1 -1
- package/dist/secret.js +2 -2
- package/dist/secret.js.map +1 -1
- package/dist/utilities.js +3 -3
- package/dist/utilities.js.map +1 -1
- package/package.json +12 -8
package/dist/lib.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib.ts"],"sourcesContent":["import crypto from \"node:crypto\";\nimport { Logger } from \"@node-cli/logger\";\n\nexport const logger = new Logger({\n\tboring: process.env.NODE_ENV === \"test\",\n});\n\nconst HEX = \"hex\";\nconst UTF8 = \"utf8\";\n\nconst DEFAULT_CRYPTO_ALGO = \"aes-256-ctr\";\nconst DEFAULT_HASH_ALGO = \"md5\";\nconst DEFAULT_BYTES_FOR_IV = 16;\nconst DEFAULT_BYTES_FOR_SALT = 256;\nconst DEFAULT_SALT_SIZE_FOR_HASH = 16;\n\n/**\n * Create an hexadecimal hash from a given string. The default\n * algorithm is md5 but it can be changed to anything that\n * crypto.createHash allows.\n * @param {String} string the string to hash\n * @param {String} [algorithm='md5'] the algorithm to use or hashing\n * @return {String} the hashed string in hexa format\n */\nexport const createHash = (\n\tstring: string,\n\talgorithm: string = DEFAULT_HASH_ALGO,\n): string => {\n\treturn crypto.createHash(algorithm).update(string, UTF8).digest(HEX);\n};\n\n/**\n * Creates a random SALT value using the crypto library.\n * @param {Number} [bytes=256] the number of bytes to generate\n * @return {String} the generated salt in hexa format\n */\nexport const createSalt = (bytes?: number): string => {\n\tbytes = bytes || DEFAULT_BYTES_FOR_SALT;\n\treturn crypto.randomBytes(bytes).toString(HEX);\n};\n\n/**\n * Encrypts a string or a buffer using AES-256-CTR\n * algorithm.\n * @param {String} password a unique password\n * @param {String} data a string to encrypt\n * @return {String} the encrypted data in hexa\n * encoding, followed by a dollar sign ($) and by a\n * unique random initialization vector.\n */\nexport const encrypt = (password: string, data: string): string => {\n\t// Ensure that the initialization vector (IV) is random.\n\tconst iv = crypto.randomBytes(DEFAULT_BYTES_FOR_IV);\n\t// Hash the given password (result is always the same).\n\tconst key = createHash(password);\n\t// Create a cipher.\n\tconst cipher = crypto.createCipheriv(DEFAULT_CRYPTO_ALGO, key, iv);\n\t// Encrypt the data using the newly created cipher.\n\tconst encrypted = cipher.update(data, UTF8, HEX) + cipher.final(HEX);\n\t/*\n\t * Append the IV at the end of the encrypted data\n\t * to reuse it for decryption (IV is not a key,\n\t * it can be public).\n\t */\n\treturn `${encrypted}$${iv.toString(HEX)}`;\n};\n\n/**\n * Decrypts a string that was encrypted using the\n * AES-256-CRT algorithm via `encrypt`. It expects\n * the encrypted string to have the corresponding\n * initialization vector appended at the end, after\n * a dollar sign ($) - which was done via the\n * corresponding `encrypt` method.\n * @param {String} password a unique password\n * @param {String} data a string to decrypt\n * @return {String} the decrypted data\n */\nexport const decrypt = (password: string, data: string): string => {\n\t// Extract encrypted data and initialization vector (IV).\n\tconst [encrypted, ivHex] = data.split(\"$\");\n\t// Create a buffer out of the raw hex IV\n\tconst iv = Buffer.from(ivHex, HEX);\n\t// Hash the given password (result is always the same).\n\tconst hash = createHash(password);\n\t// Create a cipher.\n\tconst decipher = crypto.createDecipheriv(DEFAULT_CRYPTO_ALGO, hash, iv);\n\t// Return the decrypted data using the newly created cipher.\n\treturn decipher.update(encrypted, HEX, UTF8) + decipher.final(\"utf8\");\n};\n\n/**\n * Function using the scrypt Password-Based Key Derivation method.\n * It generates a derived key of a given length from the given data\n * and salt.\n *\n * @param {String} data the data to derive the key from\n * @param {String} salt the salt to use\n * @returns {String} the derived key in hex format\n */\nfunction generateScryptKey(data: string, salt: string): string {\n\tconst encodedData = new TextEncoder().encode(data);\n\tconst encodedSalt = new TextEncoder().encode(salt);\n\t/**\n\t * The scryptSync parameters are based on the following recommendations:\n\t * - The cost parameter should be set as high as possible without causing\n\t * significant performance degradation. OWASP recommends a cost of 2^17,\n\t * but this fails on some systems due to the high memory requirements.\n\t * - The blockSize parameter should be set to 8 at a minimum.\n\t * - The parallelization parameter should be set to 1.\n\t * - The size parameter should be set to 64.\n\t *\n\t * Reference:\n\t * https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html\n\t */\n\tconst derivedKey = crypto.scryptSync(encodedData, encodedSalt, 64, {\n\t\tcost: Math.pow(2, 14),\n\t\tblockSize: 8,\n\t\tparallelization: 1,\n\t});\n\treturn (derivedKey as Buffer).toString(HEX);\n}\n\n/**\n * Method to hash a password using the scrypt algorithm.\n *\n * @param {String} password the password to hash\n * @param {String} salt the salt to use\n * @returns {String} the hashed password\n */\nexport const hashPassword = (password: string, salt?: string): string => {\n\tconst pepper = salt || createSalt(DEFAULT_SALT_SIZE_FOR_HASH);\n\tconst key = generateScryptKey(password.normalize(\"NFKC\"), pepper);\n\treturn `${pepper}:${key}`;\n};\n\n/**\n * Method to verify a password against a hash using the scrypt algorithm.\n *\n * @param {String} password the password to verify\n * @param {String} hash the hash to verify against\n * @returns {Boolean} true if the password is correct, false otherwise\n */\nexport const verifyPassword = (password: string, hash: string): boolean => {\n\tconst [salt, key] = hash.split(\":\");\n\tconst keyBuffer = Buffer.from(key, HEX);\n\n\tconst derivedKey = generateScryptKey(password.normalize(\"NFKC\"), salt);\n\tconst derivedKeyBuffer = Buffer.from(derivedKey, HEX);\n\n\ttry {\n\t\treturn crypto.timingSafeEqual(keyBuffer, derivedKeyBuffer);\n\t} catch (_error) {\n\t\treturn false;\n\t}\n};\n"],"names":["crypto","Logger","logger","boring","process","env","NODE_ENV","HEX","UTF8","DEFAULT_CRYPTO_ALGO","DEFAULT_HASH_ALGO","DEFAULT_BYTES_FOR_IV","DEFAULT_BYTES_FOR_SALT","DEFAULT_SALT_SIZE_FOR_HASH","createHash","string","algorithm","update","digest","createSalt","bytes","randomBytes","toString","encrypt","password","data","iv","key","cipher","createCipheriv","encrypted","final","decrypt","ivHex","split","Buffer","from","hash","decipher","createDecipheriv","generateScryptKey","salt","encodedData","TextEncoder","encode","encodedSalt","derivedKey","scryptSync","cost","Math","pow","blockSize","parallelization","hashPassword","pepper","normalize","verifyPassword","keyBuffer","derivedKeyBuffer","timingSafeEqual","_error"],"mappings":"AAAA,OAAOA,YAAY,cAAc;AACjC,SAASC,MAAM,QAAQ,mBAAmB;AAE1C,OAAO,MAAMC,SAAS,IAAID,OAAO;IAChCE,QAAQC,QAAQC,GAAG,CAACC,QAAQ,KAAK;AAClC,GAAG;AAEH,MAAMC,MAAM;AACZ,MAAMC,OAAO;AAEb,MAAMC,sBAAsB;AAC5B,MAAMC,oBAAoB;AAC1B,MAAMC,uBAAuB;AAC7B,MAAMC,yBAAyB;AAC/B,MAAMC,6BAA6B;AAEnC;;;;;;;CAOC,GACD,OAAO,MAAMC,aAAa,CACzBC,QACAC,YAAoBN,iBAAiB;IAErC,OAAOV,OAAOc,UAAU,CAACE,WAAWC,MAAM,CAACF,QAAQP,MAAMU,MAAM,CAACX;AACjE,EAAE;AAEF;;;;CAIC,GACD,OAAO,MAAMY,aAAa,CAACC;IAC1BA,QAAQA,SAASR;IACjB,OAAOZ,OAAOqB,WAAW,CAACD,OAAOE,QAAQ,CAACf;AAC3C,EAAE;AAEF;;;;;;;;CAQC,GACD,OAAO,MAAMgB,UAAU,CAACC,UAAkBC;IACzC,wDAAwD;IACxD,MAAMC,KAAK1B,OAAOqB,WAAW,CAACV;IAC9B,uDAAuD;IACvD,MAAMgB,MAAMb,WAAWU;IACvB,mBAAmB;IACnB,MAAMI,SAAS5B,OAAO6B,cAAc,CAACpB,qBAAqBkB,KAAKD;IAC/D,mDAAmD;IACnD,MAAMI,YAAYF,OAAOX,MAAM,CAACQ,MAAMjB,MAAMD,OAAOqB,OAAOG,KAAK,CAACxB;IAChE;;;;EAIC,GACD,OAAO,
|
|
1
|
+
{"version":3,"sources":["../src/lib.ts"],"sourcesContent":["import crypto from \"node:crypto\";\nimport { Logger } from \"@node-cli/logger\";\n\nexport const logger = new Logger({\n\tboring: process.env.NODE_ENV === \"test\",\n});\n\nconst HEX = \"hex\";\nconst UTF8 = \"utf8\";\n\nconst DEFAULT_CRYPTO_ALGO = \"aes-256-ctr\";\nconst DEFAULT_HASH_ALGO = \"md5\";\nconst DEFAULT_BYTES_FOR_IV = 16;\nconst DEFAULT_BYTES_FOR_SALT = 256;\nconst DEFAULT_SALT_SIZE_FOR_HASH = 16;\n\n/**\n * Create an hexadecimal hash from a given string. The default\n * algorithm is md5 but it can be changed to anything that\n * crypto.createHash allows.\n * @param {String} string the string to hash\n * @param {String} [algorithm='md5'] the algorithm to use or hashing\n * @return {String} the hashed string in hexa format\n */\nexport const createHash = (\n\tstring: string,\n\talgorithm: string = DEFAULT_HASH_ALGO,\n): string => {\n\treturn crypto.createHash(algorithm).update(string, UTF8).digest(HEX);\n};\n\n/**\n * Creates a random SALT value using the crypto library.\n * @param {Number} [bytes=256] the number of bytes to generate\n * @return {String} the generated salt in hexa format\n */\nexport const createSalt = (bytes?: number): string => {\n\tbytes = bytes || DEFAULT_BYTES_FOR_SALT;\n\treturn crypto.randomBytes(bytes).toString(HEX);\n};\n\n/**\n * Encrypts a string or a buffer using AES-256-CTR\n * algorithm.\n * @param {String} password a unique password\n * @param {String} data a string to encrypt\n * @return {String} the encrypted data in hexa\n * encoding, followed by a dollar sign ($) and by a\n * unique random initialization vector.\n */\nexport const encrypt = (password: string, data: string): string => {\n\t// Ensure that the initialization vector (IV) is random.\n\tconst iv = crypto.randomBytes(DEFAULT_BYTES_FOR_IV);\n\t// Hash the given password (result is always the same).\n\tconst key = createHash(password);\n\t// Create a cipher.\n\tconst cipher = crypto.createCipheriv(DEFAULT_CRYPTO_ALGO, key, iv);\n\t// Encrypt the data using the newly created cipher.\n\tconst encrypted = cipher.update(data, UTF8, HEX) + cipher.final(HEX);\n\t/*\n\t * Append the IV at the end of the encrypted data\n\t * to reuse it for decryption (IV is not a key,\n\t * it can be public).\n\t */\n\treturn `${encrypted}$${iv.toString(HEX)}`;\n};\n\n/**\n * Decrypts a string that was encrypted using the\n * AES-256-CRT algorithm via `encrypt`. It expects\n * the encrypted string to have the corresponding\n * initialization vector appended at the end, after\n * a dollar sign ($) - which was done via the\n * corresponding `encrypt` method.\n * @param {String} password a unique password\n * @param {String} data a string to decrypt\n * @return {String} the decrypted data\n */\nexport const decrypt = (password: string, data: string): string => {\n\t// Extract encrypted data and initialization vector (IV).\n\tconst [encrypted, ivHex] = data.split(\"$\");\n\t// Create a buffer out of the raw hex IV\n\tconst iv = Buffer.from(ivHex, HEX);\n\t// Hash the given password (result is always the same).\n\tconst hash = createHash(password);\n\t// Create a cipher.\n\tconst decipher = crypto.createDecipheriv(DEFAULT_CRYPTO_ALGO, hash, iv);\n\t// Return the decrypted data using the newly created cipher.\n\treturn decipher.update(encrypted, HEX, UTF8) + decipher.final(\"utf8\");\n};\n\n/**\n * Function using the scrypt Password-Based Key Derivation method.\n * It generates a derived key of a given length from the given data\n * and salt.\n *\n * @param {String} data the data to derive the key from\n * @param {String} salt the salt to use\n * @returns {String} the derived key in hex format\n */\nfunction generateScryptKey(data: string, salt: string): string {\n\tconst encodedData = new TextEncoder().encode(data);\n\tconst encodedSalt = new TextEncoder().encode(salt);\n\t/**\n\t * The scryptSync parameters are based on the following recommendations:\n\t * - The cost parameter should be set as high as possible without causing\n\t * significant performance degradation. OWASP recommends a cost of 2^17,\n\t * but this fails on some systems due to the high memory requirements.\n\t * - The blockSize parameter should be set to 8 at a minimum.\n\t * - The parallelization parameter should be set to 1.\n\t * - The size parameter should be set to 64.\n\t *\n\t * Reference:\n\t * https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html\n\t */\n\tconst derivedKey = crypto.scryptSync(encodedData, encodedSalt, 64, {\n\t\tcost: Math.pow(2, 14),\n\t\tblockSize: 8,\n\t\tparallelization: 1,\n\t});\n\treturn (derivedKey as Buffer).toString(HEX);\n}\n\n/**\n * Method to hash a password using the scrypt algorithm.\n *\n * @param {String} password the password to hash\n * @param {String} salt the salt to use\n * @returns {String} the hashed password\n */\nexport const hashPassword = (password: string, salt?: string): string => {\n\tconst pepper = salt || createSalt(DEFAULT_SALT_SIZE_FOR_HASH);\n\tconst key = generateScryptKey(password.normalize(\"NFKC\"), pepper);\n\treturn `${pepper}:${key}`;\n};\n\n/**\n * Method to verify a password against a hash using the scrypt algorithm.\n *\n * @param {String} password the password to verify\n * @param {String} hash the hash to verify against\n * @returns {Boolean} true if the password is correct, false otherwise\n */\nexport const verifyPassword = (password: string, hash: string): boolean => {\n\tconst [salt, key] = hash.split(\":\");\n\tconst keyBuffer = Buffer.from(key, HEX);\n\n\tconst derivedKey = generateScryptKey(password.normalize(\"NFKC\"), salt);\n\tconst derivedKeyBuffer = Buffer.from(derivedKey, HEX);\n\n\ttry {\n\t\treturn crypto.timingSafeEqual(keyBuffer, derivedKeyBuffer);\n\t} catch (_error) {\n\t\treturn false;\n\t}\n};\n"],"names":["crypto","Logger","logger","boring","process","env","NODE_ENV","HEX","UTF8","DEFAULT_CRYPTO_ALGO","DEFAULT_HASH_ALGO","DEFAULT_BYTES_FOR_IV","DEFAULT_BYTES_FOR_SALT","DEFAULT_SALT_SIZE_FOR_HASH","createHash","string","algorithm","update","digest","createSalt","bytes","randomBytes","toString","encrypt","password","data","iv","key","cipher","createCipheriv","encrypted","final","decrypt","ivHex","split","Buffer","from","hash","decipher","createDecipheriv","generateScryptKey","salt","encodedData","TextEncoder","encode","encodedSalt","derivedKey","scryptSync","cost","Math","pow","blockSize","parallelization","hashPassword","pepper","normalize","verifyPassword","keyBuffer","derivedKeyBuffer","timingSafeEqual","_error"],"mappings":"AAAA,OAAOA,YAAY,cAAc;AACjC,SAASC,MAAM,QAAQ,mBAAmB;AAE1C,OAAO,MAAMC,SAAS,IAAID,OAAO;IAChCE,QAAQC,QAAQC,GAAG,CAACC,QAAQ,KAAK;AAClC,GAAG;AAEH,MAAMC,MAAM;AACZ,MAA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WvB,KAAKC,QAAQ8B,SAASP,KAAK,CAAC;AAC/D,EAAE;AAEF;;;;;;;;CAQC,GACD,SAASS,kBAAkBf,IAAY,EAAEgB,IAAY;IACpD,MAAMC,cAAc,IAAIC,cAAcC,MAAM,CAACnB;IAC7C,MAAMoB,cAAc,IAAIF,cAAcC,MAAM,CAACH;IAC7C;;;;;;;;;;;EAWC,GACD,MAAMK,aAAa9C,OAAO+C,UAAU,CAACL,aAAaG,aAAa,IAAI;QAClEG,MAAMC,KAAKC,GAAG,CAAC,GAAG;QAClBC,WAAW;QACXC,iBAAiB;IAClB;IACA,OAAO,AAACN,WAAsBxB,QAAQ,CAACf;AACxC;AAEA;;;;;;CAMC,GACD,OAAO,MAAM8C,eAAe,CAAC7B,UAAkBiB;IAC9C,MAAMa,SAASb,QAAQtB,WAAWN;IAClC,MAAMc,MAAMa,kBAAkBhB,SAAS+B,SAAS,CAAC,SAASD;IAC1D,OAAO,GAAGA,OAAO,CAAC,EAAE3B,KAAK;AAC1B,EAAE;AAEF;;;;;;CAMC,GACD,OAAO,MAAM6B,iBAAiB,CAAChC,UAAkBa;IAChD,MAAM,CAACI,MAAMd,IAAI,GAAGU,KAAKH,KAAK,CAAC;IAC/B,MAAMuB,YAAYtB,OAAOC,IAAI,CAACT,KAAKpB;IAEnC,MAAMuC,aAAaN,kBAAkBhB,SAAS+B,SAAS,CAAC,SAASd;IACjE,MAAMiB,mBAAmBvB,OAAOC,IAAI,CAACU,YAAYvC;IAEjD,IAAI;QACH,OAAOP,OAAO2D,eAAe,CAACF,WAAWC;IAC1C,EAAE,OAAOE,QAAQ;QAChB,OAAO;IACR;AACD,EAAE"}
|
package/dist/parse.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { parser } from "@node-cli/parser";
|
|
1
|
+
/* v8 ignore start */ import { parser } from "@node-cli/parser";
|
|
2
2
|
import kleur from "kleur";
|
|
3
|
-
/*
|
|
3
|
+
/* v8 ignore next 67 */ export const config = parser({
|
|
4
4
|
meta: import.meta,
|
|
5
5
|
examples: [
|
|
6
6
|
{
|
|
@@ -55,6 +55,6 @@ import kleur from "kleur";
|
|
|
55
55
|
}
|
|
56
56
|
],
|
|
57
57
|
usage: "secret [options] [input] [output]"
|
|
58
|
-
});
|
|
58
|
+
}); /* v8 ignore stop */
|
|
59
59
|
|
|
60
60
|
//# sourceMappingURL=parse.js.map
|
package/dist/parse.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/parse.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../src/parse.ts"],"sourcesContent":["/* v8 ignore start */\nimport { parser } from \"@node-cli/parser\";\nimport kleur from \"kleur\";\n\nexport type Flags = {\n\tdecrypt?: boolean;\n\tencrypt?: boolean;\n\thelp?: boolean;\n\tversion?: boolean;\n};\n\nexport type Parameters = {\n\tinput?: string;\n\toutput?: string;\n};\n\nexport type Configuration = {\n\tflags?: Flags;\n\tparameters?: Parameters;\n};\n\n/* v8 ignore next 67 */\nexport const config: Configuration = parser({\n\tmeta: import.meta,\n\texamples: [\n\t\t{\n\t\t\tcommand: 'secret -e \"my-file.txt\" \"my-file.txt.enc\"',\n\t\t\tcomment:\n\t\t\t\t'## Encrypt the file \"my-file.txt\" and save the result in\\n ## \"my-file.txt.enc\" - password will be prompted',\n\t\t},\n\t\t{\n\t\t\tcommand: 'secret -d \"my-file.txt.enc\" \"my-file.txt\"',\n\t\t\tcomment: `## Decrypt the file \"my-file.txt.enc\" and save the\\n ## result in \"my-file.txt\" - password will be prompted`,\n\t\t},\n\t],\n\tflags: {\n\t\tdecrypt: {\n\t\t\tshortFlag: \"d\",\n\t\t\tdescription: `Decrypt a password protected file`,\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\tencrypt: {\n\t\t\tshortFlag: \"e\",\n\t\t\tdescription: `Encrypt a file with a password`,\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\thelp: {\n\t\t\tshortFlag: \"h\",\n\t\t\tdescription: \"Display help instructions\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\tversion: {\n\t\t\tshortFlag: \"v\",\n\t\t\tdescription: \"Output the current version\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t},\n\tparameters: {\n\t\tinput: {\n\t\t\tdescription: \"The file to encrypt or decrypt\",\n\t\t},\n\t\toutput: {\n\t\t\tdescription:\n\t\t\t\t\"The file to create in order to save the result of the encryption or decryption\",\n\t\t},\n\t},\n\trestrictions: [\n\t\t{\n\t\t\texit: 1,\n\t\t\tmessage: () =>\n\t\t\t\tkleur.red(\n\t\t\t\t\t`\\nError: one of --encrypt or --decrypt option must be provided.`,\n\t\t\t\t),\n\t\t\ttest: (x: { encrypt: boolean; decrypt: boolean }) =>\n\t\t\t\tx.encrypt === false && x.decrypt === false,\n\t\t},\n\t\t{\n\t\t\texit: 1,\n\t\t\tmessage: () =>\n\t\t\t\tkleur.red(\n\t\t\t\t\t`\\nError: either --encrypt or --decrypt option must be provided, but not both.`,\n\t\t\t\t),\n\t\t\ttest: (x: { encrypt: boolean; decrypt: boolean }) =>\n\t\t\t\tx.encrypt === true && x.decrypt === true,\n\t\t},\n\t],\n\n\tusage: \"secret [options] [input] [output]\",\n});\n/* v8 ignore stop */\n"],"names":["parser","kleur","config","meta","examples","command","comment","flags","decrypt","shortFlag","description","type","encrypt","help","version","parameters","input","output","restrictions","exit","message","red","test","x","usage"],"mappings":"AAAA,mBAAmB,GACnB,SAASA,MAAM,QAAQ,mBAAmB;AAC1C,OAAOC,WAAW,QAAQ;AAmB1B,qBAAqB,GACrB,OAAO,MAAMC,SAAwBF,OAAO;IAC3CG,MAAM;IACNC,UAAU;QACT;YACCC,SAAS;YACTC,SACC;QACF;QACA;YACCD,SAAS;YACTC,SAAS,CAAC,8GAA8G,CAAC;QAC1H;KACA;IACDC,OAAO;QACNC,SAAS;YACRC,WAAW;YACXC,aAAa,CAAC,iCAAiC,CAAC;YAChDC,MAAM;QACP;QACAC,SAAS;YACRH,WAAW;YACXC,aAAa,CAAC,8BAA8B,CAAC;YAC7CC,MAAM;QACP;QACAE,MAAM;YACLJ,WAAW;YACXC,aAAa;YACbC,MAAM;QACP;QACAG,SAAS;YACRL,WAAW;YACXC,aAAa;YACbC,MAAM;QACP;IACD;IACAI,YAAY;QACXC,OAAO;YACNN,aAAa;QACd;QACAO,QAAQ;YACPP,aACC;QACF;IACD;IACAQ,cAAc;QACb;YACCC,MAAM;YACNC,SAAS,IACRnB,MAAMoB,GAAG,CACR,CAAC,+DAA+D,CAAC;YAEnEC,MAAM,CAACC,IACNA,EAAEX,OAAO,KAAK,SAASW,EAAEf,OAAO,KAAK;QACvC;QACA;YACCW,MAAM;YACNC,SAAS,IACRnB,MAAMoB,GAAG,CACR,CAAC,6EAA6E,CAAC;YAEjFC,MAAM,CAACC,IACNA,EAAEX,OAAO,KAAK,QAAQW,EAAEf,OAAO,KAAK;QACtC;KACA;IAEDgB,OAAO;AACR,GAAG,CACH,kBAAkB"}
|
package/dist/secret.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
/*
|
|
2
|
+
/* v8 ignore start */ import { displayConfirmation, displayPromptWithPassword, logger, processFileWithPassword, shouldContinue } from "./utilities.js";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import fs from "fs-extra";
|
|
5
5
|
import { config } from "./parse.js";
|
|
@@ -42,6 +42,6 @@ try {
|
|
|
42
42
|
}
|
|
43
43
|
} catch (error) {
|
|
44
44
|
logger.error(error);
|
|
45
|
-
}
|
|
45
|
+
} /* v8 ignore stop */
|
|
46
46
|
|
|
47
47
|
//# sourceMappingURL=secret.js.map
|
package/dist/secret.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/secret.ts"],"sourcesContent":["#!/usr/bin/env node\n/*
|
|
1
|
+
{"version":3,"sources":["../src/secret.ts"],"sourcesContent":["#!/usr/bin/env node\n/* v8 ignore start */\n\nimport {\n\tdisplayConfirmation,\n\tdisplayPromptWithPassword,\n\tlogger,\n\tprocessFileWithPassword,\n\tshouldContinue,\n} from \"./utilities.js\";\n\nimport path from \"node:path\";\nimport fs from \"fs-extra\";\nimport { config } from \"./parse.js\";\n\nconst ENCRYPT = \"encrypt\";\nconst DECRYPT = \"decrypt\";\n\n/**\n * Caching the \"action\" for future usage (encrypt or decrypt).\n */\nconst actionName = config.flags.encrypt ? ENCRYPT : DECRYPT;\n\n/**\n * Extracting the input and output files.\n */\nlet inputFile: string,\n\toutputFile: string,\n\toutputFileExists: boolean = false;\nif (Object.entries(config.parameters).length > 0) {\n\tinputFile = config.parameters[\"0\"];\n\toutputFile = config.parameters[\"1\"];\n\tif (!fs.existsSync(inputFile)) {\n\t\tlogger.printErrorsAndExit([`File \"${inputFile}\" does not exist!`], 1);\n\t}\n\tif (fs.existsSync(outputFile)) {\n\t\toutputFileExists = true;\n\t}\n}\n\nif (outputFileExists) {\n\tconst goodToGo = await displayConfirmation(\n\t\t`The file ${outputFile} already exists, overwrite it?`,\n\t);\n\tshouldContinue(goodToGo);\n}\n\nconst password = await displayPromptWithPassword(\n\t`Enter password to ${actionName} the file`,\n);\n\ntry {\n\tawait processFileWithPassword({\n\t\tencode: config.flags.encrypt,\n\t\tinput: inputFile,\n\t\toutput: outputFile,\n\t\tpassword,\n\t});\n\tlogger.log();\n\tlogger.info(`File ${path.basename(inputFile)} was ${actionName}ed.`);\n\tif (outputFileExists) {\n\t\tlogger.info(`The result was saved in the file ${outputFile}`);\n\t}\n} catch (error) {\n\tlogger.error(error);\n}\n/* v8 ignore stop */\n"],"names":["displayConfirmation","displayPromptWithPassword","logger","processFileWithPassword","shouldContinue","path","fs","config","ENCRYPT","DECRYPT","actionName","flags","encrypt","inputFile","outputFile","outputFileExists","Object","entries","parameters","length","existsSync","printErrorsAndExit","goodToGo","password","encode","input","output","log","info","basename","error"],"mappings":";AACA,mBAAmB,GAEnB,SACCA,mBAAmB,EACnBC,yBAAyB,EACzBC,M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A,kBAAkB"}
|
package/dist/utilities.js
CHANGED
|
@@ -19,7 +19,7 @@ export const processFileWithPassword = async (options)=>{
|
|
|
19
19
|
logger.log(fileProcessor(password, data));
|
|
20
20
|
}
|
|
21
21
|
};
|
|
22
|
-
/*
|
|
22
|
+
/* v8 ignore next 12 */ export const displayConfirmation = async (message)=>{
|
|
23
23
|
const questions = {
|
|
24
24
|
default: true,
|
|
25
25
|
message: message || "Do you want to continue?",
|
|
@@ -30,7 +30,7 @@ export const processFileWithPassword = async (options)=>{
|
|
|
30
30
|
const answers = await inquirer.prompt(questions);
|
|
31
31
|
return answers.goodToGo;
|
|
32
32
|
};
|
|
33
|
-
/*
|
|
33
|
+
/* v8 ignore next 16 */ export const displayPromptWithPassword = async (message)=>{
|
|
34
34
|
const questions = {
|
|
35
35
|
message: message,
|
|
36
36
|
name: "password",
|
|
@@ -45,7 +45,7 @@ export const processFileWithPassword = async (options)=>{
|
|
|
45
45
|
const answers = await inquirer.prompt(questions);
|
|
46
46
|
return answers.password;
|
|
47
47
|
};
|
|
48
|
-
/*
|
|
48
|
+
/* v8 ignore next 8 */ export const shouldContinue = (goodToGo)=>{
|
|
49
49
|
if (!goodToGo) {
|
|
50
50
|
logger.log("\nBye then!");
|
|
51
51
|
// eslint-disable-next-line unicorn/no-process-exit
|
package/dist/utilities.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utilities.ts"],"sourcesContent":["import { Logger } from \"@node-cli/logger\";\nimport fs from \"fs-extra\";\nimport inquirer from \"inquirer\";\n\nimport { decrypt, encrypt } from \"./lib.js\";\n\nexport const logger = new Logger({\n\tboring: process.env.NODE_ENV === \"test\",\n});\n\nconst UTF8 = \"utf8\";\nconst DEFAULT_FILE_ENCODING = UTF8;\n\n/**\n * Process a file with a given password. The file can be\n * encoded or decoded depending on the `encode` flag.\n * @param {Boolean} encode whether to encode or decode the file\n * @param {String} input the input file path\n * @param {String} [output] the output file path\n * @param {String} password the password to use\n * @return {Promise} a promise that resolves when\n * the file has been processed.\n */\nexport type ProcessFileOptions = {\n\tencode: boolean;\n\tinput: string;\n\toutput?: string;\n\tpassword: string;\n};\nexport const processFileWithPassword = async (\n\toptions: ProcessFileOptions,\n): Promise<void> => {\n\tconst { encode, input, output, password } = options;\n\tconst fileProcessor = encode ? encrypt : decrypt;\n\tconst data = await fs.readFile(input, DEFAULT_FILE_ENCODING);\n\n\tif (output) {\n\t\t// Save data to output file\n\t\tawait fs.outputFile(output, fileProcessor(password, data));\n\t} else {\n\t\t// Print to stdout directly\n\t\tlogger.log(fileProcessor(password, data));\n\t}\n};\n\n/*
|
|
1
|
+
{"version":3,"sources":["../src/utilities.ts"],"sourcesContent":["import { Logger } from \"@node-cli/logger\";\nimport fs from \"fs-extra\";\nimport inquirer from \"inquirer\";\n\nimport { decrypt, encrypt } from \"./lib.js\";\n\nexport const logger = new Logger({\n\tboring: process.env.NODE_ENV === \"test\",\n});\n\nconst UTF8 = \"utf8\";\nconst DEFAULT_FILE_ENCODING = UTF8;\n\n/**\n * Process a file with a given password. The file can be\n * encoded or decoded depending on the `encode` flag.\n * @param {Boolean} encode whether to encode or decode the file\n * @param {String} input the input file path\n * @param {String} [output] the output file path\n * @param {String} password the password to use\n * @return {Promise} a promise that resolves when\n * the file has been processed.\n */\nexport type ProcessFileOptions = {\n\tencode: boolean;\n\tinput: string;\n\toutput?: string;\n\tpassword: string;\n};\nexport const processFileWithPassword = async (\n\toptions: ProcessFileOptions,\n): Promise<void> => {\n\tconst { encode, input, output, password } = options;\n\tconst fileProcessor = encode ? encrypt : decrypt;\n\tconst data = await fs.readFile(input, DEFAULT_FILE_ENCODING);\n\n\tif (output) {\n\t\t// Save data to output file\n\t\tawait fs.outputFile(output, fileProcessor(password, data));\n\t} else {\n\t\t// Print to stdout directly\n\t\tlogger.log(fileProcessor(password, data));\n\t}\n};\n\n/* v8 ignore next 12 */\nexport const displayConfirmation = async (message: string) => {\n\tconst questions = {\n\t\tdefault: true,\n\t\tmessage: message || \"Do you want to continue?\",\n\t\tname: \"goodToGo\",\n\t\ttype: \"confirm\",\n\t};\n\tlogger.log();\n\tconst answers = await inquirer.prompt(questions);\n\treturn answers.goodToGo;\n};\n\n/* v8 ignore next 16 */\nexport const displayPromptWithPassword = async (message: string) => {\n\tconst questions = {\n\t\tmessage: message,\n\t\tname: \"password\",\n\t\ttype: \"password\",\n\t\tvalidate(value: string) {\n\t\t\tif (!value) {\n\t\t\t\treturn \"Password cannot be empty...\";\n\t\t\t}\n\t\t\treturn true;\n\t\t},\n\t};\n\tconst answers = await inquirer.prompt(questions);\n\treturn answers.password;\n};\n\n/* v8 ignore next 8 */\nexport const shouldContinue = (goodToGo: boolean) => {\n\tif (!goodToGo) {\n\t\tlogger.log(\"\\nBye then!\");\n\t\t// eslint-disable-next-line unicorn/no-process-exit\n\t\tprocess.exit(0);\n\t}\n\treturn true;\n};\n"],"names":["Logger","fs","inquirer","decrypt","encrypt","logger","boring","process","env","NODE_ENV","UTF8","DEFAULT_FILE_ENCODING","processFileWithPassword","options","encode","input","output","password","fileProcessor","data","readFile","outputFile","log","displayConfirmation","message","questions","default","name","type","answers","prompt","goodToGo","displayPromptWithPassword","validate","value","shouldContinue","exit"],"mappings":"AAAA,SAASA,MAAM,QAAQ,mBAAmB;AAC1C,OAAOC,QAAQ,WAAW;AAC1B,OAAOC,cAAc,WAAW;AAEhC,SAASC,OAAO,EAAEC,OAAO,QAAQ,WAAW;AAE5C,OAAO,MAAMC,SAAS,IAAIL,OAAO;IAChCM,QAAQC,QAAQC,GAAG,CAACC,QAAQ,KAAK;AAClC,GAAG;AAEH,MAAMC,OAAO;AACb,MAAMC,wBAAwBD;AAkB9B,OAAO,MAAME,0BAA0B,OACtCC;IAEA,MAAM,EAAEC,MAAM,EAAEC,KAAK,EAAEC,MAAM,EAAEC,QAAQ,EAAE,GAAGJ;IAC5C,MAAMK,gBAAgBJ,SAASV,UAAUD;IACzC,MAAMgB,OAAO,MAAMlB,GAAGmB,QAAQ,CAACL,OAAOJ;IAEtC,IAAIK,QAAQ;QACX,2BAA2B;QAC3B,MAAMf,GAAGoB,UAAU,CAACL,QAAQE,cAAcD,UAAUE;IACrD,OAAO;QACN,2BAA2B;QAC3Bd,OAAOiB,GAAG,CAACJ,cAAcD,UAAUE;IACpC;AACD,EAAE;AAEF,qBAAqB,GACrB,OAAO,MAAMI,sBAAsB,OAAOC;IACzC,MAAMC,YAAY;QACjBC,SAAS;QACTF,SAASA,WAAW;QACpBG,MAAM;QACNC,MAAM;IACP;IACAvB,OAAOiB,GAAG;IACV,MAAMO,UAAU,MAAM3B,SAAS4B,MAAM,CAACL;IACtC,OAAOI,QAAQE,QAAQ;AACxB,EAAE;AAEF,qBAAqB,GACrB,OAAO,MAAMC,4BAA4B,OAAOR;IAC/C,MAAMC,YAAY;QACjBD,SAASA;QACTG,MAAM;QACNC,MAAM;QACNK,UAASC,KAAa;YACrB,IAAI,CAACA,OAAO;gBACX,OAAO;YACR;YACA,OAAO;QACR;IACD;IACA,MAAML,UAAU,MAAM3B,SAAS4B,MAAM,CAACL;IACtC,OAAOI,QAAQZ,QAAQ;AACxB,EAAE;AAEF,oBAAoB,GACpB,OAAO,MAAMkB,iBAAiB,CAACJ;IAC9B,IAAI,CAACA,UAAU;QACd1B,OAAOiB,GAAG,CAAC;QACX,mDAAmD;QACnDf,QAAQ6B,IAAI,CAAC;IACd;IACA,OAAO;AACR,EAAE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@node-cli/secret",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"description": "Secret is a CLI tool that can encode or decode a file with a password",
|
|
@@ -19,19 +19,23 @@
|
|
|
19
19
|
"build:types": "tsc",
|
|
20
20
|
"clean": "rimraf dist types coverage",
|
|
21
21
|
"lint": "biome lint src",
|
|
22
|
-
"test": "
|
|
23
|
-
"test:coverage": "
|
|
22
|
+
"test": "vitest run --globals",
|
|
23
|
+
"test:coverage": "vitest run --coverage --globals",
|
|
24
24
|
"test:watch": "npm run test -- --watch",
|
|
25
25
|
"watch": "swc --strip-leading-paths --watch --out-dir dist src"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@node-cli/logger": "1.
|
|
29
|
-
"@node-cli/parser": "2.
|
|
30
|
-
"fs-extra": "11.
|
|
31
|
-
"inquirer": "9.3.
|
|
28
|
+
"@node-cli/logger": "1.3.1",
|
|
29
|
+
"@node-cli/parser": "2.4.1",
|
|
30
|
+
"fs-extra": "11.3.0",
|
|
31
|
+
"inquirer": "9.3.7"
|
|
32
32
|
},
|
|
33
33
|
"publishConfig": {
|
|
34
34
|
"access": "public"
|
|
35
35
|
},
|
|
36
|
-
"
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@vitest/coverage-v8": "3.2.4",
|
|
38
|
+
"vitest": "3.2.4"
|
|
39
|
+
},
|
|
40
|
+
"gitHead": "2cef8f88aa5316a1789caad2bd7327ca908ccb9f"
|
|
37
41
|
}
|