-
Notifications
You must be signed in to change notification settings - Fork 577
/
v5.ts
68 lines (57 loc) · 1.67 KB
/
v5.ts
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import {
bytesToUuid,
createBuffer,
stringToBytes,
uuidToBytes,
} from "./_common.ts";
import { Sha1 } from "../hash/sha1.ts";
import { assert } from "../_util/assert.ts";
const UUID_RE =
/^[0-9a-f]{8}-[0-9a-f]{4}-[5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
/**
* Validates the UUID v5.
* @param id UUID value.
*/
export function validate(id: string): boolean {
return UUID_RE.test(id);
}
/** The options used for generating a v5 uuid. */
export interface V5Options {
value: string | number[];
namespace: string | number[];
}
/**
* Generates a RFC4122 v5 UUID (SHA-1 namespace-based).
* @param options Can use a namespace and value to create SHA-1 hash.
* @param buf Can allow the UUID to be written in byte-form starting at the offset.
* @param offset Index to start writing on the UUID bytes in buffer.
*/
export function generate(
options: V5Options,
buf?: number[],
offset?: number,
): string | number[] {
const i = (buf && offset) || 0;
let { value, namespace } = options;
if (typeof value === "string") {
value = stringToBytes(value);
}
if (typeof namespace === "string") {
namespace = uuidToBytes(namespace);
}
assert(
namespace.length === 16,
"namespace must be uuid string or an Array of 16 byte values",
);
const content = namespace.concat(value);
const bytes = new Sha1().update(createBuffer(content)).digest();
bytes[6] = (bytes[6] & 0x0f) | 0x50;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
if (buf !== undefined) {
for (let idx = 0; idx < 16; ++idx) {
buf[i + idx] = bytes[idx];
}
}
return buf ?? bytesToUuid(bytes);
}