How to use the lib function from crypto-js

Find comprehensive JavaScript crypto-js.lib code examples handpicked from public code repositorys.

83
84
85
86
87
88
89
90
91
92
}

function ripemd160(data) {
    let temp = data;
    if (!Buffer.isBuffer(data) && data instanceof Uint8Array) {
        temp = CryptoJS.lib.WordArray.create(temp);
    } else if (Buffer.isBuffer(data)) {
        temp = CryptoJS.lib.WordArray.create(new Uint8Array(temp));
    }
    
fork icon129
star icon11
watch icon4

+ 27 other calls in file

47
48
49
50
51
52
53
54
55
56
 * Return ripemd160 hash of input.
 */
function ripemd160(input) {
    // https://github.com/mahdiyari/hive-tx-js/blob/master/helpers/crypto.js
    if (typeof input !== 'string')
        input = CryptoJS.lib.WordArray.create(input)
    const hash = Buffer.from(CryptoJS.RIPEMD160(input).toString(CryptoJS.enc.Hex),'hex')
    return hash
    // return CryptoOld.createHash('rmd160').update(input).digest()
}
fork icon0
star icon5
watch icon3

+ 11 other calls in file

28
29
30
31
32
33
34
35
36
37
ciphertext.sigBytes -= 8

let salt
if (ciphertextWords[0] == SaltMark[0] && ciphertextWords[1] == SaltMark[1]) {
    // Extract salt
    salt = CryptoJS.lib.WordArray.create(ciphertextWords.slice(2, 4));

    // Remove salt from ciphertext
    ciphertextWords.splice(0, 4);
    ciphertext.sigBytes -= 16;
fork icon0
star icon2
watch icon1

+ 11 other calls in file

212
213
214
215
216
217
218
219
220

If you disabled the new user signup as suggested at the end of the first part of the tutorial then you'll have a slightly harder time creating a new user (the Signup page is still enabled in the example repo for convenience). You could create one with the Redwood console, but you'll need to be clever—remember that we don't store the original password, just the hashed result when combined with a salt. Here's the commands to enter at the console for creating a new user (replace 'password' with your password of choice):

```javascript
const CryptoJS = require('crypto-js')
const salt = CryptoJS.lib.WordArray.random(128 / 8).toString()
const hashedPassword = CryptoJS.PBKDF2('password', salt, { keySize: 256 / 32 }).toString()
db.user.create({ data: { email: 'moderator@moderator.com', hashedPassword, salt } })
```
fork icon882
star icon0
watch icon0

112
113
114
115
116
117
118
119
120
121
function encrypt(data, password, pbkdf2_iterations) {
  assert(data, "data missing");
  assert(password, "password missing");
  assert(pbkdf2_iterations, "pbkdf2_iterations missing");

  var salt = CryptoJS.lib.WordArray.random(16);
  var stretched_password = stretchPassword(password, salt, pbkdf2_iterations);
  var iv = salt;
  var payload = CryptoJS.enc.Utf8.parse(data);
  var encrypted = CryptoJS.AES.encrypt(payload, stretched_password, {
fork icon16
star icon17
watch icon6

12
13
14
15
16
17
18
19
20
21
  newKey.set(key.subarray(0, 8), 16);
  return newKey;
}

export function ecb3des(encKey: Uint8Array, data: Uint8Array) : Uint8Array {
  let dataWArray = CryptoJS.lib.WordArray.create(data);
  let sessionEncKeyWArray = CryptoJS.lib.WordArray.create(resizeKey24(encKey));
  let encData = CryptoJS.TripleDES.encrypt(dataWArray, sessionEncKeyWArray, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding});

  return CryptoUtils.wordArrayToByteArray(encData.ciphertext);
fork icon0
star icon5
watch icon2

+ 31 other calls in file

32
33
34
35
36
37
38
39
40
41
 */
public static toMobileKey = (password, privateKey) => {
    // Errors
    if (!password || !privateKey) { throw new Error('Missing argument !'); }
    // Processing
    const salt = CryptoJS.lib.WordArray.random(256 / 8);
    const key = CryptoJS.PBKDF2(password, salt, {
        keySize: 256 / 32,
        iterations: 2000,
    });
fork icon61
star icon0
watch icon2

4
5
6
7
8
9
10
11
12
13
const sha256 = require('crypto-js/sha256')
const ripemd160 = require('crypto-js/ripemd160')
const CryptoJS = require('crypto-js')

function standardRandomBytesFunc(size) {
  return CryptoJS.lib.WordArray.random(size).toString()
}

async function generateWalletFromSeed(mnemonic, bip) {
  const masterKey = await deriveMasterKey(mnemonic)
fork icon39
star icon0
watch icon1

44
45
46
47
48
49
50
51
52
53
54
  'csp_pdt_pubkey':'02ffef6766b43225e273a5da598037c1787b3b9c1043e99b27a780d06d0ae367bf',
  'csp_selector': 'www.fn-share.com/crypto_host',
};


function wrapCryptoBuf(msg) {
  if (msg.words instanceof Array)  // msg is instance of CryptoJS.lib.WordArray
    return msg;
  else if (msg.buffer instanceof ArrayBuffer)  // msg is instance of Buffer
    return CryptoJS.lib.WordArray.create(msg);
  else return CryptoJS.enc.Utf8.parse(msg);    // assume msg is utf-8 string
fork icon0
star icon0
watch icon2

+ 27 other calls in file

3
4
5
6
7
8
9
10
11
12
const ivSize = 128;
const iterations = 100;

module.exports = {
    encrypt(data, encryptionPassword) {
        var salt = CryptoJS.lib.WordArray.random(128 / 8);
        var key = CryptoJS.PBKDF2(encryptionPassword, salt, {
            keySize: keySize / 32,
            iterations: iterations
        });
fork icon0
star icon0
watch icon4

+ 5 other calls in file

25
26
27
28
29
30
31
32
33
34
  return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
};

AesUtil.prototype.decrypt = function(salt, iv, passPhrase, cipherText) {
  var key = this.generateKey(salt, passPhrase);
  var cipherParams = CryptoJS.lib.CipherParams.create({
    ciphertext: CryptoJS.enc.Base64.parse(cipherText)
  });
  var decrypted = CryptoJS.AES.decrypt(
      cipherParams,
fork icon0
star icon0
watch icon1