How does nodejs crypto method chaining work?












0















What do each of the method chained calls in following example code do?:



var crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
.update('I love cupcakes')
.digest('base64');
console.log(hash);

// Prints:
// wPobwAUxvXjvOMYoRJxRAq6r1Jtdw6KlFupuqVnWZY4=


I'm trying to do similarly in Swift and not able to get the final hash.



What I thought it was doing was the following:



1) crypto.createHmac('sha256', secret) //initialize the crypto object



2) .update('I love cupcakes') // sha256 hash 'I love cupcakes' with the secret key



3) digest('base64') //base 64 encode the result from 2



However this is not the case as doing those operations at the command line give a different result.



Can anyone help?










share|improve this question























  • This isn't an answer, but for search terms this is called a fluent interface using method chaining.

    – TypeIA
    Nov 14 '18 at 22:01
















0















What do each of the method chained calls in following example code do?:



var crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
.update('I love cupcakes')
.digest('base64');
console.log(hash);

// Prints:
// wPobwAUxvXjvOMYoRJxRAq6r1Jtdw6KlFupuqVnWZY4=


I'm trying to do similarly in Swift and not able to get the final hash.



What I thought it was doing was the following:



1) crypto.createHmac('sha256', secret) //initialize the crypto object



2) .update('I love cupcakes') // sha256 hash 'I love cupcakes' with the secret key



3) digest('base64') //base 64 encode the result from 2



However this is not the case as doing those operations at the command line give a different result.



Can anyone help?










share|improve this question























  • This isn't an answer, but for search terms this is called a fluent interface using method chaining.

    – TypeIA
    Nov 14 '18 at 22:01














0












0








0


1






What do each of the method chained calls in following example code do?:



var crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
.update('I love cupcakes')
.digest('base64');
console.log(hash);

// Prints:
// wPobwAUxvXjvOMYoRJxRAq6r1Jtdw6KlFupuqVnWZY4=


I'm trying to do similarly in Swift and not able to get the final hash.



What I thought it was doing was the following:



1) crypto.createHmac('sha256', secret) //initialize the crypto object



2) .update('I love cupcakes') // sha256 hash 'I love cupcakes' with the secret key



3) digest('base64') //base 64 encode the result from 2



However this is not the case as doing those operations at the command line give a different result.



Can anyone help?










share|improve this question














What do each of the method chained calls in following example code do?:



var crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
.update('I love cupcakes')
.digest('base64');
console.log(hash);

// Prints:
// wPobwAUxvXjvOMYoRJxRAq6r1Jtdw6KlFupuqVnWZY4=


I'm trying to do similarly in Swift and not able to get the final hash.



What I thought it was doing was the following:



1) crypto.createHmac('sha256', secret) //initialize the crypto object



2) .update('I love cupcakes') // sha256 hash 'I love cupcakes' with the secret key



3) digest('base64') //base 64 encode the result from 2



However this is not the case as doing those operations at the command line give a different result.



Can anyone help?







javascript node.js cryptography hmac






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 21:55









bhartsbbhartsb

465322




465322













  • This isn't an answer, but for search terms this is called a fluent interface using method chaining.

    – TypeIA
    Nov 14 '18 at 22:01



















  • This isn't an answer, but for search terms this is called a fluent interface using method chaining.

    – TypeIA
    Nov 14 '18 at 22:01

















This isn't an answer, but for search terms this is called a fluent interface using method chaining.

– TypeIA
Nov 14 '18 at 22:01





This isn't an answer, but for search terms this is called a fluent interface using method chaining.

– TypeIA
Nov 14 '18 at 22:01












1 Answer
1






active

oldest

votes


















0














var crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
.update('I love cupcakes')
.digest('base64');
console.log(hash);

// Prints:
// wPobwAUxvXjvOMYoRJxRAq6r1Jtdw6KlFupuqVnWZY4=



  1. crypto.createHmac('sha256', secret) - Self explanatory initializes a crypto hmac object.


  2. .update('I love cupcakes') - The second missing argument is the encoding of the data you are passing in to be hashed, since it is left out it is forced to utf-8 string, you could pass in a buffer and specify the encoding, or a typed array and specify the encoding, etc. The update method can be called multiple times to add additional strings, buffers, etc onto the end of the data being passed in that way if you are receiving chunks of data you can pass them in as they arrive.


  3. digest('base64') - this is the final call after you digest you are essentially saying I am done adding data to this hash and compute the results in the base64 format, you could also pass in other formats to return the hash in. An important thing to remember is that once you call digest the hash is done no more data can be added and you would need to create a new hmac variable to create another hmac, even if using the same parameters.



