forked from utily/cryptly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithms.ts
More file actions
26 lines (24 loc) · 850 Bytes
/
Algorithms.ts
File metadata and controls
26 lines (24 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { Algorithm } from "./Algorithm"
import { Tuple2 } from "./Tuple"
export type Algorithms = Record<string, Algorithm> & { current: Algorithm }
export namespace Algorithms {
export function create(create: (keys: string[]) => Algorithm, current: string, ...secrets: string[]): Algorithms {
const [first, ...remainder] = secrets.map(part =>
Object.fromEntries(part.split(",").map(secret => secret.split(":", 2).map(item => item.trim())))
)
const result = Object.assign(
{},
...Object.entries(first)
.map<Tuple2<string, string[]>>(([name, secret]) => [
name,
[secret, ...remainder.map(part => part[name]).filter(part => part)],
])
.map(([name, secrets]) => ({
get [name]() {
return Object.assign(create(secrets), { name })
},
}))
)
return { current: result[current], ...result }
}
}