Skip to content

Commit

Permalink
Use sha1 instead of sha256 for hashing
Browse files Browse the repository at this point in the history
This should be more performant while still being FIPS compliant
(see #12722).

sha1 isn't as secure as sha256, but since the usage context is just "has
this file changed? 🤔", this should be an acceptable degredation.
 pu

Signed-off-by: Mitchell Hentges <[email protected]>
  • Loading branch information
mitchhentgesspotify committed Oct 10, 2022
1 parent f192ac5 commit a496155
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/babel-jest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getCacheKeyFromConfig(

const configPath = [babelOptions.config ?? '', babelOptions.babelrc ?? ''];

return createHash('sha256')
return createHash('sha1')
.update(THIS_FILE)
.update('\0', 'utf8')
.update(JSON.stringify(babelOptions.options))
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-circus/src/__mocks__/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface Result extends ExecaSyncReturnValue {
}

export const runTest = (source: string) => {
const filename = createHash('sha256')
const filename = createHash('sha1')
.update(source)
.digest('hex')
.substring(0, 32);
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ afterEach(() => {

it('picks an id based on the rootDir', async () => {
const rootDir = '/root/path/foo';
const expected = createHash('sha256')
const expected = createHash('sha1')
.update('/root/path/foo')
.update(String(Infinity))
.digest('hex')
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const getDisplayNameColor = (seed?: string): Color => {
return 'white';
}

const hash = createHash('sha256');
const hash = createHash('sha1');
hash.update(seed);
const num = hash.digest().readUInt32LE(0);
return colors[num % colors.length];
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ const normalizeMissingOptions = (
projectIndex: number,
): Config.InitialOptionsWithRootDir => {
if (!options.id) {
options.id = createHash('sha256')
options.id = createHash('sha1')
.update(options.rootDir)
// In case we load config from some path that has the same root dir
.update(configPath || '')
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-create-cache-key-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function getGlobalCacheKey(files: Array<string>, values: Array<string>) {
]
.reduce(
(hash, chunk) => hash.update('\0', 'utf8').update(chunk || ''),
createHash('sha256'),
createHash('sha1'),
)
.digest('hex')
.substring(0, 32);
Expand All @@ -62,7 +62,7 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction {
const inferredOptions = options || configString;
const {config, instrument} = inferredOptions;

return createHash('sha256')
return createHash('sha1')
.update(globalCacheKey)
.update('\0', 'utf8')
.update(sourceText)
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-haste-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class HasteMap extends EventEmitter implements IHasteMap {
}

private async setupCachePath(options: Options): Promise<void> {
const rootDirHash = createHash('sha256')
const rootDirHash = createHash('sha1')
.update(options.rootDir)
.digest('hex')
.substring(0, 32);
Expand Down Expand Up @@ -344,7 +344,7 @@ class HasteMap extends EventEmitter implements IHasteMap {
id: string,
...extra: Array<string>
): string {
const hash = createHash('sha256').update(extra.join(''));
const hash = createHash('sha1').update(extra.join(''));
return path.join(
tmpdir,
`${id.replace(/\W/g, '-')}-${hash.digest('hex').substring(0, 32)}`,
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ class ScriptTransformer {
transformerCacheKey: string | undefined,
): string {
if (transformerCacheKey != null) {
return createHash('sha256')
return createHash('sha1')
.update(transformerCacheKey)
.update(CACHE_VERSION)
.digest('hex')
.substring(0, 32);
}

return createHash('sha256')
return createHash('sha1')
.update(fileData)
.update(transformOptions.configString)
.update(transformOptions.instrument ? 'instrument' : '')
Expand Down Expand Up @@ -882,7 +882,7 @@ const stripShebang = (content: string) => {
* could get corrupted, out-of-sync, etc.
*/
function writeCodeCacheFile(cachePath: string, code: string) {
const checksum = createHash('sha256')
const checksum = createHash('sha1')
.update(code)
.digest('hex')
.substring(0, 32);
Expand All @@ -901,7 +901,7 @@ function readCodeCacheFile(cachePath: string): string | null {
return null;
}
const code = content.substring(33);
const checksum = createHash('sha256')
const checksum = createHash('sha1')
.update(code)
.digest('hex')
.substring(0, 32);
Expand Down

0 comments on commit a496155

Please sign in to comment.