-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[_]: feature/Switch to argon2 #242
Draft
CandelR
wants to merge
7
commits into
master
Choose a base branch
from
feature/switch-to-argon2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+485
−98
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
81a7a4f
Switched to argon2
362c492
Added call to update hash when login to new argon2 hash
b3dad56
Minor change
848dd71
Reduced duplicated lines in passToHash tests
83c6f50
Merge branch 'master' into feature/switch-to-argon2
CandelR 41a07ef
Update logic to login with new argon2 hash
CandelR 6f2bfc9
Merge branch 'master' into feature/switch-to-argon2
CandelR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,31 @@ | ||
import CryptoJS from 'crypto-js'; | ||
import crypto from 'react-native-crypto'; | ||
import { argon2id, createSHA1, pbkdf2 } from 'hash-wasm'; | ||
// import crypto from 'react-native-crypto'; | ||
import * as crypto from 'crypto'; | ||
import { constants } from '../../services/AppService'; | ||
import errorService from '../../services/ErrorService'; | ||
import AesUtils from '../aesUtils'; | ||
|
||
const password = constants.CRYPTO_SECRET || ''; // Force env var loading | ||
|
||
/** | ||
* Argon2id parameters taken from RFC9106 (variant for memory-constrained environments) | ||
* * @constant | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think two ** here is a typo |
||
* @type {number} | ||
* @default | ||
*/ | ||
const ARGON2ID_PARALLELISM = 4; | ||
const ARGON2ID_ITERATIONS = 3; | ||
const ARGON2ID_MEMORY = 65536; | ||
const ARGON2ID_TAG_LEN = 32; | ||
const ARGON2ID_SALT_LEN = 16; | ||
|
||
const PBKDF2_ITERATIONS = 10000; | ||
const PBKDF2_TAG_LEN = 32; | ||
|
||
interface PassObjectInterface { | ||
salt?: string | null; | ||
password: string; | ||
salt?: string; | ||
} | ||
|
||
export function passToHash(passObject: PassObjectInterface): { salt: string; hash: string } { | ||
const salt = passObject.salt ? CryptoJS.enc.Hex.parse(passObject.salt) : CryptoJS.lib.WordArray.random(128 / 8); | ||
const hash = CryptoJS.PBKDF2(passObject.password, salt, { keySize: 256 / 32, iterations: 10000 }); | ||
const hashedObjetc = { | ||
salt: salt.toString(), | ||
hash: hash.toString(), | ||
}; | ||
|
||
return hashedObjetc; | ||
} | ||
|
||
// AES Plain text encryption method | ||
|
@@ -56,32 +62,6 @@ export function decryptTextWithKey(encryptedText: string, keyToDecrypt: string): | |
} | ||
} | ||
|
||
export function probabilisticEncryption(content: string): string | null { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. those functions are not used, right? |
||
try { | ||
const b64 = crypto.createCipher('aes-256-gcm', constants.CRYPTO_SECRET); | ||
|
||
b64.write(content); | ||
|
||
const e64 = Buffer.concat([b64.update(content), b64.final()]).toString('base64'); | ||
const eHex = Buffer.from(e64, 'base64').toString('hex'); | ||
|
||
return eHex; | ||
} catch (error) { | ||
return null; | ||
} | ||
} | ||
|
||
export function probabilisticDecryption(cipherText: string): string | null { | ||
try { | ||
const decrypt = crypto.createDecipher('aes-256-gcm', constants.CRYPTO_SECRET); | ||
const plain = Buffer.concat([decrypt.update(cipherText), decrypt.final()]).toString('utf8'); | ||
|
||
return plain; | ||
} catch (error) { | ||
return null; | ||
} | ||
} | ||
|
||
export function isValidFilename(filename: string) { | ||
const EXCLUDED = ['..']; | ||
if (EXCLUDED.includes(filename)) { | ||
|
@@ -104,31 +84,102 @@ export function encryptFilename(filename: string, folderId: string): string { | |
return AesUtils.encrypt(filename, `${CRYPTO_KEY}-${folderId}`); | ||
} | ||
|
||
export function deterministicEncryption(content: string, salt?: string | number): string | null { | ||
try { | ||
const key = Buffer.from(constants.CRYPTO_SECRET as string).toString('hex'); | ||
const iv = salt ? Buffer.from(salt.toString()).toString('hex') : key; | ||
const encrypt = crypto.createCipheriv('aes-256-gcm', key, iv); | ||
const b64 = Buffer.concat([encrypt.update(content), encrypt.final()]).toString('base64'); | ||
const eHex = Buffer.from(b64).toString('hex'); | ||
|
||
return eHex; | ||
} catch (e) { | ||
return null; | ||
/** | ||
* Computes PBKDF2 and outputs the result in HEX format | ||
* @param {string} password - The password | ||
* @param {number} salt - The salt | ||
* @param {number}[iterations=PBKDF2_ITERATIONS] - The number of iterations to perform | ||
* @param {number} [hashLength=PBKDF2_TAG_LEN] - The desired output length | ||
* @returns {Promise<string>} The result of PBKDF2 in HEX format | ||
*/ | ||
export function getPBKDF2( | ||
password: string, | ||
salt: string | Uint8Array, | ||
iterations = PBKDF2_ITERATIONS, | ||
hashLength = PBKDF2_TAG_LEN, | ||
): Promise<string> { | ||
return pbkdf2({ | ||
password, | ||
salt, | ||
iterations, | ||
hashLength, | ||
hashFunction: createSHA1(), | ||
outputType: 'hex', | ||
}); | ||
} | ||
|
||
/** | ||
* Computes Argon2 and outputs the result in HEX format | ||
* @param {string} password - The password | ||
* @param {number} salt - The salt | ||
* @param {number} [parallelism=ARGON2ID_PARALLELISM] - The parallelism degree | ||
* @param {number}[iterations=ARGON2ID_ITERATIONS] - The number of iterations to perform | ||
* @param {number}[memorySize=ARGON2ID_MEMORY] - The number of KB of memeory to use | ||
* @param {number} [hashLength=ARGON2ID_TAG_LEN] - The desired output length | ||
* @param {'hex'|'binary'|'encoded'} [outputType="encoded"] - The output type | ||
* @returns {Promise<string>} The result of Argon2 | ||
*/ | ||
export function getArgon2( | ||
password: string, | ||
salt: string, | ||
outputType: 'hex' | 'binary' | 'encoded' = 'encoded', | ||
parallelism: number = ARGON2ID_PARALLELISM, | ||
iterations: number = ARGON2ID_ITERATIONS, | ||
memorySize: number = ARGON2ID_MEMORY, | ||
hashLength: number = ARGON2ID_TAG_LEN, | ||
): Promise<string> { | ||
return argon2id({ | ||
password, | ||
salt, | ||
parallelism, | ||
iterations, | ||
memorySize, | ||
hashLength, | ||
outputType, | ||
}); | ||
} | ||
|
||
/** | ||
* Converts HEX string to Uint8Array the same way CryptoJS did it (for compatibility) | ||
* @param {string} hex - The input string in HEX | ||
* @returns {Uint8Array} The resulting Uint8Array identical to what CryptoJS previously did | ||
*/ | ||
export function hex2oldEncoding(hex: string): Uint8Array { | ||
const words: number[] = []; | ||
for (let i = 0; i < hex.length; i += 8) { | ||
words.push(parseInt(hex.slice(i, i + 8), 16) | 0); | ||
} | ||
const sigBytes = hex.length / 2; | ||
const uint8Array = new Uint8Array(sigBytes); | ||
|
||
for (let i = 0; i < sigBytes; i++) { | ||
uint8Array[i] = (words[i >>> 2] >>> ((3 - (i % 4)) * 8)) & 0xff; | ||
} | ||
|
||
return uint8Array; | ||
} | ||
|
||
export function deterministicDecryption(cipherText: string, salt?: string | number): string | null { | ||
try { | ||
const key = Buffer.from(constants.CRYPTO_SECRET as string).toString('hex'); | ||
const iv = salt ? Buffer.from(salt.toString()).toString('hex') : key; | ||
const reb64 = Buffer.from(cipherText).toString('hex'); | ||
const bytes = Buffer.from(reb64).toString('base64'); | ||
const decrypt = crypto.createDecipheriv('aes-256-gcm', key, iv); | ||
const plain = Buffer.concat([decrypt.update(Buffer.from(bytes)), decrypt.final()]).toString('utf8'); | ||
|
||
return plain; | ||
} catch (e) { | ||
return null; | ||
/** | ||
* Password hash computation. If no salt or salt starts with 'argon2id$' - uses Argon2, else - PBKDF2 | ||
* @param {PassObjectInterface} passObject - The input object containing password and salt (optional) | ||
* @returns {Promise<{salt: string; hash: string }>} The resulting hash and salt | ||
*/ | ||
export async function passToHash(passObject: PassObjectInterface): Promise<{ salt: string; hash: string }> { | ||
let salt; | ||
let hash; | ||
|
||
if (!passObject.salt) { | ||
const argonSalt = crypto.randomBytes(ARGON2ID_SALT_LEN).toString('hex'); | ||
hash = await getArgon2(passObject.password, argonSalt, 'hex'); | ||
salt = 'argon2id$' + argonSalt; | ||
} else if (passObject.salt.startsWith('argon2id$')) { | ||
const argonSalt = passObject.salt.replace('argon2id$', ''); | ||
hash = await getArgon2(passObject.password, argonSalt, 'hex'); | ||
salt = passObject.salt; | ||
} else { | ||
salt = passObject.salt; | ||
const encoded = hex2oldEncoding(salt); | ||
hash = await getPBKDF2(passObject.password, encoded); | ||
} | ||
return { salt, hash }; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wasm works or do we need a kotlin lib?