ex.



const crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret);
hash.update('I ', 'utf8');
hash.update('love ', 'utf8');
hash.update('cupcakes', 'utf8');
hash.digest('base64');
console.log(hash);


This should return the same. If you are using the windows cmd.exe I believe windows uses a different encoding than utf8, you'll have to check on that, or create a node js file passing in arguments and hashing then logging to console instead of using the REPL






share|improve this answer


























  • I'm not clear about what is getting sha256 hashed and when it is applied, and what is getting base64 encoded, and when that is getting performed.

    – bhartsb
    Nov 14 '18 at 23:28











  • @bhartsb the text in the update method is being sha hashed with the secret, the first block of code you do it all in one go, the second block of code just shows that you can keep adding chunks of text together by calling the update method multiple times and passing it sections of the string you want to hash. Once you are done adding all your content to the hash update function you want to read that data, that's where the digest part comes in it takes the raw buffer created that holds the hashed data and converts it to a format of your choice in this case base64.

    – Harry Chilinguerian
    Nov 15 '18 at 1:37











  • Your explanation is good, unfortunately your code example is not equivalent to the original code which returns the result of the hmac operation in the variable hash.

    – James K Polk
    Nov 15 '18 at 3:20











  • Added the returned result that is stored in hash, thanks @James K Polk

    – Harry Chilinguerian
    Nov 19 '18 at 17:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309306%2fhow-does-nodejs-crypto-method-chaining-work%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














var crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
.update('I love cupcakes')
.digest('base64');
console.log(hash);

// Prints:
// wPobwAUxvXjvOMYoRJxRAq6r1Jtdw6KlFupuqVnWZY4=



  1. crypto.createHmac('sha256', secret) - Self explanatory initializes a crypto hmac object.


  2. .update('I love cupcakes') - The second missing argument is the encoding of the data you are passing in to be hashed, since it is left out it is forced to utf-8 string, you could pass in a buffer and specify the encoding, or a typed array and specify the encoding, etc. The update method can be called multiple times to add additional strings, buffers, etc onto the end of the data being passed in that way if you are receiving chunks of data you can pass them in as they arrive.


  3. digest('base64') - this is the final call after you digest you are essentially saying I am done adding data to this hash and compute the results in the base64 format, you could also pass in other formats to return the hash in. An important thing to remember is that once you call digest the hash is done no more data can be added and you would need to create a new hmac variable to create another hmac, even if using the same parameters.



ex.



const crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret);
hash.update('I ', 'utf8');
hash.update('love ', 'utf8');
hash.update('cupcakes', 'utf8');
hash.digest('base64');
console.log(hash);


This should return the same. If you are using the windows cmd.exe I believe windows uses a different encoding than utf8, you'll have to check on that, or create a node js file passing in arguments and hashing then logging to console instead of using the REPL






share|improve this answer


























  • I'm not clear about what is getting sha256 hashed and when it is applied, and what is getting base64 encoded, and when that is getting performed.

    – bhartsb
    Nov 14 '18 at 23:28











  • @bhartsb the text in the update method is being sha hashed with the secret, the first block of code you do it all in one go, the second block of code just shows that you can keep adding chunks of text together by calling the update method multiple times and passing it sections of the string you want to hash. Once you are done adding all your content to the hash update function you want to read that data, that's where the digest part comes in it takes the raw buffer created that holds the hashed data and converts it to a format of your choice in this case base64.

    – Harry Chilinguerian
    Nov 15 '18 at 1:37











  • Your explanation is good, unfortunately your code example is not equivalent to the original code which returns the result of the hmac operation in the variable hash.

    – James K Polk
    Nov 15 '18 at 3:20











  • Added the returned result that is stored in hash, thanks @James K Polk

    – Harry Chilinguerian
    Nov 19 '18 at 17:41
















0














var crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
.update('I love cupcakes')
.digest('base64');
console.log(hash);

