{"id":27443051,"url":"https://github.com/log1997/gm-log","last_synced_at":"2025-04-15T01:18:45.035Z","repository":{"id":196805892,"uuid":"697174147","full_name":"LOG1997/gm-log","owner":"LOG1997","description":"国密密钥交换算法","archived":false,"fork":false,"pushed_at":"2024-01-25T09:51:20.000Z","size":1190,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-15T01:18:27.931Z","etag":null,"topics":["ecdh","encryption","encryption-decryption","gm","gmssl","sm2","sm2-encryption","sm4"],"latest_commit_sha":null,"homepage":"https://log1997.github.io/gm-log/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LOG1997.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-27T07:51:01.000Z","updated_at":"2024-01-24T09:10:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"fa0105d6-f175-4438-8b3e-2827131284ad","html_url":"https://github.com/LOG1997/gm-log","commit_stats":null,"previous_names":["log1997/gm-log"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LOG1997%2Fgm-log","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LOG1997%2Fgm-log/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LOG1997%2Fgm-log/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LOG1997%2Fgm-log/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LOG1997","download_url":"https://codeload.github.com/LOG1997/gm-log/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248986316,"owners_count":21194025,"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":["ecdh","encryption","encryption-decryption","gm","gmssl","sm2","sm2-encryption","sm4"],"created_at":"2025-04-15T01:18:44.226Z","updated_at":"2025-04-15T01:18:45.021Z","avatar_url":"https://github.com/LOG1997.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 国密密钥交换JavaScript实现\n\n该方法是使用国密进行密钥交换算法的js版本实现。实现方法详见[示例项目](./examples/react-project/src/App.tsx)。\n\n步骤：\n1. 安装依赖包\n\n```bash\nnpm i gmlog\n\nor\n\npnpm i gmlog\n```\n\n2. 导入使用\n\n```typescript\nimport { SM2 } from 'gmlog'\n```\n\n3. 初始化\n\n设置初始点坐标\n\n```typescript\np = 0x8542d69e4c044f18e8b92435bf6ff7de457283915c45517d722edb8b08f1dfc3n\na = 0x787968b4fa32c3fd2417842e73bbfeff2f3c848b6831d7e0ec65228b3937e498n\nb = 0x63e4c6d3b23b0c849cf84241484bfe48f61d59a5b16ba06e6e12d1da27c5249an\nn = 0x8542d69e4c044f18e8b92435bf6ff7dd297720630485628d5ae74ee7c32e79b7n\nG = [0x421debd61b62eab6746434ebc3cc315e32220b3badd50bdc4c4e6c147fedd43dn,                0x0680512bcbb42c07d47349d2153b70c4e5d7fdfcbfa36ea1a85841b9e46e09a2n]\nh = 1n\n```\n\n设置身份(也可以为空)\n\n```typescipt\nIDA='ALICE123@YAHOO.COM'\nIDB='BOB123@YAHOO.COM'\n```\n\n将上面设置的椭圆曲线上的点赋值给导入的`SM2`对象。\n\n```typescript\n    const curveParam: any[] = [\n        p,\n        a,\n        b,\n        n,\n        G,\n        h,\n    ];\n    const sm = new SM2(...curveParam);\n\n// 生成SMA\nconst smA = new SM2(...curveParam, IDA as any, privateKeyA, publicKeyA as any);\n```\n\n4. 生成密钥对\n\nA要完成密钥协商需要A的密钥对和B的临时密钥对。同理，B要完成密钥协商也要A的临时密钥对和B的密钥对。以A为例，在A这侧需要生成两个密钥对。\n生成方式使用`sm.genKeyPair()`方法。\n生成临时密钥使用`sm.agreement_initiate()`方法。\n\n在获得临时密钥后，将临时密钥的和正式密钥的公钥都发送给B，同理A收到B的临时密钥和正式密钥的公钥后即可以开始密钥协商。\n\n```typescript\n  const { res: res1, content: content1 } = smA.agreement_confirm(\n            rKeyA.privateKey as bigint, //A的临时密钥私钥\n            rKeyA.publicKey as { x: bigint, y: bigint }, // A的临时密钥公钥\n            rKeyB.publicKey as { x: bigint, y: bigint }, // b的临时密钥公钥\n            publicKeyB as { x: bigint, y: bigint }, //B的正式密钥公钥\n            IDB, // B的ID 可为空\n            null,\n            option // 选项 可为空，取值为bool值\n        );\n```\n\n5. 获取密钥协商后的密钥\n\n```typescript\n let KA = null, // 密钥协商获取的密钥\n            SA = null;\n        if (option) {\n            KA = content1[0];\n            SA = content1[1];\n        } else {\n            KA = content1[0];\n        }\n```\n\n\u003e {x:bigint,y:bigint}类型的密钥类型转化为十六进制字符串使用toString(16)方法。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flog1997%2Fgm-log","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flog1997%2Fgm-log","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flog1997%2Fgm-log/lists"}