/
encodeSHA.ts
90 lines (86 loc) · 2.84 KB
/
encodeSHA.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
86
87
88
89
90
import { BinaryToTextEncoding } from 'crypto';
import { FieldType } from '@terascope/types';
import {
FieldTransformConfig,
ProcessMode,
FunctionDefinitionType,
DataTypeFieldAndChildren,
FunctionDefinitionCategory
} from '../interfaces';
import { cryptoEncode } from './encode-utils';
export interface EncodeSHAArgs {
hash?: string;
digest?: BinaryToTextEncoding;
}
const hashDefault = 'sha256';
const digestDefault: BinaryToTextEncoding = 'hex';
export const encodeSHAConfig: FieldTransformConfig<EncodeSHAArgs> = {
name: 'encodeSHA',
type: FunctionDefinitionType.FIELD_TRANSFORM,
process_mode: ProcessMode.INDIVIDUAL_VALUES,
category: FunctionDefinitionCategory.STRING,
description: 'Returns a SHA encoded version of the input string. Specify the hash algorithm and digest with the args options.',
examples: [
{
args: {},
config: {
version: 1,
fields: {
testField: {
type: FieldType.String
}
}
},
field: 'testField',
input: '{ "some": "data" }',
output: 'e43e698b8ee20f09ae4257e81d7c8ac5074cdda2a8aef8d6c00dbbe5b404f7e5',
description: 'hashing algorithm defaults to sha256, and digest defaults to hex'
},
{
args: { digest: 'base64' },
config: {
version: 1,
fields: {
testField: {
type: FieldType.String
}
}
},
field: 'testField',
input: '{ "some": "data" }',
output: '5D5pi47iDwmuQlfoHXyKxQdM3aKorvjWwA275bQE9+U=',
}
],
create({ args: { hash = hashDefault, digest = digestDefault } }) {
return cryptoEncode(hash, digest);
},
accepts: [FieldType.String],
argument_schema: {
hash: {
type: FieldType.String,
array: false,
description: 'Which hashing algorithm to use, defaults to sha256'
},
digest: {
type: FieldType.String,
array: false,
description: 'Which hash digest to use, may be set to either "base64" or "hex", defaults to "hex"'
}
},
validate_arguments({ hash }) {
if (hash == null) return;
if (!hash.startsWith('sha')) {
throw new TypeError(`Invalid hash argument "${hash}" given to encodeSHA, must be a valid sha algorithm`);
}
},
output_type(inputConfig: DataTypeFieldAndChildren): DataTypeFieldAndChildren {
const { field_config, child_config } = inputConfig;
return {
field_config: {
...field_config,
type: FieldType.String
},
child_config
};
}
};