mirror of
https://github.com/versia-pub/docs.git
synced 2025-12-06 06:18:19 +01:00
Update signing.md
This commit is contained in:
parent
26c609fa5b
commit
8f5136c8c8
|
|
@ -56,7 +56,7 @@ Here is an example of signing a request using TypeScript and the WebCrypto API:
|
||||||
```typescript
|
```typescript
|
||||||
const privateKey = await crypto.subtle.importKey(
|
const privateKey = await crypto.subtle.importKey(
|
||||||
"pkcs8",
|
"pkcs8",
|
||||||
atob("base64_private_key"),
|
str2ab(atob("base64_private_key")),
|
||||||
"Ed25519",
|
"Ed25519",
|
||||||
false,
|
false,
|
||||||
["sign"]
|
["sign"]
|
||||||
|
|
@ -67,18 +67,26 @@ const digest = await crypto.subtle.digest(
|
||||||
new TextEncoder().encode("request_body")
|
new TextEncoder().encode("request_body")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const userInbox = new URL("...");
|
||||||
|
|
||||||
|
const date = new Date();
|
||||||
|
|
||||||
const signature = await crypto.subtle.sign(
|
const signature = await crypto.subtle.sign(
|
||||||
"Ed25519",
|
"Ed25519",
|
||||||
privateKey,
|
privateKey,
|
||||||
new TextEncoder().encode(
|
new TextEncoder().encode(
|
||||||
"(request-target): post /users/uuid/inbox\n" +
|
`(request-target): post ${userInbox.pathname}\n` +
|
||||||
"host: example.com\n" +
|
`host: ${userInbox.host}\n` +
|
||||||
"date: Fri, 01 Jan 2021 00:00:00 GMT\n" +
|
`date: ${date.toUTCString()}\n` +
|
||||||
"digest: SHA-256=" + btoa(digest)
|
`digest: SHA-256=${btoa(
|
||||||
|
String.fromCharCode(...new Uint8Array(digest))
|
||||||
|
)}\n`
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
const signatureBase64 = base64Encode(signature);
|
const signatureBase64 = btoa(
|
||||||
|
String.fromCharCode(...new Uint8Array(signature))
|
||||||
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
> **Note**: Support for Ed25519 in the WebCrypto API is recent and may not be available in some older runtimes, such as Node.js or older browsers.
|
> **Note**: Support for Ed25519 in the WebCrypto API is recent and may not be available in some older runtimes, such as Node.js or older browsers.
|
||||||
|
|
@ -89,9 +97,9 @@ await fetch("https://example.com/users/uuid/inbox", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Date": "Fri, 01 Jan 2021 00:00:00 GMT",
|
Date: date.toUTCString(),
|
||||||
"Origin": "https://example.com",
|
Origin: "https://example.com",
|
||||||
"Signature": `keyId="https://example.com/users/uuid",algorithm="ed25519",headers="(request-target) host date digest",signature="${signatureBase64}"`
|
Signature: `keyId="${...}",algorithm="ed25519",headers="(request-target) host date digest",signature="${signatureBase64}"`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
// ...
|
// ...
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue