Wednesday 7 June 2017

crypto.randomBytes does not accept md5 generated utf8 string

My code is as shown below:

let createCipher = (req, res) => {
    const token = req.body.token;
    let keyVal = req.body.keyVal;
    const codeToken = utf8.encode(token);
    keyVal = utf8.encode(keyVal);
    console.log("keyVal " + keyVal);
    let hash = crypto.createHash('md5').update(codeToken).digest('hex');
    console.log("hash " + hash);
    var sharedSecret = crypto.randomBytes(hash);
    var initializationVector = crypto.randomBytes(hash);
    console.log("iv " + initializationVector);
    var encrypted;
    cipher = crypto.Cipheriv('aes-128-cbc', sharedSecret, initializationVector);
    encrypted += cipher.update(keyVal, 'utf8', 'base64');
    encrypted += cipher.final('base64');
    res.json({
        status: '200',
        cipher: encrypted
    });
}

I have written the above code as per the steps given below:

  1. UTF-8 encode the TempToken string and generate an MD5 hash of it.
  2. UTF-8 encode the Key-Value Pair string and encrypt using AES-128 encryption using Cipher Block Chaining (CBC) mode. a. Set both the key and initialization vector (IV) equal to result from step 1.
  3. Base64 encode the result of 2

But the above code gives me the error as shown below:

TypeError: size must be a number >= 0
            <br> &nbsp; &nbsp;at TypeError (native)
            <br> &nbsp; &nbsp;at createCipher (C:\Users\anand\quFlipApi\controller\test.js:17:31)
            <br> &nbsp; &nbsp;at Layer.handle [as handle_request] (C:\Users\anand\quFlipApi\node_modules\express\lib\router\layer.js:95:5)
            <br> &nbsp; &nbsp;at trim_prefix (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:317:13)
            <br> &nbsp; &nbsp;at C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:284:7
            <br> &nbsp; &nbsp;at Function.process_params (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:335:12)
            <br> &nbsp; &nbsp;at next (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:275:10)
            <br> &nbsp; &nbsp;at C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:635:15
            <br> &nbsp; &nbsp;at next (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:260:14)
            <br> &nbsp; &nbsp;at Function.handle (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:174:3)
            <br> &nbsp; &nbsp;at router (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:47:12)
            <br> &nbsp; &nbsp;at Layer.handle [as handle_request] (C:\Users\anand\quFlipApi\node_modules\express\lib\router\layer.js:95:5)
            <br> &nbsp; &nbsp;at trim_prefix (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:317:13)
            <br> &nbsp; &nbsp;at C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:284:7
            <br> &nbsp; &nbsp;at Function.process_params (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:335:12)
            <br> &nbsp; &nbsp;at next (C:\Users\anand\quFlipApi\node_modules\express\lib\router\index.js:275:10)
            <br> &nbsp; &nbsp;at C:\Users\anand\quFlipApi\node_modules\body-parser\lib\read.js:129:5
            <br> &nbsp; &nbsp;at invokeCallback (C:\Users\anand\quFlipApi\node_modules\raw-body\index.js:262:16)
            <br> &nbsp; &nbsp;at done (C:\Users\anand\quFlipApi\node_modules\raw-body\index.js:251:7)
            <br> &nbsp; &nbsp;at IncomingMessage.onEnd (C:\Users\anand\quFlipApi\node_modules\raw-body\index.js:307:7)
            <br> &nbsp; &nbsp;at emitNone (events.js:86:13)
            <br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:185:7)

Here my code does not accept hash generated from previous step in crypto.randomBytes(hash). Is there any method missing in this execution?



via M thaker

No comments:

Post a Comment