Skip to content
Snippets Groups Projects
Commit 46c25f3b authored by Éloïs's avatar Éloïs
Browse files

[enh] scrypt: keypair generation use duniteroxyde instead of tweetnacl

parent 01690ecf
No related branches found
No related tags found
1 merge request!1292Feature/oxyde crypto
......@@ -11,11 +11,8 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
import {Base58encode} from "../../../lib/common-libs/crypto/base58"
import {decodeBase64} from "../../../lib/common-libs/crypto/nacl-util"
import * as crypto from 'crypto'
const nacl = require('tweetnacl');
import { KeyPairBuilder, seedToSecretKey } from 'duniteroxyde'
const SEED_LENGTH = 32; // Length of the key
......@@ -29,20 +26,16 @@ const SEED_LENGTH = 32; // Length of the key
* @return keyPair An object containing the public and private keys, base58 encoded.
*/
export const Scrypt = async (salt:string, key:string, N = 4096, r = 16, p = 1) => {
const keyBytes = await getScryptKey(key, salt, N, r, p)
const pair = nacl.sign.keyPair.fromSeed(keyBytes);
return {
pub: Base58encode(new Buffer(pair.publicKey, 'hex')),
sec: Base58encode(new Buffer(pair.secretKey, 'hex'))
};
}
const getScryptKey = async (key:string, salt:string, N:number, r:number, p:number) => {
const res:any = await new Promise((resolve, reject) => {
crypto.scrypt(key, salt, SEED_LENGTH, { N, r, p }, (err:any, res:Buffer) => {
const res: { pub: string, sec: string } = await new Promise((resolve, reject) => {
crypto.scrypt(key, salt, SEED_LENGTH, { N, r, p }, (err:any, seed:Buffer) => {
if (err) return reject(err)
resolve(res)
const pair = KeyPairBuilder.fromSeed(seed);
resolve({
pub: pair.getPublicKey(),
sec: seedToSecretKey(seed)
})
})
})
return decodeBase64(res.toString("base64"))
return res;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment