Ed25519 验证Key2020 链接数据密钥对等库 (@数字证书/ed25519-核查-钥匙-2020)
Typescript/Javascript 库,用于生成并配合 Ed25519VerificationKey2020 密钥对,用于加密-ld.
目录
背景情况
(从 digitalbazaar/ed25519-verification-key-2020 页:1
以提供类型脚本兼容性。 )
用于:
@digitalcredentials/ed25519-signature-2020^2.0.0密码套件( 有linked-data-integrity(中文(简体) )@digitalcredentials/vc^2.0.0
另见(相关规格):
警卫
同大多数与安全和密码学有关的工具一样 您的系统将在很大程度上取决于您的设计决定 .
安装
- 需要节点.js 20+.
在当地安装(用于开发):
git clone https://github.com/digitalcredentials/ed25519-verification-key-2020.git
cd ed25519-verification-key-2020
npm install
使用量
生成新的公用/私用密钥对
要生成一个新的公/私密密钥配对 :
{string} [controller]可选控制器 URI 或 Did 初始化 生成了密钥。 (此会输入密钥 ID. ){string} [seed]可选的确定种子值,用于生成 键.
import {Ed25519VerificationKey2020} from '@digitalcredentials/ed25519-verification-key-2020';
const edKeyPair = await Ed25519VerificationKey2020.generate();
从存储导入一对密钥
从下列数据中创建公/私密密钥对实例:
存储,使用 .from()数字 :
const serializedKeyPair = { ... };
const keyPair = await Ed25519VerificationKey2020.from(serializedKeyPair);
只导出公钥
要导出一对的公钥 :
await keyPair.export({publicKey: true});
// ->
{
type: 'Ed25519VerificationKey2020',
id: 'did:example:1234#z6MkszZtxCmA2Ce4vUV132PCuLQmwnaDD5mw2L23fGNnsiX3',
controller: 'did:example:1234',
publicKeyMultibase: 'zEYJrMxWigf9boyeJMTRN4Ern8DJMoCXaLK77pzQmxVjf'
}
导出完整的公-私密密钥配对
要导出完整一对密钥, 包括私钥( 警告 : 这应该是 仔细考虑的操作,最好留给专用密钥管理系统:
await keyPair.export({publicKey: true, privateKey: true});
// ->
{
type: 'Ed25519VerificationKey2020',
id: 'did:example:1234#z6MkszZtxCmA2Ce4vUV132PCuLQmwnaDD5mw2L23fGNnsiX3',
controller: 'did:example:1234',
publicKeyMultibase: 'zEYJrMxWigf9boyeJMTRN4Ern8DJMoCXaLK77pzQmxVjf',
privateKeyMultibase: 'z4E7Q4neNHwv3pXUNzUjzc6TTYspqn9Aw6vakpRKpbVrCzwKWD4hQDHnxuhfrTaMjnR8BTp9NeUvJiwJoSUM6xHAZ'
}
生成和验证密钥指纹
要生成指纹 :
keyPair.fingerprint();
// ->
'z6MkszZtxCmA2Ce4vUV132PCuLQmwnaDD5mw2L23fGNnsiX3'
为了验证指纹:
const fingerprint = 'z6MkszZtxCmA2Ce4vUV132PCuLQmwnaDD5mw2L23fGNnsiX3';
keyPair.verifyFingerprint({fingerprint});
// ->
{verified: true}
创建签名函数
为了执行加密签名,您需要创建 sign
函数,然后引用它.
const keyPair = Ed25519VerificationKey2020.generate();
const {sign} = keyPair.signer();
// data is a Uint8Array of bytes
const data = (new TextEncoder()).encode('test data goes here');
// Signing also outputs a Uint8Array, which you can serialize to text etc.
const signatureValueBytes = await sign({data});
创建验证函数
为了验证密码签名,您需要创建 verify
函数,然后引用它(通过数据验证,和签名).
const keyPair = Ed25519VerificationKey2020.generate();
const {verify} = keyPair.verifier();
const verified = await verify({data, signature});
// true
转换为和从以前的 Ed25519 验证Key2018 密钥类型
如果您已经序列化并存储了上一个密钥
Ed25519VerificationKey2018 密钥类型(例如,使用
页:1 ed25519-verification-key-2018(中文(简体) )
库,或使用 Ed25519KeyPair 捆绑的密钥 crypto-ld v3.x, (中文(简体) ),
记住的事情:
- 这些关键类型的实例仍然包含同样的关键材料,即:
从2018年套房到2020年套房的改变 就是公众的方式
和私人关键材料在出口时进行序列化。 2018套房密钥
类型
publicKeyBase58财务报告和审定财务报表privateKeyBase58属性, 和 2020 套接字密钥( 此回波) 使用相应的publicKeyMultibase财务报告和审定财务报表privateKeyMultibase属性. - 您可以使用 2018 密钥类型转换为 2020 密钥类型
Ed25519VerificationKey2020.fromEd25519VerificationKey2018()方法(见下文). - 他们
generate()相同的关键材料,考虑到相同的seed参数. - 2018年和2020年的密钥都产生并验证了相同的签名.
2018年转换实例:
import {Ed25519VerificationKey2018}
from '@digitalbazaar/ed25519-verification-key-2018';
import {Ed25519VerificationKey2020}
from '@digitalbazaar/ed25519-verification-key-2020';
const keyPair2018 = await Ed25519VerificationKey2018.generate({
controller: 'did:example:1234'
});
const keyPair2020 = await Ed25519VerificationKey2020
.fromEd25519VerificationKey2018({keyPair: keyPair2018});
// The resulting keyPair2020 will have the same `id` and `controller` properties
// as its 2018 source. They will also produce and verify the same signatures.
// data is a Uint8Array of bytes
const data = (new TextEncoder()).encode('test data goes here');
const signatureBytes2018 = await keyPair2018.signer().sign({data});
// this is the same signature as that produced by the 2020 key. And will verify
// the same.
await keyPair2020.verifier().verify({data, signature: signatureBytes2018})
// true
转换到2018系列化的例子:
const keyPair2020 = await Ed25519VerificationKey2020.generate({
controller: 'did:example:1234'
});
const keyObject2018 = keyPair2020.toEd255519VerificationKey2018({
publicKey: true, privateKey: true, includeContext: true
});
贡献
公关接受.
请遵照 标准读取 规格.
许可证
- MIT许可证 - DCC - TypeScript兼容.
- 新BSD许可证 (3-clause) 2020-2021 Digital Bazaar - 初始实施.