Skip to content

Commit 2eab3bf

Browse files
author
ChadKluck
committed
updated util hash function and tests
1 parent ca52662 commit 2eab3bf

File tree

3 files changed

+103
-13
lines changed

3 files changed

+103
-13
lines changed

application-infrastructure/src/tests/index.mjs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,35 +135,79 @@ describe('Test validations from config/validations.js', () => {
135135
*/
136136

137137
describe('Test utils', () => {
138-
describe('utils.hash.hashLast8', () => {
138+
describe('utils.hash.takeLast', () => {
139139
it('should return a string of length 8', () => {
140140
const input = 'example';
141-
const result = utils.hash.hashLast8(input);
141+
const result = utils.hash.takeLast(input);
142142
expect(result).to.be.a('string');
143143
expect(result).to.have.lengthOf(8);
144144
});
145145

146+
it('should return a string of length 6', () => {
147+
const input = 'example';
148+
const result = utils.hash.takeLast(input, 6);
149+
expect(result).to.be.a('string');
150+
expect(result).to.have.lengthOf(6);
151+
});
152+
146153
it('should return a different string for different inputs', () => {
147154
const input1 = 'example1';
148155
const input2 = 'example2';
149-
const result1 = utils.hash.hashLast8(input1);
150-
const result2 = utils.hash.hashLast8(input2);
156+
const result1 = utils.hash.takeLast(input1);
157+
const result2 = utils.hash.takeLast(input2);
151158
expect(result1).to.not.equal(result2);
152159
});
153160

154161
it('should return the same string for the same input', () => {
155162
const input = 'example';
156-
const result1 = utils.hash.hashLast8(input);
157-
const result2 = utils.hash.hashLast8(input);
163+
const result1 = utils.hash.takeLast(input);
164+
const result2 = utils.hash.takeLast(input);
158165
expect(result1).to.equal(result2);
159166
});
160167

161168
it('should return a string containing only valid characters', () => {
162169
const input = 'example';
163-
const result = utils.hash.hashLast8(input);
170+
const result = utils.hash.takeLast(input, 20);
164171
expect(result).to.match(/^[a-f0-9]+$/);
165172
});
166173
});
174+
175+
describe('utils.hash.takeFirst', () => {
176+
it('should return a string of length 8', () => {
177+
const input = 'example';
178+
const result = utils.hash.takeFirst(input);
179+
expect(result).to.be.a('string');
180+
expect(result).to.have.lengthOf(8);
181+
});
182+
183+
it('should return a string of length 6', () => {
184+
const input = 'example';
185+
const result = utils.hash.takeFirst(input, 6);
186+
expect(result).to.be.a('string');
187+
expect(result).to.have.lengthOf(6);
188+
});
189+
190+
it('should return a different string for different inputs', () => {
191+
const input1 = 'example1';
192+
const input2 = 'example2';
193+
const result1 = utils.hash.takeFirst(input1);
194+
const result2 = utils.hash.takeFirst(input2);
195+
expect(result1).to.not.equal(result2);
196+
});
197+
198+
it('should return the same string for the same input', () => {
199+
const input = 'example';
200+
const result1 = utils.hash.takeFirst(input);
201+
const result2 = utils.hash.takeFirst(input);
202+
expect(result1).to.equal(result2);
203+
});
204+
205+
it('should return a string containing only valid characters', () => {
206+
const input = 'example';
207+
const result = utils.hash.takeFirst(input, 20);
208+
expect(result).to.match(/^[a-f0-9]+$/);
209+
});
210+
})
167211
});
168212

169213
/* ****************************************************************************
Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,58 @@
11
const crypto = require('crypto');
22

33
/**
4-
* Hash a string using SHA256 and return the last 8 characters
4+
* Validates input for takeFirst and takeLast
5+
* @param {string} input
6+
* @param {number} n
7+
* @returns {boolean} - True if input is valid, false otherwise}
8+
*/
9+
const isValidSliceInput = (input, n) => {
10+
// throw error if input is not a string
11+
if (typeof input !== 'string') {
12+
throw new Error('input must be a string');
13+
}
14+
// throw error if n is not a number
15+
if (typeof n !== 'number') {
16+
throw new Error('n must be a number');
17+
}
18+
// throw error if n is less than 1
19+
if (n < 1) {
20+
throw new Error('n must be greater than 0');
21+
}
22+
// throw error if n is greater than 64
23+
if (n > 64) {
24+
throw new Error('n must be 64 or less');
25+
}
26+
return true;
27+
}
28+
29+
/**
30+
* Hash a string using SHA256 and return the last set of characters
531
* @param {string} input - The string to hash
6-
* @returns {string} The last 8 characters of the SHA256 hash
32+
* @param {number} n - The number of characters to return (default: 8)
33+
* @returns {string} The last set of characters of the SHA256 hash
734
*/
8-
const hashLast8 = (input) => {
9-
return crypto.createHash('sha256').update(input).digest('hex').slice(-8);
35+
const takeLast = (input, n=8) => {
36+
if (!isValidSliceInput(input, n)) {
37+
return "";
38+
} else {
39+
return crypto.createHash('sha256').update(input).digest('hex').slice(-n);
40+
}
1041
};
1142

12-
module.exports = { hashLast8 };
43+
/**
44+
* Hash a string using SHA256 and return the first set of characters
45+
* @param {string} input - The string to hash
46+
* @param {number} n - The number of characters to return (default: 8)
47+
* @returns {string} The first set of characters of the SHA256 hash
48+
*/
49+
const takeFirst = (input, n=8) => {
50+
if (!isValidSliceInput(input, n)) {
51+
return "";
52+
} else {
53+
return crypto.createHash('sha256').update(input).digest('hex').slice(0, n);
54+
}
55+
};
56+
57+
58+
module.exports = { takeLast, takeFirst };

application-infrastructure/src/views/example.view.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const transform = (data) => {
3333
// take data and create an 8 character hash
3434
// since we only expect a list of <20 games this is okay for an example
3535
// using sha256, create a hash and retain the last 8 characters
36-
const hashId = utils.hash.hashLast8(data);
36+
const hashId = utils.hash.takeLast(data, 8);
3737

3838
const returnData = {
3939
id: `G-${hashId}`,

0 commit comments

Comments
 (0)