{\fn黑体\fs22\bord1\shad0\3ahbe\4ah00\fscx67\fscy66\2chffffff\3ch808080}你这混蛋
大多数消费者想要 @e4a/pg-js
而不是直接用这个包包来包装这些捆绑
API,以及 @e4a/pg-wasm 是它的依赖.
此软件包会在每集发布到 npm pg-core 发布时间 pg-core是因为
版本,没有自己的发行周期。 对于一个特定的变化
版本,已读
pg-core更改日志
相同的版本编号.
此软件包包含自动生成的WebAssembly 绑定以调用 来自 Javascript 或 Typescript 的 PostGuard Rust 库。 这个图书馆一直 配置为通过捆绑器在浏览器中运行 .
这个 ReadableStream 财务报告和审定财务报表 WritableStream 需要网络API. 最值得注意的是,
WritableStream 在Firefox上不支持直到100版本,参见
可写入结构。 。 。 .
如果无法使用,请考虑使用多填充器,见 网络流-聚合。 。 。 .
使用量
见 实例 postguard-js
工作实例.
获取密钥
从 PKG 获取密钥( 用于解密/ 签名) 是最简单的
Yivi 前端软件包和自定义 session 字段.
// The URL of the PKG.
const PKG_URL = "...";
const KeySorts = {
Encryption: "key",
Signing: "sign/key",
};
export async function fetchKey(
sort,
keyRequest,
timestamp = undefined,
signingKeyRequest = undefined
) {
const session = {
url: PKG_URL,
start: {
url: (o) => `${o.url}/v2/request/start`,
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(keyRequest),
},
result: {
url: (o, { sessionToken }) => `${o.url}/v2/request/jwt/${sessionToken}`,
parseResponse: (r) => {
return r
.text()
.then((jwt) =>
fetch(
`${PKG_URL}/v2/irma/${sort}${
timestamp ? "/" + timestamp.toString() : ""
}`,
{
method: sort === KeySorts.Encryption ? "GET" : "POST",
headers: {
Authorization: `Bearer ${jwt}`,
"Content-Type": "application/json",
},
...(signingKeyRequest && {
body: JSON.stringify({ ...signingKeyRequest }),
}),
}
)
)
.then((r) => r.json())
.then((json) => {
if (json.status !== "DONE" || json.proofStatus !== "VALID")
throw new Error("not done and valid");
return sort === KeySorts.Encryption
? json.key
: { pubSignKey: json.pubSignKey, privSignKey: json.privSignKey };
})
.catch((e) => console.log("error: ", e));
},
},
};
const yivi = new YiviCore({ debugging: false, session });
yivi.use(YiviClient);
yivi.use(YiviPopup);
return yivi.start();
}
加密
// Load the WASM module.
const { sealStream } = await import("@e4a/pg-wasm");
// Retrieve the public key from PKG API:
const resp = await fetch(`${url}/v2/parameters`);
const pk = await resp.json().then((r) => r.publicKey);
// We provide the policies which we want to use for encryption.
const policy = {
Bob: {
ts: Math.round(Date.now() / 1000),
con: [{ t: "irma-demo.sidn-pbdf.email.email", v: "[email protected]" }],
},
};
// We provide the policies which we want to sign with.
// This policy is visible to everyone.
const pubSignId = [
{ t: "irma-demo.gemeente.personalData.fullname", v: "Alice" },
];
// This policy is only visible to recipients.
const privSignId = [{ t: "irma-demo.gemeente.personalData.bsn", v: "1234" }];
// We retrieve keys for these policies.
let { pubSignKey, privSignKey } = await fetchKey(
KeySorts.Signing,
{ con: [...pubSignId, ...privSignId] },
undefined,
{ pubSignId, privSignId }
);
const sealOptions = {
policy,
pubSignKey,
privSignKey,
};
// The following call reads data from a `ReadableStream` and seals it into `WritableStream`.
// Make sure that only chunks of type `Uint8Array` are enqueued to `readable`.
await sealStream(pk, sealOptions, readable, writable);
解密
// Load the WASM module.
const { StreamUnsealer } = await import("@e4a/pg-wasm");
// Retrieve to global verification key.
const vk = await fetch(`${PKG_URL}/v2/sign/parameters`)
.then((r) => r.json())
.then((j) => j.publicKey);
// Start reading from the ReadableStream. This will read
// the metadata up until the actual payload. The stream is still locked.
const unsealer = await StreamUnsealer.new(readable, vk);
// Retrieve the recipients (and their respective policies) from the header.
const recipients = unsealer.inspect_header();
// In this case it will yield:
// {
// 'Bob': { // recipient identifier
// ts: 1643634276, // timestamp
// con: [ // conjunction of attributes
// { t: "irma-demo.sidn-pbdf.email.email", v: "" }, // type/value pairs
// ],
// },
// }
// The disclosed values have to match with the values used for encryption.
// Note that we do not include a timestamp here.
const keyRequest = {
con: [{ t: "irma-demo.sidn-pbdf.email.email", v: "Bob" }],
};
const timestamp = recipients.get("Bob").ts;
const usk = await fetchKey(KeySorts.Encryption, keyRequest, timestamp);
// Unseal the contents, writing the plaintext to a `WritableStream`.
let sender = await unsealer.unseal("Bob", usk, writable);
// console.log(sender) will give the policy that was used to sign:
// {
// "public": {
// "ts": 1680531126,
// "con": [
// {
// "t": "irma-demo.gemeente.personalData.fullname",
// "v": "Alice"
// }
// ]
// },
// "private": {
// "ts": 1680531130,
// "con": [
// {
// "t": "irma-demo.gemeente.personalData.bsn",
// "v": "1234"
// }
// ]
// }
// }
加密 Uint8Array
加密和解密 Uint8Array 与上面的例子相似。 这个
WASM 模块也导出 seal 财务报告和审定财务报表 Unsealer,可用于此。 这个
函数 seal 返回一个新的 Uint8Array编辑 Unsealer.unseal 方法
返回数组 [plain, policy]时 plain 是一个 Uint8Array 包含
简写和 policy 是一个包含发送者的签名政策的对象.
使用网络工人
自兹 ReadableStream 财务报告和审定财务报表 WritableStream 为:
可转让, (中文(简体) ),
建议它进行封口和解除封口,例如,
在一个 网络工作者。 。 。 .
连接 可以成为有用的库
用于线条之间的通信.