// Prints:
// wPobwAUxvXjvOMYoRJxRAq6r1Jtdw6KlFupuqVnWZY4=



  1. crypto.createHmac('sha256', secret) - Self explanatory initializes a crypto hmac object.


  2. .update('I love cupcakes') - The second missing argument is the encoding of the data you are passing in to be hashed, since it is left out it is forced to utf-8 string, you could pass in a buffer and specify the encoding, or a typed array and specify the encoding, etc. The update method can be called multiple times to add additional strings, buffers, etc onto the end of the data being passed in that way if you are receiving chunks of data you can pass them in as they arrive.


  3. digest('base64') - this is the final call after you digest you are essentially saying I am done adding data to this hash and compute the results in the base64 format, you could also pass in other formats to return the hash in. An important thing to remember is that once you call digest the hash is done no more data can be added and you would need to create a new hmac variable to create another hmac, even if using the same parameters.



ex.



const crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret);
hash.update('I ', 'utf8');
hash.update('love ', 'utf8');
hash.update('cupcakes', 'utf8');
hash.digest('base64');
console.log(hash);


This should return the same. If you are using the windows cmd.exe I believe windows uses a different encoding than utf8, you'll have to check on that, or create a node js file passing in arguments and hashing then logging to console instead of using the REPL






share|improve this answer


























  • I'm not clear about what is getting sha256 hashed and when it is applied, and what is getting base64 encoded, and when that is getting performed.

    – bhartsb
    Nov 14 '18 at 23:28











  • @bhartsb the text in the update method is being sha hashed with the secret, the first block of code you do it all in one go, the second block of code just shows that you can keep adding chunks of text together by calling the update method multiple times and passing it sections of the string you want to hash. Once you are done adding all your content to the hash update function you want to read that data, that's where the digest part comes in it takes the raw buffer created that holds the hashed data and converts it to a format of your choice in this case base64.

    – Harry Chilinguerian
    Nov 15 '18 at 1:37











  • Your explanation is good, unfortunately your code example is not equivalent to the original code which returns the result of the hmac operation in the variable hash.

    – James K Polk
    Nov 15 '18 at 3:20











  • Added the returned result that is stored in hash, thanks @James K Polk

    – Harry Chilinguerian
    Nov 19 '18 at 17:41














0












0








0







var crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
.update('I love cupcakes')
.digest('base64');
console.log(hash);

// Prints:
// wPobwAUxvXjvOMYoRJxRAq6r1Jtdw6KlFupuqVnWZY4=



  1. crypto.createHmac('sha256', secret) - Self explanatory initializes a crypto hmac object.


  2. .update('I love cupcakes') - The second missing argument is the encoding of the data you are passing in to be hashed, since it is left out it is forced to utf-8 string, you could pass in a buffer and specify the encoding, or a typed array and specify the encoding, etc. The update method can be called multiple times to add additional strings, buffers, etc onto the end of the data being passed in that way if you are receiving chunks of data you can pass them in as they arrive.


  3. digest('base64') - this is the final call after you digest you are essentially saying I am done adding data to this hash and compute the results in the base64 format, you could also pass in other formats to return the hash in. An important thing to remember is that once you call digest the hash is done no more data can be added and you would need to create a new hmac variable to create another hmac, even if using the same parameters.



ex.



const crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret);
hash.update('I ', 'utf8');
hash.update('love ', 'utf8');
hash.update('cupcakes', 'utf8');
hash.digest('base64');
console.log(hash);


This should return the same. If you are using the windows cmd.exe I believe windows uses a different encoding than utf8, you'll have to check on that, or create a node js file passing in arguments and hashing then logging to console instead of using the REPL






share|improve this answer















var crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
.update('I love cupcakes')
.digest('base64');
console.log(hash);

// Prints:
// wPobwAUxvXjvOMYoRJxRAq6r1Jtdw6KlFupuqVnWZY4=



  1. crypto.createHmac('sha256', secret) - Self explanatory initializes a crypto hmac object.


  2. .update('I love cupcakes') - The second missing argument is the encoding of the data you are passing in to be hashed, since it is left out it is forced to utf-8 string, you could pass in a buffer and specify the encoding, or a typed array and specify the encoding, etc. The update method can be called multiple times to add additional strings, buffers, etc onto the end of the data being passed in that way if you are receiving chunks of data you can pass them in as they arrive.


  3. digest('base64') - this is the final call after you digest you are essentially saying I am done adding data to this hash and compute the results in the base64 format, you could also pass in other formats to return the hash in. An important thing to remember is that once you call digest the hash is done no more data can be added and you would need to create a new hmac variable to create another hmac, even if using the same parameters.



