@guhttps/veu 0.0.0-stage → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +113 -2
- package/dist/addresses.d.ts +7 -0
- package/dist/addresses.js +43 -0
- package/dist/analyzer.d.ts +28 -0
- package/dist/analyzer.js +94 -0
- package/dist/anonymizer.d.ts +43 -0
- package/dist/anonymizer.js +173 -0
- package/dist/data.d.ts +16 -0
- package/dist/data.js +249 -0
- package/dist/entities.d.ts +20 -0
- package/dist/entities.js +34 -0
- package/dist/gateway.d.ts +136 -0
- package/dist/gateway.js +736 -0
- package/dist/index.d.ts +90 -0
- package/dist/index.js +162 -0
- package/dist/integrations/ai-sdk.d.ts +38 -0
- package/dist/integrations/ai-sdk.js +197 -0
- package/dist/integrations/fetch.d.ts +25 -0
- package/dist/integrations/fetch.js +99 -0
- package/dist/navegador.d.ts +3 -0
- package/dist/navegador.js +4 -0
- package/dist/patterns.d.ts +43 -0
- package/dist/patterns.js +142 -0
- package/dist/persons.d.ts +24 -0
- package/dist/persons.js +231 -0
- package/dist/secrets.d.ts +17 -0
- package/dist/secrets.js +144 -0
- package/dist/sha256.d.ts +3 -0
- package/dist/sha256.js +68 -0
- package/dist/text.d.ts +24 -0
- package/dist/text.js +121 -0
- package/dist/validators.d.ts +30 -0
- package/dist/validators.js +267 -0
- package/dist/vault.d.ts +63 -0
- package/dist/vault.js +200 -0
- package/dist/veu.global.js +31 -0
- package/package.json +68 -4
package/dist/patterns.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { contextWindow, fold, wordsRegex } from "./text.js";
|
|
2
|
+
import * as v from "./validators.js";
|
|
3
|
+
const B = "(?<![\\p{L}\\p{N}_])";
|
|
4
|
+
const E = "(?![\\p{L}\\p{N}_])";
|
|
5
|
+
export function pickScore(scores, isValid, hasContext) {
|
|
6
|
+
const base = (isValid ? scores.valid : scores.invalid) ?? null;
|
|
7
|
+
const withContext = (isValid ? scores.validContext : scores.invalidContext) ?? null;
|
|
8
|
+
if (!hasContext)
|
|
9
|
+
return base;
|
|
10
|
+
if (withContext !== null)
|
|
11
|
+
return withContext;
|
|
12
|
+
return base === null ? null : Math.min(1, base + 0.15);
|
|
13
|
+
}
|
|
14
|
+
export function spec(label, pattern, scores, options = {}) {
|
|
15
|
+
return {
|
|
16
|
+
label,
|
|
17
|
+
regex: new RegExp(pattern, `g${options.flags ?? ""}u`),
|
|
18
|
+
scores,
|
|
19
|
+
validator: options.validator,
|
|
20
|
+
context: wordsRegex(options.context ?? []),
|
|
21
|
+
before: options.before ?? 40,
|
|
22
|
+
after: options.after ?? 20,
|
|
23
|
+
adjust: options.adjust,
|
|
24
|
+
exclude: options.exclude,
|
|
25
|
+
name: options.name,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const S = (valid, invalid = null, validContext = null, invalidContext = null) => ({ valid, invalid, validContext, invalidContext });
|
|
29
|
+
const CTX_CPF = ["cpf", "c.p.f", "cadastro de pessoa fisica", "contribuinte", "cpf/cnpj"];
|
|
30
|
+
const CTX_CNPJ = ["cnpj", "c.n.p.j", "cpf/cnpj", "razao social", "inscricao"];
|
|
31
|
+
const CTX_RG = ["rg", "r.g", "identidade", "registro geral", "carteira de identidade", "ssp", "doc. identidade", "cin"];
|
|
32
|
+
const CTX_CNH = ["cnh", "habilitacao", "carteira de motorista", "carteira nacional de habilitacao", "registro da cnh"];
|
|
33
|
+
const CTX_TITULO = ["titulo de eleitor", "titulo eleitoral", "eleitor", "inscricao eleitoral"];
|
|
34
|
+
const CTX_PIS = ["pis", "pasep", "pis/pasep", "nit", "nis", "pis/nit"];
|
|
35
|
+
const CTX_CNS = ["cns", "cartao sus", "cartao do sus", "cartao nacional de saude", "sus"];
|
|
36
|
+
const CTX_RENAVAM = ["renavam"];
|
|
37
|
+
const CTX_PASSAPORTE = ["passaporte", "passport"];
|
|
38
|
+
const CTX_PHONE = [
|
|
39
|
+
"tel", "tel.", "telefone", "telefones", "celular", "cel", "cel.", "whatsapp", "whats", "zap", "wpp",
|
|
40
|
+
"contato", "fone", "ligar", "ligue", "ramal", "numero",
|
|
41
|
+
];
|
|
42
|
+
const CTX_CEP = ["cep"];
|
|
43
|
+
const CTX_PLATE = ["placa", "veiculo", "carro", "moto", "caminhao", "automovel"];
|
|
44
|
+
const CTX_CARD = ["cartao", "credito", "debito", "visa", "mastercard", "master", "elo", "amex", "hipercard", "final"];
|
|
45
|
+
const CTX_PIX = ["pix", "chave pix", "chave aleatoria", "chave"];
|
|
46
|
+
const CTX_IP = ["ip", "endereco ip", "ipv4", "ipv6", "acesso", "login"];
|
|
47
|
+
const CTX_BIRTH = [
|
|
48
|
+
"nascimento", "nasc", "nasc.", "nascido", "nascida", "nasceu", "data de nascimento", "dn", "d.n",
|
|
49
|
+
"aniversario", "nascida em", "nascido em",
|
|
50
|
+
];
|
|
51
|
+
const CTX_ACCOUNT = ["conta", "conta corrente", "conta-corrente", "c/c", "cc", "agencia", "ag", "ag.", "poupanca", "conta poupanca"];
|
|
52
|
+
const CTX_PROCESS = ["processo", "autos", "proc", "proc.", "acao", "numero do processo"];
|
|
53
|
+
const CTX_EMAIL = ["email", "e-mail"];
|
|
54
|
+
const CTX_IBAN = ["iban", "swift", "bic", "transferencia internacional", "conta internacional"];
|
|
55
|
+
const phoneAdjust = (value, score) => (/[()\s.+-]/u.test(value) ? score : score - 0.3);
|
|
56
|
+
const isYear = (value) => /^(?:19|20)\d{2}$/.test(value);
|
|
57
|
+
const MONTHS = "janeiro|fevereiro|mar[cç]o|abril|maio|junho|julho|agosto|setembro|outubro|novembro|dezembro";
|
|
58
|
+
export const DEFAULT_SPECS = [
|
|
59
|
+
// CPF
|
|
60
|
+
spec("CPF", `${B}\\d{3}\\.\\d{3}\\.\\d{3}-\\d{2}${E}`, S(0.95, 0.5, 1.0, 0.7), { validator: v.isValidCpf, context: CTX_CPF }),
|
|
61
|
+
spec("CPF", `${B}\\d{9}-\\d{2}${E}`, S(0.9, 0.4, 1.0, 0.65), { validator: v.isValidCpf, context: CTX_CPF }),
|
|
62
|
+
spec("CPF", `${B}\\d{3}\\s\\d{3}\\s\\d{3}\\s\\d{2}${E}`, S(0.85, null, 0.95, 0.6), { validator: v.isValidCpf, context: CTX_CPF }),
|
|
63
|
+
spec("CPF", `${B}\\d{11}${E}`, S(0.75, null, 0.95, 0.6), { validator: v.isValidCpf, context: CTX_CPF }),
|
|
64
|
+
// CNPJ (numérico e alfanumérico)
|
|
65
|
+
spec("CNPJ", `${B}[0-9A-Z]{2}\\.[0-9A-Z]{3}\\.[0-9A-Z]{3}/[0-9A-Z]{4}-\\d{2}${E}`, S(0.95, 0.5, 1.0, 0.7), { validator: v.isValidCnpj, context: CTX_CNPJ }),
|
|
66
|
+
spec("CNPJ", `${B}\\d{14}${E}`, S(0.75, null, 0.95, 0.6), { validator: v.isValidCnpj, context: CTX_CNPJ }),
|
|
67
|
+
spec("CNPJ", `${B}(?=[0-9A-Z]{12}\\d{2}${E})(?=[0-9]*[A-Z])[0-9A-Z]{12}\\d{2}${E}`, S(0.6, null, 0.95, 0.55), { validator: v.isValidCnpj, context: CTX_CNPJ }),
|
|
68
|
+
// Documentos que dependem de contexto
|
|
69
|
+
spec("PIS", `${B}\\d{3}\\.\\d{5}\\.\\d{2}-\\d${E}`, S(0.85, 0.45, 1.0, 0.75), { validator: v.isValidPis, context: CTX_PIS }),
|
|
70
|
+
spec("PIS", `${B}\\d{11}${E}`, S(null, null, 0.97, 0.65), { validator: v.isValidPis, context: CTX_PIS, before: 25 }),
|
|
71
|
+
spec("CNH", `${B}\\d{11}${E}`, S(null, null, 0.97, 0.75), { validator: v.isValidCnh, context: CTX_CNH, before: 30 }),
|
|
72
|
+
spec("TITULO_ELEITOR", `${B}\\d{4}\\s?\\d{4}\\s?\\d{4}${E}`, S(null, null, 0.95, 0.75), { validator: v.isValidTituloEleitor, context: CTX_TITULO, before: 30 }),
|
|
73
|
+
spec("RENAVAM", `${B}\\d{9,11}${E}`, S(null, null, 0.97, 0.75), { validator: v.isValidRenavam, context: CTX_RENAVAM, before: 25 }),
|
|
74
|
+
spec("CNS", `${B}[12789]\\d{2}\\s?\\d{4}\\s?\\d{4}\\s?\\d{4}${E}`, S(0.6, null, 0.97, 0.65), { validator: v.isValidCns, context: CTX_CNS }),
|
|
75
|
+
spec("RG", `${B}\\d{1,2}\\.\\d{3}\\.\\d{3}-[\\dXx]${E}`, S(0.6, null, 0.95), { context: CTX_RG }),
|
|
76
|
+
spec("RG", `${B}\\d{7,9}-?[\\dXx]?${E}`, S(null, null, 0.85), { context: CTX_RG, before: 20 }),
|
|
77
|
+
spec("RG", `${B}[A-Z]{2}-?\\d{1,2}\\.?\\d{3}\\.?\\d{3}${E}`, S(null, null, 0.85), { context: CTX_RG, before: 20 }),
|
|
78
|
+
spec("PASSAPORTE", `${B}[A-Z]{2}\\d{6}${E}`, S(null, null, 0.9), { context: CTX_PASSAPORTE, before: 30 }),
|
|
79
|
+
// Contatos
|
|
80
|
+
spec("EMAIL", `(?<![\\p{L}\\p{N}_.+-])[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\\.[A-Za-z0-9-]+)*\\.[A-Za-z]{2,}${E}`, S(0.95), { context: CTX_EMAIL }),
|
|
81
|
+
spec("TELEFONE", `(?<![\\p{L}\\p{N}_+])(?:\\+?55[\\s.-]?)?(?:\\(\\d{2}\\)|\\d{2})[\\s.-]?(?:9[\\s.]?\\d{4}|[2-5]\\d{3})[\\s.-]?\\d{4}${E}`, S(0.85, null, 0.95), { validator: v.isValidPhone, context: CTX_PHONE, adjust: phoneAdjust }),
|
|
82
|
+
spec("TELEFONE", `${B}9\\d{4}-\\d{4}${E}`, S(0.7, null, 0.9), { context: CTX_PHONE }),
|
|
83
|
+
spec("TELEFONE", `(?<![\\p{L}\\p{N}_+])\\+(?!55)[1-9]\\d{0,2}(?:[\\s.-]?\\(?\\d{1,4}\\)?){1,2}(?:[\\s.-]?\\d{2,4}){2,4}${E}`, S(0.8, null, 0.95), { validator: v.isValidIntlPhone, context: CTX_PHONE }),
|
|
84
|
+
spec("TELEFONE", `${B}[2-5]\\d{3}-\\d{4}${E}`, S(null, null, 0.8), { context: CTX_PHONE, before: 25 }),
|
|
85
|
+
// Localização e veículos
|
|
86
|
+
spec("CEP", `${B}\\d{5}-\\d{3}${E}`, S(0.75, null, 0.95), { context: CTX_CEP }),
|
|
87
|
+
spec("CEP", `${B}\\d{2}\\.\\d{3}-\\d{3}${E}`, S(0.8, null, 0.95), { context: CTX_CEP }),
|
|
88
|
+
spec("CEP", `${B}\\d{8}${E}`, S(null, null, 0.85), { context: CTX_CEP, before: 15 }),
|
|
89
|
+
spec("PLACA", `${B}[A-Z]{3}-\\d{4}${E}`, S(0.8, null, 0.95), { context: CTX_PLATE }),
|
|
90
|
+
spec("PLACA", `${B}[A-Z]{3}\\d[A-Z]\\d{2}${E}`, S(0.75, null, 0.95), { context: CTX_PLATE }),
|
|
91
|
+
spec("PLACA", `${B}[A-Z]{3}\\d{4}${E}`, S(0.45, null, 0.9), { context: CTX_PLATE }),
|
|
92
|
+
// Financeiro
|
|
93
|
+
spec("CARTAO_CREDITO", `${B}\\d(?:[ -]?\\d){12,18}${E}`, S(0.85, null, 0.95, 0.6), { validator: v.isValidCard, context: CTX_CARD }),
|
|
94
|
+
spec("IBAN", `${B}[A-Z]{2}\\d{2}(?:[ ]?[A-Z0-9]{4}){2,7}(?:[ ]?[A-Z0-9]{1,4})?${E}`, S(0.95, null, 1.0), { validator: v.isValidIban, context: CTX_IBAN }),
|
|
95
|
+
spec("CHAVE_PIX", `${B}[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}${E}`, S(null, null, 0.9), { context: CTX_PIX }),
|
|
96
|
+
spec("CONTA_BANCARIA", `${B}\\d{1,12}(?:[.-]?\\d{1,4})?-[\\dXx]${E}`, S(null, null, 0.85), { context: CTX_ACCOUNT, before: 20, after: 0 }),
|
|
97
|
+
spec("CONTA_BANCARIA", `${B}\\d{4,12}${E}`, S(null, null, 0.8), { context: CTX_ACCOUNT, before: 15, after: 0, exclude: isYear }),
|
|
98
|
+
// Rede
|
|
99
|
+
spec("IP", `${B}(?:\\d{1,3}\\.){3}\\d{1,3}${E}`, S(0.7, null, 0.9), { validator: v.isValidIpv4, context: CTX_IP }),
|
|
100
|
+
spec("IP", `(?<![\\p{L}\\p{N}_:])(?:[0-9A-Fa-f]{0,4}:){2,7}[0-9A-Fa-f]{0,4}(?![\\p{L}\\p{N}_:])`, S(0.75, null, 0.9), { validator: v.isValidIpv6, context: CTX_IP }),
|
|
101
|
+
// Datas de nascimento (só com contexto)
|
|
102
|
+
spec("DATA_NASCIMENTO", `${B}(?:\\d{1,2}[/.-]\\d{1,2}[/.-](?:\\d{4}|\\d{2})|\\d{4}-\\d{2}-\\d{2})${E}`, S(null, null, 0.85), { validator: v.isPlausibleBirthdate, context: CTX_BIRTH, before: 35, after: 0 }),
|
|
103
|
+
spec("DATA_NASCIMENTO", `${B}\\d{1,2}\\s+de\\s+(?:${MONTHS})\\s+de\\s+\\d{4}${E}`, S(null, null, 0.85), { validator: v.isPlausibleBirthdate, context: CTX_BIRTH, flags: "i", before: 35, after: 0 }),
|
|
104
|
+
// Processos judiciais
|
|
105
|
+
spec("PROCESSO", `${B}\\d{7}-\\d{2}\\.\\d{4}\\.\\d\\.\\d{2}\\.\\d{4}${E}`, S(0.9, 0.6, 1.0, 0.75), { validator: v.isValidProcessoCnj, context: CTX_PROCESS }),
|
|
106
|
+
];
|
|
107
|
+
const round3 = (value) => Math.round(value * 1000) / 1000;
|
|
108
|
+
export class PatternRecognizer {
|
|
109
|
+
name = "padroes";
|
|
110
|
+
specs;
|
|
111
|
+
labels;
|
|
112
|
+
constructor(specs) {
|
|
113
|
+
this.specs = specs ?? DEFAULT_SPECS;
|
|
114
|
+
this.labels = new Set(this.specs.map((item) => item.label));
|
|
115
|
+
}
|
|
116
|
+
*find(text) {
|
|
117
|
+
for (const item of this.specs) {
|
|
118
|
+
for (const match of text.matchAll(item.regex)) {
|
|
119
|
+
const value = match[0];
|
|
120
|
+
const start = match.index;
|
|
121
|
+
if (item.exclude?.(value))
|
|
122
|
+
continue;
|
|
123
|
+
const isValid = item.validator ? item.validator(value) : true;
|
|
124
|
+
let hasContext = false;
|
|
125
|
+
if (item.context) {
|
|
126
|
+
hasContext = item.context.test(contextWindow(text, start, start + value.length, item.before, item.after));
|
|
127
|
+
}
|
|
128
|
+
let score = pickScore(item.scores, isValid, hasContext);
|
|
129
|
+
if (score !== null && item.adjust)
|
|
130
|
+
score = item.adjust(value, score);
|
|
131
|
+
if (score === null || score <= 0)
|
|
132
|
+
continue;
|
|
133
|
+
yield { label: item.label, start, end: start + value.length, score: round3(score), recognizer: item.name ?? this.name };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/** Padrão definido pelo usuário. requiresContext: só conta com uma das palavras de contexto por perto. */
|
|
139
|
+
export function customPattern(label, pattern, score = 0.9, context = [], requiresContext = false) {
|
|
140
|
+
const scores = requiresContext ? S(null, null, score) : S(score, null, Math.min(1, score + 0.1));
|
|
141
|
+
return spec(label.toUpperCase(), pattern, scores, { context: context.map(fold), name: "personalizado" });
|
|
142
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Candidate, Recognizer } from "./entities.ts";
|
|
2
|
+
export declare const CONNECTORS: ReadonlySet<string>;
|
|
3
|
+
export declare function ambiguousNames(): ReadonlySet<string>;
|
|
4
|
+
export declare class PersonRecognizer implements Recognizer {
|
|
5
|
+
name: string;
|
|
6
|
+
labels: ReadonlySet<string>;
|
|
7
|
+
private firstNames;
|
|
8
|
+
private surnames;
|
|
9
|
+
private ambiguous;
|
|
10
|
+
private stopExact;
|
|
11
|
+
private stopFolded;
|
|
12
|
+
private places;
|
|
13
|
+
private organizations;
|
|
14
|
+
private context;
|
|
15
|
+
private greeting;
|
|
16
|
+
constructor(extraFirstNames?: Iterable<string>, extraSurnames?: Iterable<string>);
|
|
17
|
+
private isStop;
|
|
18
|
+
private isCapitalized;
|
|
19
|
+
private hasContext;
|
|
20
|
+
find(text: string): Iterable<Candidate>;
|
|
21
|
+
private scoreSequence;
|
|
22
|
+
private strongContext;
|
|
23
|
+
private lowercaseNames;
|
|
24
|
+
}
|
package/dist/persons.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// Nomes de pessoas por listas de nomes brasileiros e contexto (equivalente a veu/recognizers/persons.py).
|
|
2
|
+
import * as data from "./data.js";
|
|
3
|
+
import { escapeRegex, fold, isLower, isUpper, textBefore, wordsRegex } from "./text.js";
|
|
4
|
+
const WORD = /[A-Za-zÀ-ÖØ-öø-ÿ]+(?:['’][A-Za-zÀ-ÖØ-öø-ÿ]+)*/gu;
|
|
5
|
+
const SPACE_GAP = /^[ \t]{1,2}$/;
|
|
6
|
+
export const CONNECTORS = new Set([
|
|
7
|
+
"da", "de", "do", "das", "dos", "e", "del", "della", "di", "du", "van", "von", "der", "d",
|
|
8
|
+
]);
|
|
9
|
+
const PERSON_CONTEXT = [
|
|
10
|
+
"sr", "sr.", "sra", "sra.", "srta", "senhor", "senhora", "dona", "dom", "seu", "dr", "dr.", "dra", "dra.",
|
|
11
|
+
"doutor", "doutora", "prof", "prof.", "profa", "professor", "professora", "cliente", "paciente", "aluno",
|
|
12
|
+
"aluna", "nome", "nome completo", "chamo", "chama", "chamado", "chamada", "prezado", "prezada", "caro", "cara",
|
|
13
|
+
"querido", "querida", "oi", "ola", "atenciosamente", "att", "cordialmente", "assinado", "assinatura",
|
|
14
|
+
"titular", "responsavel", "contratante", "contratado", "contratada", "locador", "locadora", "locatario",
|
|
15
|
+
"locataria", "fiador", "fiadora", "autor", "autora", "reu", "requerente", "requerido", "requerida",
|
|
16
|
+
"testemunha", "advogado", "advogada", "funcionario", "funcionaria", "colaborador", "colaboradora", "colega",
|
|
17
|
+
"filho", "filha", "mae", "pai", "esposa", "esposo", "marido", "irmao", "irma", "avo", "neto", "neta", "tio",
|
|
18
|
+
"tia", "sobrinho", "sobrinha", "amigo", "amiga", "vizinho", "vizinha", "motorista", "entregador",
|
|
19
|
+
"entregadora", "atendente", "medico", "medica", "enfermeiro", "enfermeira", "falar com", "aos cuidados de",
|
|
20
|
+
"a/c", "de:", "para:", "por", "com o", "com a", "pelo", "pela", "segurado", "segurada", "beneficiario",
|
|
21
|
+
"beneficiaria", "dependente", "candidato", "candidata", "inquilino", "inquilina", "proprietario",
|
|
22
|
+
"proprietaria", "comprador", "compradora", "vendedor", "vendedora", "destinatario", "remetente",
|
|
23
|
+
];
|
|
24
|
+
const ORGANIZATION_WORDS = new Set([
|
|
25
|
+
"ltda", "ltd", "s.a", "sa", "me", "eireli", "epp", "mei", "cia", "companhia", "hospital", "clinica",
|
|
26
|
+
"farmacia", "drogaria", "padaria", "mercado", "supermercado", "mercearia", "construtora", "incorporadora",
|
|
27
|
+
"escola", "colegio", "faculdade", "universidade", "instituto", "fundacao", "associacao", "cooperativa",
|
|
28
|
+
"sindicato", "banco", "grupo", "empresa", "industria", "industrias", "comercio", "distribuidora",
|
|
29
|
+
"transportadora", "logistica", "consultoria", "tecnologia", "solucoes", "servicos", "sistemas", "restaurante",
|
|
30
|
+
"lanchonete", "pizzaria", "hotel", "pousada", "academia", "loja", "lojas", "magazine", "atacadao", "posto",
|
|
31
|
+
"auto", "pecas", "imobiliaria", "advocacia", "advogados", "associados", "contabilidade", "laboratorio",
|
|
32
|
+
"odontologia", "studio", "estudio", "agencia", "editora", "igreja", "paroquia", "prefeitura", "secretaria",
|
|
33
|
+
"ministerio", "tribunal", "assembleia", "hub", "tech", "digital", "software", "games", "foods",
|
|
34
|
+
]);
|
|
35
|
+
const STREET_BEFORE = new RegExp("(?<![\\p{L}\\p{N}_])(?:rua|r\\.|av\\.?|avenida|travessa|tv\\.|alameda|al\\.|praca|pca\\.|rodovia|rod\\.|estrada|" +
|
|
36
|
+
"largo|viela|beco|ladeira|vila|bairro|jardim|parque|condominio|edificio|residencial)(?:\\s+d[aeo]s?)?\\s*$", "u");
|
|
37
|
+
const GREETINGS = [
|
|
38
|
+
"oi", "ola", "e ai", "eai", "fala", "bom dia", "boa tarde", "boa noite", "obrigado", "obrigada", "valeu",
|
|
39
|
+
"vlw", "tchau", "beijo", "bjs", "abraco", "querido", "querida", "meu nome e", "me chamo", "sou o", "sou a",
|
|
40
|
+
"aqui e o", "aqui e a", "com o", "com a",
|
|
41
|
+
];
|
|
42
|
+
const STRONG_CONTEXT = /(?:meu nome e|me chamo|nome:|nome completo:|sr\.?|sra\.?|dona|dr\.?|dra\.?)\s*$/u;
|
|
43
|
+
const casefold = (text) => text.toLowerCase().replace(/ß/g, "ss");
|
|
44
|
+
const foldedSet = (values) => new Set(values.map(fold));
|
|
45
|
+
let shared = null;
|
|
46
|
+
function lists() {
|
|
47
|
+
shared ??= {
|
|
48
|
+
firstNames: foldedSet(data.firstNames),
|
|
49
|
+
surnames: foldedSet(data.surnames),
|
|
50
|
+
ambiguous: foldedSet(data.ambiguous),
|
|
51
|
+
stopwords: foldedSet(data.stopwords),
|
|
52
|
+
stopExact: new Set(data.stopwords.map(casefold)),
|
|
53
|
+
places: foldedSet(data.places),
|
|
54
|
+
organizations: foldedSet(data.organizations),
|
|
55
|
+
};
|
|
56
|
+
return shared;
|
|
57
|
+
}
|
|
58
|
+
export function ambiguousNames() {
|
|
59
|
+
return lists().ambiguous;
|
|
60
|
+
}
|
|
61
|
+
export class PersonRecognizer {
|
|
62
|
+
name = "nomes";
|
|
63
|
+
labels = new Set(["PESSOA"]);
|
|
64
|
+
firstNames;
|
|
65
|
+
surnames;
|
|
66
|
+
ambiguous;
|
|
67
|
+
stopExact;
|
|
68
|
+
stopFolded;
|
|
69
|
+
places;
|
|
70
|
+
organizations;
|
|
71
|
+
context = wordsRegex(PERSON_CONTEXT);
|
|
72
|
+
greeting = new RegExp(`(?:^|[^\\p{L}\\p{N}_])(?:${GREETINGS.map((g) => escapeRegex(fold(g))).join("|")})[\\s,!]*$`, "u");
|
|
73
|
+
constructor(extraFirstNames = [], extraSurnames = []) {
|
|
74
|
+
const base = lists();
|
|
75
|
+
this.firstNames = new Set([...base.firstNames, ...[...extraFirstNames].map(fold)]);
|
|
76
|
+
this.surnames = new Set([...base.surnames, ...[...extraSurnames].map(fold)]);
|
|
77
|
+
this.ambiguous = base.ambiguous;
|
|
78
|
+
this.stopExact = base.stopExact;
|
|
79
|
+
this.stopFolded = new Set([...base.stopwords].filter((w) => !this.firstNames.has(w) && !this.surnames.has(w)));
|
|
80
|
+
this.places = base.places;
|
|
81
|
+
this.organizations = base.organizations;
|
|
82
|
+
}
|
|
83
|
+
isStop(word) {
|
|
84
|
+
return this.stopExact.has(casefold(word)) || this.stopFolded.has(fold(word));
|
|
85
|
+
}
|
|
86
|
+
isCapitalized(word) {
|
|
87
|
+
if (!isUpper(word[0]))
|
|
88
|
+
return false;
|
|
89
|
+
const folded = fold(word);
|
|
90
|
+
return !(isUpper(word) && word.length <= 3) || this.firstNames.has(folded) || this.surnames.has(folded);
|
|
91
|
+
}
|
|
92
|
+
hasContext(text, start, before = 30) {
|
|
93
|
+
return this.context.test(fold(textBefore(text, start, before)));
|
|
94
|
+
}
|
|
95
|
+
*find(text) {
|
|
96
|
+
const words = [...text.matchAll(WORD)].map((m) => [m.index, m.index + m[0].length, m[0]]);
|
|
97
|
+
let index = 0;
|
|
98
|
+
while (index < words.length) {
|
|
99
|
+
const word = words[index][2];
|
|
100
|
+
if (!this.isCapitalized(word) || CONNECTORS.has(fold(word))) {
|
|
101
|
+
index += 1;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const sequence = [index];
|
|
105
|
+
let next = index + 1;
|
|
106
|
+
while (next < words.length) {
|
|
107
|
+
const gap = text.slice(words[next - 1][1], words[next][0]);
|
|
108
|
+
if (!SPACE_GAP.test(gap))
|
|
109
|
+
break;
|
|
110
|
+
const candidate = words[next][2];
|
|
111
|
+
if (CONNECTORS.has(fold(candidate))) {
|
|
112
|
+
if (next + 1 < words.length && SPACE_GAP.test(text.slice(words[next][1], words[next + 1][0]))
|
|
113
|
+
&& this.isCapitalized(words[next + 1][2])) {
|
|
114
|
+
sequence.push(next);
|
|
115
|
+
next += 1;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
if (!this.isCapitalized(candidate))
|
|
121
|
+
break;
|
|
122
|
+
sequence.push(next);
|
|
123
|
+
next += 1;
|
|
124
|
+
}
|
|
125
|
+
const found = this.scoreSequence(text, sequence.map((k) => words[k]));
|
|
126
|
+
if (found)
|
|
127
|
+
yield found;
|
|
128
|
+
index = sequence[sequence.length - 1] + 1;
|
|
129
|
+
}
|
|
130
|
+
yield* this.lowercaseNames(text, words);
|
|
131
|
+
}
|
|
132
|
+
scoreSequence(text, tokens) {
|
|
133
|
+
const whole = tokens.map((token) => fold(token[2])).join(" ");
|
|
134
|
+
if (this.places.has(whole) || this.organizations.has(whole))
|
|
135
|
+
return null;
|
|
136
|
+
if (tokens.some((token) => ORGANIZATION_WORDS.has(fold(token[2]))))
|
|
137
|
+
return null;
|
|
138
|
+
const firstIndex = tokens.findIndex((token) => this.firstNames.has(fold(token[2])));
|
|
139
|
+
if (firstIndex !== -1) {
|
|
140
|
+
tokens = tokens.slice(firstIndex);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
while (tokens.length && (this.isStop(tokens[0][2]) || CONNECTORS.has(fold(tokens[0][2]))))
|
|
144
|
+
tokens = tokens.slice(1);
|
|
145
|
+
}
|
|
146
|
+
while (tokens.length && (this.isStop(tokens[tokens.length - 1][2]) || CONNECTORS.has(fold(tokens[tokens.length - 1][2])))) {
|
|
147
|
+
tokens = tokens.slice(0, -1);
|
|
148
|
+
}
|
|
149
|
+
if (!tokens.length)
|
|
150
|
+
return null;
|
|
151
|
+
const folded = tokens.map((token) => fold(token[2]));
|
|
152
|
+
const joined = folded.join(" ");
|
|
153
|
+
if (this.places.has(joined) || this.organizations.has(joined))
|
|
154
|
+
return null;
|
|
155
|
+
const start = tokens[0][0];
|
|
156
|
+
const end = tokens[tokens.length - 1][1];
|
|
157
|
+
if (STREET_BEFORE.test(fold(textBefore(text, start, 16))))
|
|
158
|
+
return null;
|
|
159
|
+
const context = this.hasContext(text, start);
|
|
160
|
+
const rawNameWords = tokens.map((token) => token[2]).filter((word) => !CONNECTORS.has(fold(word)));
|
|
161
|
+
if (rawNameWords.length >= 2 && rawNameWords.some((word) => this.isStop(word)))
|
|
162
|
+
return null;
|
|
163
|
+
const nameWords = rawNameWords.map(fold);
|
|
164
|
+
const knownFirst = this.firstNames.has(folded[0]);
|
|
165
|
+
const known = nameWords.filter((word) => this.firstNames.has(word) || this.surnames.has(word)).length;
|
|
166
|
+
let score;
|
|
167
|
+
if (nameWords.length >= 2) {
|
|
168
|
+
if (knownFirst && known >= 2)
|
|
169
|
+
score = 0.9;
|
|
170
|
+
else if (knownFirst)
|
|
171
|
+
score = 0.8;
|
|
172
|
+
else if (this.surnames.has(nameWords[nameWords.length - 1]) || known >= 2)
|
|
173
|
+
score = context ? 0.7 : 0.62;
|
|
174
|
+
else if (context)
|
|
175
|
+
score = 0.65;
|
|
176
|
+
else
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
const word = nameWords[0];
|
|
181
|
+
if (this.isStop(tokens[0][2]) || (this.places.has(word) && !this.firstNames.has(word)))
|
|
182
|
+
return null;
|
|
183
|
+
if (this.firstNames.has(word)) {
|
|
184
|
+
if (this.ambiguous.has(word)) {
|
|
185
|
+
if (!context)
|
|
186
|
+
return null;
|
|
187
|
+
score = 0.7;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
score = context ? 0.8 : 0.6;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
else if (context && (this.surnames.has(word) || this.strongContext(text, start))) {
|
|
194
|
+
score = 0.6;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return { label: "PESSOA", start, end, score, recognizer: this.name };
|
|
201
|
+
}
|
|
202
|
+
strongContext(text, start) {
|
|
203
|
+
return STRONG_CONTEXT.test(fold(textBefore(text, start, 25)));
|
|
204
|
+
}
|
|
205
|
+
*lowercaseNames(text, words) {
|
|
206
|
+
let index = 0;
|
|
207
|
+
while (index < words.length) {
|
|
208
|
+
const [start, end, word] = words[index];
|
|
209
|
+
const folded = fold(word);
|
|
210
|
+
if (!isLower(word) || !this.firstNames.has(folded) || this.ambiguous.has(folded) || this.isStop(word)
|
|
211
|
+
|| folded.length < 3) {
|
|
212
|
+
index += 1;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
let last = index;
|
|
216
|
+
while (last + 1 < words.length && isLower(words[last + 1][2])
|
|
217
|
+
&& (this.surnames.has(fold(words[last + 1][2])) || CONNECTORS.has(fold(words[last + 1][2])))
|
|
218
|
+
&& SPACE_GAP.test(text.slice(words[last][1], words[last + 1][0]))) {
|
|
219
|
+
last += 1;
|
|
220
|
+
}
|
|
221
|
+
while (last > index && CONNECTORS.has(fold(words[last][2])))
|
|
222
|
+
last -= 1;
|
|
223
|
+
const before = fold(textBefore(text, start, 18));
|
|
224
|
+
if (last > index)
|
|
225
|
+
yield { label: "PESSOA", start, end: words[last][1], score: 0.65, recognizer: this.name };
|
|
226
|
+
else if (this.greeting.test(before))
|
|
227
|
+
yield { label: "PESSOA", start, end, score: 0.6, recognizer: this.name };
|
|
228
|
+
index = last + 1;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Candidate, Recognizer } from "./entities.ts";
|
|
2
|
+
export declare const SECRET_RECOGNIZER_LABELS: string[];
|
|
3
|
+
/** Prefixo que identifica o tipo da chave sem revelar o segredo ("sk-proj-", "ghp_", "AKIA"). */
|
|
4
|
+
export declare function secretPrefix(value: string): string;
|
|
5
|
+
/** Palavras do nome de um campo: "DB_PASSWORD" -> [db, password]; "clientSecret" -> [client, secret]. */
|
|
6
|
+
export declare function keyWords(key: string): string[];
|
|
7
|
+
export declare function keyKind(key: string): "SENHA" | "SEGREDO" | null;
|
|
8
|
+
export declare function isPlaceholder(value: string): boolean;
|
|
9
|
+
export declare function entropy(value: string): number;
|
|
10
|
+
/** Tem cara de chave gerada: um trecho longo misturando letras e números, ou longo e com alta entropia. */
|
|
11
|
+
export declare function looksRandom(value: string): boolean;
|
|
12
|
+
export declare function plausiblePassword(value: string, prose: boolean): boolean;
|
|
13
|
+
export declare class SecretRecognizer implements Recognizer {
|
|
14
|
+
name: string;
|
|
15
|
+
labels: ReadonlySet<string>;
|
|
16
|
+
find(text: string): Iterable<Candidate>;
|
|
17
|
+
}
|
package/dist/secrets.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// Segredos: chaves de API, tokens, senhas, chaves privadas e URLs de conexão (equivalente a recognizers/secrets.py).
|
|
2
|
+
import { secretRules } from "./data.js";
|
|
3
|
+
import { isAlpha, isLower } from "./text.js";
|
|
4
|
+
export const SECRET_RECOGNIZER_LABELS = ["CHAVE_API", "TOKEN", "SENHA", "CHAVE_PRIVADA", "URL_CONEXAO", "SEGREDO"];
|
|
5
|
+
let compiled = null;
|
|
6
|
+
function rules() {
|
|
7
|
+
compiled ??= secretRules.map((rule) => ({
|
|
8
|
+
id: rule.id,
|
|
9
|
+
label: rule.entidade,
|
|
10
|
+
regex: new RegExp(rule.padrao, "gd"),
|
|
11
|
+
score: rule.pontuacao,
|
|
12
|
+
group: rule.grupo ?? 0,
|
|
13
|
+
filter: rule.filtro,
|
|
14
|
+
trim: rule.aparar ?? "",
|
|
15
|
+
}));
|
|
16
|
+
return compiled;
|
|
17
|
+
}
|
|
18
|
+
const VALUE_CHARS = "[^\\s\"'`,;&<>()\\[\\]{}]";
|
|
19
|
+
const ASSIGNMENT = new RegExp(`(?<![A-Za-z0-9_.-])([A-Za-z_][A-Za-z0-9_.-]{0,60})["']?[ \\t]*(=>|:=|=|:)[ \\t]*["']?(${VALUE_CHARS}{3,500})`, "gd");
|
|
20
|
+
const PROSE_PASSWORD = new RegExp(`(?<![A-Za-z0-9])(?:senha|password|passwd)(?:[^\\n:=]{0,25}?)(\\s*(?::|=)\\s*|\\s+(?:é|eh|is)\\s+)["']?(${VALUE_CHARS}{3,200})`, "gdi");
|
|
21
|
+
const PASSWORD_WORDS = new Set(["password", "passwd", "passphrase", "pwd", "senha", "pass", "passw"]);
|
|
22
|
+
const SECRET_WORDS = new Set(["secret", "token", "apikey", "credential", "credentials", "credencial", "credenciais"]);
|
|
23
|
+
const SECRET_PAIRS = new Set([
|
|
24
|
+
"api key", "access key", "private key", "secret key", "auth key", "chave api", "signing key", "encryption key",
|
|
25
|
+
"master key", "account key", "app key",
|
|
26
|
+
]);
|
|
27
|
+
const NOT_SECRET_WORDS = new Set([
|
|
28
|
+
"url", "uri", "endpoint", "path", "file", "filename", "name", "type", "count", "limit", "length", "size", "ttl",
|
|
29
|
+
"expires", "expiry", "expiration", "max", "min", "header", "field", "label", "prefix", "env", "var",
|
|
30
|
+
"placeholder", "hint", "description", "desc", "enabled", "required", "policy", "rotation", "version", "tokens",
|
|
31
|
+
"usage", "id", "ref", "arn", "mode", "format", "manager", "provider", "store", "location", "nome",
|
|
32
|
+
"tipo", "arquivo", "caminho",
|
|
33
|
+
]);
|
|
34
|
+
const PLACEHOLDERS = new Set([
|
|
35
|
+
"null", "none", "nil", "true", "false", "undefined", "empty", "vazio", "string", "str", "number", "int",
|
|
36
|
+
"bool", "boolean", "any", "text", "texto", "required", "optional", "hidden", "secret", "password", "senha",
|
|
37
|
+
"token", "redacted", "removido", "removida", "omitido", "omitida", "masked", "default",
|
|
38
|
+
]);
|
|
39
|
+
const IDENTIFIER_PARTS = /[A-Z]+(?![a-z])|[A-Z]?[a-z]+|\d+/g;
|
|
40
|
+
const DOTTED = /^[A-Za-z_]\w*(?:\.[A-Za-z_]\w*)+$/;
|
|
41
|
+
const CONSTANT = /^[A-Z_]+$/;
|
|
42
|
+
const FILLER = /^[*xX.•_#-]+$/;
|
|
43
|
+
const RANDOM_RUN = /[A-Za-z0-9+/=_-]{8,}/g;
|
|
44
|
+
const PREFIX = /^(?:AKIA|ASIA|ABIA|ACCA|AIza|GOCSPX-|ya29\.|eyJ|SG\.|\$aact_|(?:[A-Za-z]{2,10}[_-]){1,2})/;
|
|
45
|
+
/** Prefixo que identifica o tipo da chave sem revelar o segredo ("sk-proj-", "ghp_", "AKIA"). */
|
|
46
|
+
export function secretPrefix(value) {
|
|
47
|
+
const match = PREFIX.exec(value);
|
|
48
|
+
return match && match[0].length < value.length ? match[0] : "";
|
|
49
|
+
}
|
|
50
|
+
/** Palavras do nome de um campo: "DB_PASSWORD" -> [db, password]; "clientSecret" -> [client, secret]. */
|
|
51
|
+
export function keyWords(key) {
|
|
52
|
+
return key.split(/[_.\-\s]+/).flatMap((chunk) => chunk.match(IDENTIFIER_PARTS) ?? []).map((part) => part.toLowerCase());
|
|
53
|
+
}
|
|
54
|
+
export function keyKind(key) {
|
|
55
|
+
const words = keyWords(key);
|
|
56
|
+
if (!words.length || words.some((word) => NOT_SECRET_WORDS.has(word)))
|
|
57
|
+
return null;
|
|
58
|
+
if (words.some((word) => PASSWORD_WORDS.has(word)))
|
|
59
|
+
return "SENHA";
|
|
60
|
+
if (words.some((word) => SECRET_WORDS.has(word)))
|
|
61
|
+
return "SEGREDO";
|
|
62
|
+
for (let i = 0; i + 1 < words.length; i += 1)
|
|
63
|
+
if (SECRET_PAIRS.has(`${words[i]} ${words[i + 1]}`))
|
|
64
|
+
return "SEGREDO";
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
export function isPlaceholder(value) {
|
|
68
|
+
const lowered = value.toLowerCase();
|
|
69
|
+
return PLACEHOLDERS.has(lowered) || "$%<{[#*".includes(value[0]) || FILLER.test(value) || DOTTED.test(value)
|
|
70
|
+
|| CONSTANT.test(value)
|
|
71
|
+
|| ["your", "sua_", "seu_", "<", "example", "exemplo", "process.env", "os.environ"].some((p) => lowered.startsWith(p));
|
|
72
|
+
}
|
|
73
|
+
export function entropy(value) {
|
|
74
|
+
const counts = new Map();
|
|
75
|
+
for (const char of value)
|
|
76
|
+
counts.set(char, (counts.get(char) ?? 0) + 1);
|
|
77
|
+
let total = 0;
|
|
78
|
+
for (const n of counts.values())
|
|
79
|
+
total -= (n / value.length) * Math.log2(n / value.length);
|
|
80
|
+
return total;
|
|
81
|
+
}
|
|
82
|
+
/** Tem cara de chave gerada: um trecho longo misturando letras e números, ou longo e com alta entropia. */
|
|
83
|
+
export function looksRandom(value) {
|
|
84
|
+
if (value.length < 8 || isPlaceholder(value))
|
|
85
|
+
return false;
|
|
86
|
+
for (const [run] of value.matchAll(RANDOM_RUN)) {
|
|
87
|
+
const hasDigit = /\d/.test(run);
|
|
88
|
+
const hasLetter = /[A-Za-z]/.test(run);
|
|
89
|
+
if (hasDigit && hasLetter)
|
|
90
|
+
return true;
|
|
91
|
+
if (run.length >= 24 && /[A-Z]/.test(run) && /[a-z]/.test(run) && entropy(run) >= 3.5)
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
export function plausiblePassword(value, prose) {
|
|
97
|
+
if (value.length < 4 || isPlaceholder(value))
|
|
98
|
+
return false;
|
|
99
|
+
return !(prose && isAlpha(value) && (isLower(value) || isLower(value.slice(1))));
|
|
100
|
+
}
|
|
101
|
+
export class SecretRecognizer {
|
|
102
|
+
name = "segredos";
|
|
103
|
+
labels = new Set(SECRET_RECOGNIZER_LABELS);
|
|
104
|
+
*find(text) {
|
|
105
|
+
for (const rule of rules()) {
|
|
106
|
+
for (const match of text.matchAll(rule.regex)) {
|
|
107
|
+
const span = match.indices?.[rule.group];
|
|
108
|
+
if (!span)
|
|
109
|
+
continue;
|
|
110
|
+
const start = span[0];
|
|
111
|
+
let value = text.slice(start, span[1]);
|
|
112
|
+
if (rule.trim) {
|
|
113
|
+
while (value && rule.trim.includes(value[value.length - 1]))
|
|
114
|
+
value = value.slice(0, -1);
|
|
115
|
+
}
|
|
116
|
+
if (!value || (rule.filter === "aleatorio" && !looksRandom(value)))
|
|
117
|
+
continue;
|
|
118
|
+
yield { label: rule.label, start, end: start + value.length, score: rule.score, recognizer: this.name };
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
for (const match of text.matchAll(ASSIGNMENT)) {
|
|
122
|
+
const kind = keyKind(match[1]);
|
|
123
|
+
if (kind === null)
|
|
124
|
+
continue;
|
|
125
|
+
const [start, end] = match.indices[3];
|
|
126
|
+
if (text[end] === "(")
|
|
127
|
+
continue; // password = getpass()
|
|
128
|
+
const value = match[3];
|
|
129
|
+
if (kind === "SENHA") {
|
|
130
|
+
const prose = match[2] === ":" && keyWords(match[1]).length === 1;
|
|
131
|
+
if (plausiblePassword(value, prose))
|
|
132
|
+
yield { label: "SENHA", start, end, score: 0.85, recognizer: this.name };
|
|
133
|
+
}
|
|
134
|
+
else if (looksRandom(value)) {
|
|
135
|
+
yield { label: "SEGREDO", start, end, score: 0.85, recognizer: this.name };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
for (const match of text.matchAll(PROSE_PASSWORD)) {
|
|
139
|
+
const [start, end] = match.indices[2];
|
|
140
|
+
if (plausiblePassword(match[2], true))
|
|
141
|
+
yield { label: "SENHA", start, end, score: 0.8, recognizer: this.name };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
package/dist/sha256.d.ts
ADDED
package/dist/sha256.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// SHA-256 e HMAC-SHA256 síncronos (a WebCrypto só tem a versão assíncrona). Usados pelo operador "hash".
|
|
2
|
+
const K = new Uint32Array([
|
|
3
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
4
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
5
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
6
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
7
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
8
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
9
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
10
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
11
|
+
]);
|
|
12
|
+
export function sha256(message) {
|
|
13
|
+
const length = message.length;
|
|
14
|
+
const padded = new Uint8Array(((length + 9 + 63) >> 6) << 6);
|
|
15
|
+
padded.set(message);
|
|
16
|
+
padded[length] = 0x80;
|
|
17
|
+
const view = new DataView(padded.buffer);
|
|
18
|
+
view.setUint32(padded.length - 8, Math.floor((length * 8) / 2 ** 32));
|
|
19
|
+
view.setUint32(padded.length - 4, (length * 8) >>> 0);
|
|
20
|
+
const h = new Uint32Array([
|
|
21
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
22
|
+
]);
|
|
23
|
+
const w = new Uint32Array(64);
|
|
24
|
+
const rotr = (x, n) => (x >>> n) | (x << (32 - n));
|
|
25
|
+
for (let offset = 0; offset < padded.length; offset += 64) {
|
|
26
|
+
for (let i = 0; i < 16; i += 1)
|
|
27
|
+
w[i] = view.getUint32(offset + i * 4);
|
|
28
|
+
for (let i = 16; i < 64; i += 1) {
|
|
29
|
+
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >>> 3);
|
|
30
|
+
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >>> 10);
|
|
31
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
|
|
32
|
+
}
|
|
33
|
+
let [a, b, c, d, e, f, g, hh] = h;
|
|
34
|
+
for (let i = 0; i < 64; i += 1) {
|
|
35
|
+
const t1 = (hh + (rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)) + ((e & f) ^ (~e & g)) + K[i] + w[i]) >>> 0;
|
|
36
|
+
const t2 = ((rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) >>> 0;
|
|
37
|
+
[hh, g, f, e, d, c, b, a] = [g, f, e, (d + t1) >>> 0, c, b, a, (t1 + t2) >>> 0];
|
|
38
|
+
}
|
|
39
|
+
h[0] += a;
|
|
40
|
+
h[1] += b;
|
|
41
|
+
h[2] += c;
|
|
42
|
+
h[3] += d;
|
|
43
|
+
h[4] += e;
|
|
44
|
+
h[5] += f;
|
|
45
|
+
h[6] += g;
|
|
46
|
+
h[7] += hh;
|
|
47
|
+
}
|
|
48
|
+
const output = new Uint8Array(32);
|
|
49
|
+
const outView = new DataView(output.buffer);
|
|
50
|
+
h.forEach((value, i) => outView.setUint32(i * 4, value));
|
|
51
|
+
return output;
|
|
52
|
+
}
|
|
53
|
+
export function hmacSha256(key, message) {
|
|
54
|
+
let block = key.length > 64 ? sha256(key) : key;
|
|
55
|
+
const padded = new Uint8Array(64);
|
|
56
|
+
padded.set(block);
|
|
57
|
+
block = padded;
|
|
58
|
+
const inner = new Uint8Array(64 + message.length);
|
|
59
|
+
const outer = new Uint8Array(64 + 32);
|
|
60
|
+
for (let i = 0; i < 64; i += 1) {
|
|
61
|
+
inner[i] = block[i] ^ 0x36;
|
|
62
|
+
outer[i] = block[i] ^ 0x5c;
|
|
63
|
+
}
|
|
64
|
+
inner.set(message, 64);
|
|
65
|
+
outer.set(sha256(inner), 64);
|
|
66
|
+
return sha256(outer);
|
|
67
|
+
}
|
|
68
|
+
export const toHex = (bytes) => [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
|