{"id":19889531,"url":"https://github.com/tsmx/string-crypto","last_synced_at":"2026-02-22T22:04:15.483Z","repository":{"id":57168115,"uuid":"303189184","full_name":"tsmx/string-crypto","owner":"tsmx","description":"Easy encryption and decryption for strings in NodeJS.","archived":false,"fork":false,"pushed_at":"2024-06-18T19:52:58.000Z","size":586,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-04T07:03:05.378Z","etag":null,"topics":["aes","cipheriv","crypto","cryptography","encryption","string"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tsmx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-11T18:43:52.000Z","updated_at":"2024-06-18T19:53:01.000Z","dependencies_parsed_at":"2023-01-30T02:00:45.021Z","dependency_job_id":"110c6361-b7a9-4086-a0b4-404302b3e3f4","html_url":"https://github.com/tsmx/string-crypto","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsmx%2Fstring-crypto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsmx%2Fstring-crypto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsmx%2Fstring-crypto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsmx%2Fstring-crypto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tsmx","download_url":"https://codeload.github.com/tsmx/string-crypto/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224064848,"owners_count":17249684,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aes","cipheriv","crypto","cryptography","encryption","string"],"created_at":"2024-11-12T18:10:38.715Z","updated_at":"2026-02-22T22:04:10.441Z","avatar_url":"https://github.com/tsmx.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [**@tsmx/string-crypto**](https://github.com/tsmx/string-crypto)\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n![npm (scoped)](https://img.shields.io/npm/v/@tsmx/string-crypto)\n![node-current (scoped)](https://img.shields.io/node/v/@tsmx/string-crypto)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/tsmx/string-crypto/git-build.yml?branch=master)](https://img.shields.io/github/actions/workflow/status/tsmx/string-crypto/git-build.yml?branch=master)\n[![Coverage Status](https://coveralls.io/repos/github/tsmx/string-crypto/badge.svg?branch=master)](https://coveralls.io/github/tsmx/string-crypto?branch=master)\n\n\u003e Encrypt and decrypt strings.\n\n## Usage\n\n### Key passed via environment variable\n\n```js\nconst sc = require('@tsmx/string-crypto');\n\nlet mySecret = 'My secret string';\n\nlet encrypted = sc.encrypt(mySecret);\n// '28bedae6f6497f68abe403fb88df340e|2071d6458...'\n\nlet decrypted = sc.decrypt(encrypted); \n// 'My secret string'\n\n```\n\n### Key passed directly via options\n\n```js\nconst sc = require('@tsmx/string-crypto');\n\nlet mySecret = 'My secret string';\n\nlet encrypted = sc.encrypt(mySecret, { key: '0123456789qwertzuiopasdfghjklyxc' });\n// 'ba7bbb57674a198ad6cb7ff65801f9c9|a49cff4c9...'\n\nlet decrypted = sc.decrypt(encrypted, { key: '0123456789qwertzuiopasdfghjklyxc' }); \n// 'My secret string'\n```\n\n## API\n\n### encrypt(value, options = null)\n\nEncrypts `value` and returns the encrypted string. The key for encryption is taken from `options.key` or the environment variable `ENCRYPTION_KEY` if no options are present.\n\n#### value\n\nType: `String`\n\nThe string that should be encrypted.\n\n#### options\n\nType: `Object`\nDefault: `null`\n\nObject containing the supported options for encryption. Please also refer to the [notes](#notes).\n\n```js\noptions = {\n    key: 'YOUR KEY HERE',\n    passNull: false\n};\n```\n\n##### options.key\n\nType: `String`\nDefault: `null`\n\nThe key used for encryption. If not present, the key is retrieved from the environment variable `ENCRYPTION_KEY`.\n\n##### options.passNull\n\nType: `Boolean`\nDefault: `false`\n\nSometimes it is helpful to let a value of `null` pass the encryption though `null` can't be encrypted either. If set to `true` the decrypt function will return `null` if value is `null`. Defaults to `false`, then an exception is thrown if the passed value is `null`.\n\n```js\nsc.encrypt(null); // throwing Error\nsc.encrypt(null,  { passNull: true }); // null\n```\n\n### decrypt(value, options = null)\n\nDecrypts `value` and returns the decrypted string. The key for decryption is taken from `options.key` or the environment variable `ENCRYPTION_KEY` if no options are present.\n\n#### value\n\nType: `String`\n\nThe string that should be decrypted. Must be in the form that `encrypt` puts out.\n\n#### options\n\nType: `Object`\nDefault: `null`\n\nObject containing the supported options for decryption. Please also refer to the [notes](#notes).\n\n```js\noptions = {\n    key: 'YOUR KEY HERE',\n    passNull: false\n};\n```\n\n##### options.key\n\nType: `String`\nDefault: `null`\n\nThe key used for decryption. If not present, the key is retrieved from the environment variable `ENCRYPTION_KEY`.\n\n##### options.passNull\n\nType: `Boolean`\nDefault: `false`\n\nSometimes it is helpful to let a value of `null` pass the decryption though `null` can't be decrypted either. If set to `true` the decrypt function will return `null` if value is `null`. Defaults to `false`, then an exception is thrown if the passed value is `null`.\n\n```js\nsc.decrypt(null); // throwing Error\nsc.decrypt(null,  { passNull: true }); // null\n```\n\n## Notes\n\nSimple helper package to encrypt and decrypt string based on standard NodeJS Crypto functions.\n- Used cipher: AES-256-CBC with initialization vector (`crypto.createCipheriv`)\n- IV generation with `crypto.randomBytes`\n- Key length must be 32 bytes. The key can be provided as\n    - a string of 32 characters length, or\n    - a hexadecimal value of 64 characters length (= 32 bytes)\n- If no key is directly passed via `options.key` it is retrieved from the environment variable `ENCRYPTION_KEY`.\n- Result: string containing the initialization vector and the encrypted value separated by `'|'`","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsmx%2Fstring-crypto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftsmx%2Fstring-crypto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsmx%2Fstring-crypto/lists"}