codemod/cryptojs-sha1-to-sha256
Transform CryptoJS.SHA1() calls to use CryptoJS.SHA256() for improved security. SHA-1 is cryptographically broken and vulnerable to collision attacks.
# CryptoJS SHA-1 to SHA-256 Migration Codemod
š **Critical Security Upgrade**: Automatically migrates CryptoJS SHA-1 usage to SHA-256 to address cryptographic vulnerabilities.
## Overview
SHA-1 is cryptographically broken and vulnerable to collision attacks. This codemod helps you upgrade from the deprecated SHA-1 algorithm to the more secure SHA-256, ensuring your application maintains cryptographic integrity.
### Security Impact
- **Vulnerability**: SHA-1 collision attacks ([CVE-2020-0601](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-0601))
- **CWE**: [CWE-327](https://cwe.mitre.org/data/definitions/327.html) (Use of a Broken or Risky Cryptographic Algorithm)
- **OWASP**: A02 - Cryptographic Failures
- **Severity**: Critical
## Installation & Usage
```bash
# Run directly from registry
npx codemod@latest run @codemod/cryptojs-sha1-to-sha256
# Or install and run locally
npm install -g @codemod/cryptojs-sha1-to-sha256
codemod run @codemod/cryptojs-sha1-to-sha256
```
## Transformations
### ES6/ESM Imports
**Before:**
```javascript
import { SHA1 } from 'crypto-js';
const hash = SHA1('hello world');
```
**After:**
```javascript
import { SHA256 } from 'crypto-js';
const hash = SHA256('hello world');
```
### Default Imports
**Before:**
```javascript
import CryptoJS from 'crypto-js';
const hash = CryptoJS.SHA1('data');
```
**After:**
```javascript
import CryptoJS from 'crypto-js';
const hash = CryptoJS.SHA256('data');
```
### CommonJS Destructuring
**Before:**
```javascript
const { SHA1 } = require('crypto-js');
const hash = SHA1('message');
```
**After:**
```javascript
const { SHA256 } = require('crypto-js');
const hash = SHA256('message');
```
### Method Chaining
**Before:**
```javascript
const result = CryptoJS.SHA1(data).toString('hex');
```
**After:**
```javascript
const result = CryptoJS.SHA256(data).toString('hex');
```
### Variable References
**Before:**
```javascript
const hasher = CryptoJS.SHA1;
const hash = hasher('input');
```
**After:**
```javascript
const hasher = CryptoJS.SHA256;
const hash = hasher('input');
```
### Aliased Imports
**Before:**
```javascript
import { SHA1 as sha1Hash } from 'crypto-js';
const hash = sha1Hash('data');
```
**After:**
```javascript
import { SHA256 as sha1Hash } from 'crypto-js';
const hash = sha1Hash('data'); // Function calls use the alias
```
## What Won't Be Transformed
- **String literals** containing "SHA1" (preserved intentionally)
- **Comments** mentioning SHA1
- **Custom functions** named SHA1 (not from crypto-js)
- **Already upgraded** SHA256 usage
- **Test files** (configurable exclusion)
## Important Notes
ā ļø **Hash Length Change**: SHA-256 produces 64-character hex strings vs SHA-1's 40-character strings. Review any code that depends on specific hash lengths.
ā
**API Compatibility**: The CryptoJS API remains the same - only the algorithm changes.
š **Manual Review Recommended**: While this codemod handles most cases automatically, review the changes to ensure correctness in your specific context.
## File Types Supported
- `.js` - JavaScript
- `.ts` - TypeScript
- `.jsx` - React JSX
- `.tsx` - React TypeScript JSX
## Excluded Patterns
The codemod automatically excludes:
- `node_modules/`
- Test files (`*.test.*`, `*.spec.*`)
- Test directories (`test/`, `tests/`, `__tests__/`)
## Development
```bash
# Run tests
pnpm test
# Test on specific case
pnpm test --filter <test-name>
# Update expected outputs
pnpm test -u
# Validate workflow
npx codemod validate -w workflow.yaml
```
## Security References
- [CWE-327: Use of a Broken or Risky Cryptographic Algorithm](https://cwe.mitre.org/data/definitions/327.html)
- [CWE-328: Use of Weak Hash](https://cwe.mitre.org/data/definitions/328.html)
- [CVE-2020-0601: Windows CryptoAPI Spoofing Vulnerability](https://nvd.nist.gov/vuln/detail/CVE-2020-0601)
- [RFC 6194: Security Considerations for the SHA-0 and SHA-1 Message-Digest Algorithms](https://tools.ietf.org/rfc/rfc6194.txt)
- [NIST Policy on Hash Functions](https://csrc.nist.gov/projects/hash-functions)
## License
MIT
---
**Powered by [Codemod](https://codemod.com)** | **Built with [ast-grep](https://ast-grep.github.io/)**