Implementation using modern libraries
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- javascript
- Domain
- cryptography
Research direction
The issue names no repository files, entry points, or tests; start by inspecting the package API and current implementation to determine whether the proposed JavaScript encryption and decryption functions fit this project. The issue provides example code but no acceptance criteria, so the intended API, compatibility requirements, and tests for completion need to be clarified first.
Written by the indexing model from the issue text.
Description
For all those who are looking for a replacement for the following code:
import AES from 'crypto-js/aes.js';
import Utf8 from 'crypto-js/enc-utf8.js';
function encrypt(data, key) {
return AES.encrypt(data, key).toString();
}
function decrypt(data, key) {
return AES.decrypt(data, key).toString(Utf8);
}
const data = 'This is a secret message';
const password = '12345';
data === decrypt(encrypt(data, password), password);
Below is an implementation using modern libraries:
import { md5 } from '@noble/hashes/legacy.js';
import { cbc } from '@noble/ciphers/aes.js';
import { base64 } from '@scure/base'
import {
abytes,
bytesToUtf8,
concatBytes,
randomBytes,
utf8ToBytes,
} from '@noble/hashes/utils.js';
export function evpBytesToKey(password, salt = new Uint8Array(0), keyLen = 32, ivLen = 16, count = 1) {
abytes(password);
abytes(salt, 0, 8);
let derived = new Uint8Array(0);
let block = new Uint8Array(0);
while (derived.length < (keyLen + ivLen)) {
block = md5(concatBytes(block, password, salt));
for (let i = 1; i < count; i++) {
block = md5(block);
}
derived = concatBytes(derived, block);
}
return {
key: derived.slice(0, keyLen),
iv: derived.slice(keyLen, keyLen + ivLen)
};
}
export function encrypt(data, password) {
const salt = randomBytes(8);
const { key, iv } = evpBytesToKey(utf8ToBytes(password), salt, 32, 16);
const encrypted = concatBytes(
utf8ToBytes('Salted__'),
salt,
cbc(key, iv).encrypt(utf8ToBytes(data))
);
return base64.encode(encrypted);
}
export function decrypt(data, password) {
const encrypted = base64.decode(data);
const salt = encrypted.slice(8, 16);
const { key, iv } = evpBytesToKey(utf8ToBytes(password), salt, 32, 16);
return bytesToUtf8(cbc(key, iv).decrypt(encrypted.slice(16)));
}
- Dominant language
- JavaScript
- Stars
- 21
- Forks
- 7
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
HarperFast/skills#96 ·
-
[Block] Latest Posts [Type] Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Automattic/studio#4908 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
sugarlabs/musicblocks#8847 ·