2022-08-26 16:19:39 +05:00
|
|
|
import { Platform } from "react-native";
|
|
|
|
|
import "react-native-get-random-values";
|
|
|
|
|
import * as Keychain from "react-native-keychain";
|
|
|
|
|
import { generateSecureRandom } from "react-native-securerandom";
|
|
|
|
|
import Sodium from "react-native-sodium";
|
2022-02-28 13:48:59 +05:00
|
|
|
|
|
|
|
|
const KEYSTORE_CONFIG = Platform.select({
|
|
|
|
|
ios: {
|
|
|
|
|
accessible: Keychain.ACCESSIBLE.WHEN_UNLOCKED_THIS_DEVICE_ONLY
|
|
|
|
|
},
|
|
|
|
|
android: {}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export async function deriveCryptoKey(name, data) {
|
|
|
|
|
try {
|
|
|
|
|
let credentials = await Sodium.deriveKey(data.password, data.salt);
|
2022-08-26 16:19:39 +05:00
|
|
|
await Keychain.setInternetCredentials(
|
|
|
|
|
"notesnook",
|
|
|
|
|
name,
|
|
|
|
|
credentials.key,
|
|
|
|
|
KEYSTORE_CONFIG
|
|
|
|
|
);
|
2022-02-28 13:48:59 +05:00
|
|
|
return credentials.key;
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getCryptoKey(name) {
|
|
|
|
|
try {
|
2022-08-26 16:19:39 +05:00
|
|
|
if (await Keychain.hasInternetCredentials("notesnook")) {
|
|
|
|
|
let credentials = await Keychain.getInternetCredentials(
|
|
|
|
|
"notesnook",
|
|
|
|
|
KEYSTORE_CONFIG
|
|
|
|
|
);
|
2022-02-28 13:48:59 +05:00
|
|
|
return credentials.password;
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function removeCryptoKey(name) {
|
|
|
|
|
try {
|
2022-08-26 16:19:39 +05:00
|
|
|
let result = await Keychain.resetInternetCredentials("notesnook");
|
2022-02-28 13:48:59 +05:00
|
|
|
return result;
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getRandomBytes(length) {
|
|
|
|
|
return await generateSecureRandom(length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function hash(password, email) {
|
|
|
|
|
let result = await Sodium.hashPassword(password, email);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function generateCryptoKey(password, salt) {
|
|
|
|
|
try {
|
|
|
|
|
let credentials = await Sodium.deriveKey(password, salt || null);
|
|
|
|
|
return credentials;
|
|
|
|
|
} catch (e) {
|
2022-08-26 16:19:39 +05:00
|
|
|
console.log("generateCryptoKey: ", e);
|
2022-02-28 13:48:59 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getAlgorithm(base64Variant) {
|
|
|
|
|
return `xcha-argon2i13-${base64Variant}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function decrypt(password, data) {
|
|
|
|
|
if (!password.password && !password.key) return undefined;
|
2022-08-26 16:19:39 +05:00
|
|
|
if (password.password && password.password === "" && !password.key)
|
|
|
|
|
return undefined;
|
2022-02-28 13:48:59 +05:00
|
|
|
let _data = { ...data };
|
2022-08-26 16:19:39 +05:00
|
|
|
_data.output = "plain";
|
2022-02-28 13:48:59 +05:00
|
|
|
return await Sodium.decrypt(password, _data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function parseAlgorithm(alg) {
|
|
|
|
|
if (!alg) return {};
|
2022-08-26 16:19:39 +05:00
|
|
|
const [enc, kdf, compressed, compressionAlg, base64variant] = alg.split("-");
|
2022-02-28 13:48:59 +05:00
|
|
|
return {
|
|
|
|
|
encryptionAlgorithm: enc,
|
|
|
|
|
kdfAlgorithm: kdf,
|
|
|
|
|
compressionAlgorithm: compressionAlg,
|
2022-08-26 16:19:39 +05:00
|
|
|
isCompress: compressed === "1",
|
2022-02-28 13:48:59 +05:00
|
|
|
base64_variant: base64variant
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function encrypt(password, data) {
|
|
|
|
|
if (!password.password && !password.key) return undefined;
|
2022-08-26 16:19:39 +05:00
|
|
|
if (password.password && password.password === "" && !password.key)
|
|
|
|
|
return undefined;
|
2022-02-28 13:48:59 +05:00
|
|
|
|
|
|
|
|
let message = {
|
2022-08-26 16:19:39 +05:00
|
|
|
type: "plain",
|
2022-02-28 13:48:59 +05:00
|
|
|
data: data
|
|
|
|
|
};
|
|
|
|
|
let result = await Sodium.encrypt(password, message);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...result,
|
|
|
|
|
alg: getAlgorithm(7)
|
|
|
|
|
};
|
|
|
|
|
}
|