Skip to content

Commit cdc25cd

Browse files
committed
add Cipher
1 parent e438733 commit cdc25cd

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tls/enum",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"exports": "./src/mod.ts",
55
"publish": {
66
"exclude": ["dist/"]

src/cipher.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @ts-self-types="../type/cipher.d.ts"
2+
3+
import { Enum } from "./enum.js";
4+
5+
export class Cipher extends Enum {
6+
static AES_128_GCM_SHA256 = new Cipher('AES_128_GCM_SHA256', 0x01);
7+
static AES_256_GCM_SHA384 = new Cipher('AES_256_GCM_SHA384', 0x02);
8+
static CHACHA20_POLY1305_SHA256 = new Cipher('CHACHA20_POLY1305_SHA256', 0x03);
9+
10+
static from(array){
11+
const copy = Uint8Array.from(array);
12+
if(copy.at(0)!==0x13)throw TypeError(`Expected 0x13 at index 0`)
13+
return Cipher.fromValue(copy.at(1))
14+
}
15+
16+
get Uint16(){return Uint8Array.of(0x13, +this)}
17+
}
18+

test/cipher_test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { assertEquals } from "jsr:@std/assert";
2+
import { Cipher } from "../src/cipher.js";
3+
4+
Deno.test("Cipher", () => {
5+
const test = Cipher.AES_128_GCM_SHA256.Uint16;
6+
const back = Cipher.from(test).Uint16;
7+
assertEquals(test, back)
8+
})

type/cipher.d.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Enum } from "../src/enum.js";
2+
3+
/**
4+
* Represents a cipher suite used in TLS 1.3.
5+
* The cipher suite includes the encryption algorithm, mode, and hash function.
6+
*/
7+
export class Cipher extends Enum {
8+
/**
9+
* Cipher suite using AES 128 GCM with SHA256 for authentication.
10+
*/
11+
static AES_128_GCM_SHA256: Cipher;
12+
13+
/**
14+
* Cipher suite using AES 256 GCM with SHA384 for authentication.
15+
*/
16+
static AES_256_GCM_SHA384: Cipher;
17+
18+
/**
19+
* Cipher suite using ChaCha20 Poly1305 with SHA256 for authentication.
20+
*/
21+
static CHACHA20_POLY1305_SHA256: Cipher;
22+
23+
/**
24+
* Creates a `Cipher` instance from a binary representation.
25+
* @param array The binary representation of the cipher suite.
26+
* @returns The corresponding `Cipher` instance.
27+
* @throws {TypeError} If the first byte of the input is not `0x13`.
28+
*/
29+
static from(array: Uint8Array | Array<number>): Cipher;
30+
31+
/**
32+
* Gets the 2-byte identifier of the cipher suite.
33+
* @returns A `Uint8Array` containing the cipher suite identifier.
34+
*/
35+
get Uint16(): Uint8Array;
36+
}

0 commit comments

Comments
 (0)