ex.



const crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret);
hash.update('I ', 'utf8');
hash.update('love ', 'utf8');
hash.update('cupcakes', 'utf8');
hash.digest('base64');
console.log(hash);


This should return the same. If you are using the windows cmd.exe I believe windows uses a different encoding than utf8, you'll have to check on that, or create a node js file passing in arguments and hashing then logging to console instead of using the REPL







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 17:40

























answered Nov 14 '18 at 22:40









Harry ChilinguerianHarry Chilinguerian

25318




25318













  • I'm not clear about what is getting sha256 hashed and when it is applied, and what is getting base64 encoded, and when that is getting performed.

    – bhartsb
    Nov 14 '18 at 23:28











  • @bhartsb the text in the update method is being sha hashed with the secret, the first block of code you do it all in one go, the second block of code just shows that you can keep adding chunks of text together by calling the update method multiple times and passing it sections of the string you want to hash. Once you are done adding all your content to the hash update function you want to read that data, that's where the digest part comes in it takes the raw buffer created that holds the hashed data and converts it to a format of your choice in this case base64.

    – Harry Chilinguerian
    Nov 15 '18 at 1:37











  • Your explanation is good, unfortunately your code example is not equivalent to the original code which returns the result of the hmac operation in the variable hash.

    – James K Polk
    Nov 15 '18 at 3:20











  • Added the returned result that is stored in hash, thanks @James K Polk

    – Harry Chilinguerian
    Nov 19 '18 at 17:41



















  • I'm not clear about what is getting sha256 hashed and when it is applied, and what is getting base64 encoded, and when that is getting performed.

    – bhartsb
    Nov 14 '18 at 23:28











  • @bhartsb the text in the update method is being sha hashed with the secret, the first block of code you do it all in one go, the second block of code just shows that you can keep adding chunks of text together by calling the update method multiple times and passing it sections of the string you want to hash. Once you are done adding all your content to the hash update function you want to read that data, that's where the digest part comes in it takes the raw buffer created that holds the hashed data and converts it to a format of your choice in this case base64.

    – Harry Chilinguerian
    Nov 15 '18 at 1:37











  • Your explanation is good, unfortunately your code example is not equivalent to the original code which returns the result of the hmac operation in the variable hash.

    – James K Polk
    Nov 15 '18 at 3:20











  • Added the returned result that is stored in hash, thanks @James K Polk

    – Harry Chilinguerian
    Nov 19 '18 at 17:41

















I'm not clear about what is getting sha256 hashed and when it is applied, and what is getting base64 encoded, and when that is getting performed.

– bhartsb
Nov 14 '18 at 23:28





I'm not clear about what is getting sha256 hashed and when it is applied, and what is getting base64 encoded, and when that is getting performed.

– bhartsb
Nov 14 '18 at 23:28













@bhartsb the text in the update method is being sha hashed with the secret, the first block of code you do it all in one go, the second block of code just shows that you can keep adding chunks of text together by calling the update method multiple times and passing it sections of the string you want to hash. Once you are done adding all your content to the hash update function you want to read that data, that's where the digest part comes in it takes the raw buffer created that holds the hashed data and converts it to a format of your choice in this case base64.

– Harry Chilinguerian
Nov 15 '18 at 1:37





@bhartsb the text in the update method is being sha hashed with the secret, the first block of code you do it all in one go, the second block of code just shows that you can keep adding chunks of text together by calling the update method multiple times and passing it sections of the string you want to hash. Once you are done adding all your content to the hash update function you want to read that data, that's where the digest part comes in it takes the raw buffer created that holds the hashed data and converts it to a format of your choice in this case base64.

– Harry Chilinguerian
Nov 15 '18 at 1:37













Your explanation is good, unfortunately your code example is not equivalent to the original code which returns the result of the hmac operation in the variable hash.

– James K Polk
Nov 15 '18 at 3:20





Your explanation is good, unfortunately your code example is not equivalent to the original code which returns the result of the hmac operation in the variable hash.

– James K Polk
Nov 15 '18 at 3:20













Added the returned result that is stored in hash, thanks @James K Polk

– Harry Chilinguerian
Nov 19 '18 at 17:41





Added the returned result that is stored in hash, thanks @James K Polk

– Harry Chilinguerian
Nov 19 '18 at 17:41




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309306%2fhow-does-nodejs-crypto-method-chaining-work%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly