forked from utily/cryptly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncrypted.ts
More file actions
25 lines (24 loc) · 846 Bytes
/
Encrypted.ts
File metadata and controls
25 lines (24 loc) · 846 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
export interface Encrypted {
key?: string
value: string
salt: string
}
export namespace Encrypted {
export function is(value: any | Encrypted): value is Encrypted {
return (
typeof value == "object" &&
(value.key == undefined || typeof value.key == "string") &&
typeof value.value == "string" &&
typeof value.salt == "string"
)
}
export function stringify(encrypted: Encrypted): string {
encrypted.key = encrypted.key && encrypted.key.length != 4 ? encrypted.key.slice(-2) : encrypted.key
return [encrypted.key, encrypted.salt, encrypted.value].join(".")
}
export function parse(encryptedString: string): Encrypted | undefined {
const splitted = encryptedString.split(".")
const encrypted = { key: splitted[0], salt: splitted[1], value: splitted[2] }
return Encrypted.is(encrypted) ? encrypted : undefined
}
}