How to use the scryptSync function from crypto

Find comprehensive JavaScript crypto.scryptSync code examples handpicked from public code repositorys.

204
205
206
207
208
209
210
211
212
213
214
215
  crypto.DEFAULT_ENCODING = defaultEncoding;
}


for (const { args, expected } of badargs) {
  assert.throws(() => crypto.scrypt(...args), expected);
  assert.throws(() => crypto.scryptSync(...args), expected);
}


{
  const expected = { code: 'ERR_INVALID_ARG_TYPE' };
fork icon42
star icon19
watch icon0

+ 21 other calls in file

2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Encrypter {


  constructor(encryptionKey) {


    this.algorithm = 'aes-256-cbc';
    this.key = crypto.scryptSync(encryptionKey, 'salt', 32);


  }


  encrypt(clearText) {
fork icon1
star icon15
watch icon0

12
13
14
15
16
17
18
19
20
21
22


function login(email, password) {
    const user = users.find(v => v.email === email);
  
    const [salt, key] = user.password.split(':');
    const hashedBuffer = scryptSync(password, salt, 64);
  
    const keyBuffer = Buffer.from(key, 'hex');
    const match = timingSafeEqual(hashedBuffer, keyBuffer);
    
fork icon0
star icon3
watch icon0

+ 3 other calls in file

103
104
105
106
107
108
109
110
111
112
    }
}

encrypt(data) {
    const iv = crypto.randomBytes(16);
    const key = crypto.scryptSync(this.secret, iv, 32);
    const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
    const encrypted = cipher.update(JSON.stringify(data), 'utf8', 'base64');
    return iv.toString('base64') + '.' + encrypted + cipher.final('base64');
}
fork icon0
star icon0
watch icon1

30
31
32
33
34
35
36
37
38
39
40
41
});


app.post(["/login"], function(req, res){
    console.log(req.body);
    var user = req.body.username;
    var password = crypto.scryptSync(req.body.password, "iewhrg3yYYDAjert377999", 32).toString('hex');
    console.log(password);


    query = `
    SELECT * FROM users
fork icon0
star icon0
watch icon0

+ 3 other calls in file

57
58
59
60
61
62
63
64
65
66
encrypt: function(data, password, enc) {
    const config = require('../setup/config');
    password = typeof password == 'string' ? password : config.secret;
    enc = typeof enc == 'string' ? enc : 'base64';
    const iv = randomBytes(16);
    const key = scryptSync(password, iv, 32);
    const cipher = createCipheriv('aes-256-cbc', key, iv);
    const encrypted = cipher.update(data, 'utf8', enc);
    return iv.toString(enc) + '.' + encrypted + cipher.final(enc);
},
fork icon0
star icon0
watch icon0

+ 3 other calls in file

12
13
14
15
16
17
18
19
20
21
22
23


function login(email, password) {
    const user = users.find(v => v.email === email);
    const [salt, pw] = user.password.split(':');
    // Make the hashedBuffer (not a hex string) because we need a buffer to use timingSafeEqual
    const hashedBuffer = scryptSync(password, salt, 64).toString('hex')// use the salt to make hash from the given password
    return hashedBuffer === pw; 
}


const users = [];
fork icon0
star icon0
watch icon0

+ 3 other calls in file