/
isHash.ts
85 lines (81 loc) · 2.3 KB
/
isHash.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { FieldType } from '@terascope/types';
import { isString, joinList } from '@terascope/utils';
import validator from 'validator';
import {
FieldValidateConfig, ProcessMode, FunctionDefinitionType,
FunctionDefinitionCategory
} from '../interfaces';
export interface IsHashArgs {
algo: string;
}
export type HashConfigAlgorithms =
| 'md4'
| 'md5'
| 'sha1'
| 'sha256'
| 'sha384'
| 'sha512'
| 'ripemd128'
| 'ripemd160'
| 'tiger128'
| 'tiger160'
| 'tiger192'
| 'crc32'
| 'crc32b';
const listOfAlgorithms = [
'md4',
'md5',
'sha1',
'sha256',
'sha384',
'sha512',
'ripemd128',
'ripemd160',
'tiger128',
'tiger160',
'tiger192',
'crc32',
'crc32b'
];
export const isHashConfig: FieldValidateConfig<IsHashArgs> = {
name: 'isHash',
type: FunctionDefinitionType.FIELD_VALIDATION,
process_mode: ProcessMode.INDIVIDUAL_VALUES,
category: FunctionDefinitionCategory.STRING,
description: 'Returns the input if it is a hashed value, otherwise returns null.',
examples: [
{
args: { algo: 'sha256' },
config: { version: 1, fields: { testField: { type: FieldType.String } } },
field: 'testField',
input: '85031b6f407e7f25cf826193338f7a4c2dc8c8b5130f5ca2c69a66d9f5107e33',
output: '85031b6f407e7f25cf826193338f7a4c2dc8c8b5130f5ca2c69a66d9f5107e33',
},
{
args: { algo: 'md5' },
config: { version: 1, fields: { testField: { type: FieldType.String } } },
field: 'testField',
input: '85031b6f407e7f25cf826193338f7a4c2dc8c8b5130f5ca2c69a66d9f5107e33',
output: null
}
],
create({ args: { algo } }) {
return (input: unknown) => isString(input) && validator.isHash(
input, algo as validator.HashAlgorithm
);
},
accepts: [FieldType.String],
argument_schema: {
algo: {
type: FieldType.String,
array: false,
description: 'The hashing algorithm to check values against'
}
},
required_arguments: ['algo'],
validate_arguments({ algo }) {
if (!listOfAlgorithms.includes(algo)) {
throw new Error(`Invalid algorithm ${algo}, must be set to one of ${joinList(listOfAlgorithms)}`);
}
}
};