Skip to content

Commit

Permalink
Refine samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
dajiaji committed Aug 18, 2023
1 parent 76057ce commit d6c4ffd
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 331 deletions.
291 changes: 149 additions & 142 deletions README.md

Large diffs are not rendered by default.

90 changes: 44 additions & 46 deletions x/chacha20poly1305/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,38 +114,36 @@ This section shows some typical usage examples.
import { Chacha20Poly1305 } from "https://esm.sh/@hpke/[email protected]";
globalThis.doHpke = async () => {
const suite = new CipherSuite({
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Chacha20Poly1305()
});
try {
const suite = new CipherSuite({
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Chacha20Poly1305()
});
const rkp = await suite.kem.generateKeyPair();
const rkp = await suite.kem.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey, // rkp (CryptoKeyPair) is also acceptable.
enc: sender.enc,
});
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey
});
// encrypt
const ct = await sender.seal(new TextEncoder().encode("hello world!"));
// encrypt
const ct = await sender.seal(new TextEncoder().encode("Hello world!"));
// decrypt
try {
const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey, // rkp (CryptoKeyPair) is also acceptable.
enc: sender.enc,
});
// decrypt
const pt = await recipient.open(ct);
// hello world!
// Hello world!
alert(new TextDecoder().decode(pt));
} catch (err) {
alert("failed to decrypt.");
alert("failed:", err.message);
}
}
</script>
<button type="button" onclick="doHpke()">do HPKE</button>
</body>
Expand Down Expand Up @@ -173,26 +171,26 @@ async function doHpke() {
recipientPublicKey: rkp.publicKey,
});

// encrypt
const ct = await sender.seal(new TextEncoder().encode("Hello world!"));

const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey,
enc: sender.enc,
});

// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));

// decrypt
try {
const pt = await recipient.open(ct);

console.log("decrypted: ", new TextDecoder().decode(pt));
// decrypted: my-secret-message
} catch (err) {
console.log("failed to decrypt.");
}
const pt = await recipient.open(ct);

// Hello world!
console.log(new TextDecoder().decode(pt));
}

doHpke();
try {
doHpke();
} catch (err) {
console.log("failed:", err.message);
}
```

### Deno
Expand All @@ -217,26 +215,26 @@ async function doHpke() {
recipientPublicKey: rkp.publicKey,
});

// encrypt
const ct = await sender.seal(new TextEncoder().encode("Hello world!"));

const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey,
enc: sender.enc,
});

// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));

try {
// decrypt
const pt = await recipient.open(ct);
// decrypt
const pt = await recipient.open(ct);

console.log("decrypted: ", new TextDecoder().decode(pt));
// decrypted: my-secret-message
} catch (_err: unknown) {
console.log("failed to decrypt.");
}
// Hello world!
console.log(new TextDecoder().decode(pt));
}

doHpke();
try {
doHpke();
} catch (_err: unknown) {
console.log("failed.");
}
```

## Contributing
Expand Down
90 changes: 44 additions & 46 deletions x/dhkem-secp256k1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,38 +111,36 @@ This section shows some typical usage examples.
import { DhkemSecp256k1HkdfSha256 } from "https://esm.sh/@hpke/[email protected]";
globalThis.doHpke = async () => {
const suite = new CipherSuite({
kem: new DhkemSecp256k1HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});
try {
const suite = new CipherSuite({
kem: new DhkemSecp256k1HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});
const rkp = await suite.kem.generateKeyPair();
const rkp = await suite.kem.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey, // rkp (CryptoKeyPair) is also acceptable.
enc: sender.enc,
});
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey
});
// encrypt
const ct = await sender.seal(new TextEncoder().encode("hello world!"));
// encrypt
const ct = await sender.seal(new TextEncoder().encode("Hello world!"));
// decrypt
try {
const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey, // rkp (CryptoKeyPair) is also acceptable.
enc: sender.enc,
});
// decrypt
const pt = await recipient.open(ct);
// hello world!
// Hello world!
alert(new TextDecoder().decode(pt));
} catch (err) {
alert("failed to decrypt.");
alert("failed:", err.message);
}
}
</script>
<button type="button" onclick="doHpke()">do HPKE</button>
</body>
Expand Down Expand Up @@ -170,26 +168,26 @@ async function doHpke() {
recipientPublicKey: rkp.publicKey,
});

// encrypt
const ct = await sender.seal(new TextEncoder().encode("Hello world!"));

const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey,
enc: sender.enc,
});

// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));

// decrypt
try {
const pt = await recipient.open(ct);

console.log("decrypted: ", new TextDecoder().decode(pt));
// decrypted: my-secret-message
} catch (err) {
console.log("failed to decrypt.");
}
const pt = await recipient.open(ct);

// Hello world!
console.log(new TextDecoder().decode(pt));
}

doHpke();
try {
doHpke();
} catch (err) {
console.log("failed:", err.message);
}
```

### Deno
Expand All @@ -212,26 +210,26 @@ async function doHpke() {
recipientPublicKey: rkp.publicKey,
});

// encrypt
const ct = await sender.seal(new TextEncoder().encode("Hello world!"));

const recipient = await suite.createRecipientContext({
recipientKey: rkp.privateKey,
enc: sender.enc,
});

// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));

try {
// decrypt
const pt = await recipient.open(ct);
// decrypt
const pt = await recipient.open(ct);

console.log("decrypted: ", new TextDecoder().decode(pt));
// decrypted: my-secret-message
} catch (_err: unknown) {
console.log("failed to decrypt.");
}
// Hello world!
console.log(new TextDecoder().decode(pt));
}

doHpke();
try {
doHpke();
} catch (_err: unknown) {
console.log("failed.");
}
```

## Contributing
Expand Down
Loading

0 comments on commit d6c4ffd

Please sign in to comment.