{"id":20347292,"url":"https://github.com/herculesinc/pohlig-hellman","last_synced_at":"2025-03-04T16:12:52.792Z","repository":{"id":100237194,"uuid":"128727238","full_name":"herculesinc/pohlig-hellman","owner":"herculesinc","description":"Pohlig-Hellman cipher for Node.js","archived":false,"fork":false,"pushed_at":"2018-04-21T05:21:37.000Z","size":135,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-18T12:49:43.890Z","etag":null,"topics":["encryption","encryption-algorithm"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/herculesinc.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":"2018-04-09T06:49:02.000Z","updated_at":"2021-06-27T04:11:39.000Z","dependencies_parsed_at":"2023-05-13T02:15:29.333Z","dependency_job_id":null,"html_url":"https://github.com/herculesinc/pohlig-hellman","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/herculesinc%2Fpohlig-hellman","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/herculesinc%2Fpohlig-hellman/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/herculesinc%2Fpohlig-hellman/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/herculesinc%2Fpohlig-hellman/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/herculesinc","download_url":"https://codeload.github.com/herculesinc/pohlig-hellman/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240881334,"owners_count":19872725,"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":["encryption","encryption-algorithm"],"created_at":"2024-11-14T22:16:01.344Z","updated_at":"2025-03-04T16:12:52.720Z","avatar_url":"https://github.com/herculesinc.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pohlig-hellman\n\nPohlig-Hellman cipher for node.js written in TypeScript.\n\n## Install\n```sh\n$ npm install pohlig-hellman --save\n```\n\n## Example\n```JavaScript\nimport * as ph from 'pohlig-hellman';\n\nconst data = 'something to encrypt';\n\n// create a cipher with a 2048-bit prime and a 256-bit random key\nconst cipher = await ph.createCipher();\n\nconst encrypted = cipher.encrypt(data);\nconst decrypted = cipher.decrypt(encrypted).toString();\n\nconsole.log(data === decrypted); // true\n```\n\n# API\nFull API defintions are available in the [pohlig-hellman.d.ts](https://github.com/herculesinc/pohlig-hellman/blob/master/pohlig-hellman.d.ts) file available.\n\n## Creating a Cipher\nIf you have a prime and an encryption key you'd like to create a cipher with, you can use the cipher constructor directly.\n```TypeScript\nconstructor(prime: Buffer, enkey: Buffer);\n```\nAs shown above, both parameters must be in Buffer form.\n\nTo initialize the cipher with a random key, `createCipher()` function can be used. This function has the following signatures:\n```TypeScript\ncreateCipher()                      : Promise\u003cCipher\u003e;\ncreateCipher(prime: Buffer)         : Promise\u003cCipher\u003e;\ncreateCipher(modpGroup: string)     : Promise\u003cCipher\u003e;\ncreateCipher(primeLength: number)   : Promise\u003cCipher\u003e;\n```\nThe method works as follows:\n* If no parameters are provided, the cipher is initialized with a 2048-bit prime from [RFC 3526](https://tools.ietf.org/html/rfc3526) and a random 256-bit encryption key.\n* If a buffer is provide it is assumed to be a prime, and and the cipher is initialized with that prime and a random key appropriate for the specified prime.\n* If a string is provided, it is assumed to be a name of a group from [RFC 3526](https://tools.ietf.org/html/rfc3526). The valid group names are: `modp2048`, `modp3072`, `modp4096`, `modp6144`, `modp8192`. The cipher is initialized with the specified prime and a random 256-bit key\n* If a number is provided, it is assumed to be the length of a desired prime in bits. The cipher is initialized with a random prime of the specified length and a random key appropriate for that prime.\n\nBecause prime and key generation are done asynchronously, the function returns a promise for the cipher.\n\n## Encrypting and Decrypting\nOnce a cipher is created you can use it to encrypt and decrypt data using the following methods:\n\n```TypeScript\n// encrypting the data\nencrypt(data: Buffer): Buffer;\nencrypt(data: string, encoding = 'utf8'): Buffer; // encoding can also be 'hex' or 'base64'\n\n// derypting the data\ndecrypt(data: Buffer): Buffer;\ndecrypt(data: string, encoding = 'hex'): Buffer; // encoding can also be 'base64'\n```\n\n## Merging Keys\nOne of the nice properties of Pohlig-Hellman chiper is that it allows key merging. This can be accomplished using `mergeKeys()` function. For example:\n\n```TypeScript\nimport * as ph from 'pohlig-hellman';\n\nconst data = 'something to encrypt';\n\n// create a cipher and encrypt the data with a random key\nconst cipher1 = await ph.createCipher();\nconst encrypted1 = cipher1.encrypt(data);\n\n// encrypt the data with a different random key\nconst cipher2 = await ph.createCipher(cipher1.prime);\nconst encrypted2 = cipher2.encrypt(encrypted1);\n\n// merge the keys from the 2 ciphers, and decrypt with it\nconst mergedKey = ph.mergeKeys(cipher1.enkey, cipher2.enkey);\nconst cipher3 = new ph.Cipher(cipher1.prime, mergedKey);\nconst decrypted = cipher3.decrypt(encrypted2).toString();\n\nconsole.log(data === decrypted);\n\n```\n\n# License\nCopyright (c) 2018 Credo360, Inc.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fherculesinc%2Fpohlig-hellman","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fherculesinc%2Fpohlig-hellman","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fherculesinc%2Fpohlig-hellman/lists"}