Is there a loss of entropy by hashing an N-bit random key to produce an N-bit hash?












4















I read the following piece of code:



dd if=/dev/urandom bs=16 count=1 2>/dev/null | md5sum


Apparently, this code was used as a trick to produce a hex string key from a 128-bit binary pseudo-random value.



Someone here claimed this is inherently insecure since it passes a cryptographically secure random value through an "insecure hash function".



On my side, I would say the collision flaws in md5 are irrelevant here since the hash function input and output have the same size. And so, the hash function output is as random as its input.



What's your opinion about that? Does hashing an N-bit random key to produce an N-bits hash change the randomness of the key?










share|improve this question





























    4















    I read the following piece of code:



    dd if=/dev/urandom bs=16 count=1 2>/dev/null | md5sum


    Apparently, this code was used as a trick to produce a hex string key from a 128-bit binary pseudo-random value.



    Someone here claimed this is inherently insecure since it passes a cryptographically secure random value through an "insecure hash function".



    On my side, I would say the collision flaws in md5 are irrelevant here since the hash function input and output have the same size. And so, the hash function output is as random as its input.



    What's your opinion about that? Does hashing an N-bit random key to produce an N-bits hash change the randomness of the key?










    share|improve this question



























      4












      4








      4








      I read the following piece of code:



      dd if=/dev/urandom bs=16 count=1 2>/dev/null | md5sum


      Apparently, this code was used as a trick to produce a hex string key from a 128-bit binary pseudo-random value.



      Someone here claimed this is inherently insecure since it passes a cryptographically secure random value through an "insecure hash function".



      On my side, I would say the collision flaws in md5 are irrelevant here since the hash function input and output have the same size. And so, the hash function output is as random as its input.



      What's your opinion about that? Does hashing an N-bit random key to produce an N-bits hash change the randomness of the key?










      share|improve this question
















      I read the following piece of code:



      dd if=/dev/urandom bs=16 count=1 2>/dev/null | md5sum


      Apparently, this code was used as a trick to produce a hex string key from a 128-bit binary pseudo-random value.



      Someone here claimed this is inherently insecure since it passes a cryptographically secure random value through an "insecure hash function".



      On my side, I would say the collision flaws in md5 are irrelevant here since the hash function input and output have the same size. And so, the hash function output is as random as its input.



      What's your opinion about that? Does hashing an N-bit random key to produce an N-bits hash change the randomness of the key?







      hash random entropy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 21 mins ago







      Sylvain Leroux

















      asked 8 hours ago









      Sylvain LerouxSylvain Leroux

      1387




      1387






















          1 Answer
          1






          active

          oldest

          votes


















          7














          Hashing is a deterministic process which means that it can never increase the randomness. But of course it can decrease the randomness: if you hash a 200 bit random value with some hash algorithms which only outputs 160 bits (like SHA-1) then of course the resulting value can never have 200 bits randomness.



          But as long as the number of input bits is significantly lower than the output size of the hash it will not reduce the randomness, providing a cryptographic hash is used. If the input size is exactly the same as the input size as in your example the resulting randomness is likely not significantly decreased when using a cryptographic hash. And you are right that collision resistance does not matter for this.






          share|improve this answer
























          • Thanks for such a rapid answer, Steffen. Could you elaborate on this though: "If the input size is exactly the same as the input size as in your example the resulting randomness is likely not significantly decreased when using a cryptographic hash." Why a cryptographic hash still decreases, even slightly, the entropy if the hash size in the same as the input size?

            – Sylvain Leroux
            8 hours ago








          • 2





            @SylvainLeroux: A cryptographic hash is not designed as a bijective mapping from N bit input to N bit output. This means that there will be some collision and thus the randomness will be slightly reduced.

            – Steffen Ullrich
            7 hours ago






          • 4





            @SylvainLeroux You cannot be sure that all possible 128-bit inputs have different hashes, and they probably don’t. If it is so, then, after the hash, some 128-bit outputs are impossible and others have a higher occurrence than $1 over 2^{128}$.

            – user2233709
            7 hours ago











          • Thanks for the clarification Steffen & @user2233709

            – Sylvain Leroux
            7 hours ago













          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "162"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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
          },
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsecurity.stackexchange.com%2fquestions%2f203318%2fis-there-a-loss-of-entropy-by-hashing-an-n-bit-random-key-to-produce-an-n-bit-ha%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









          7














          Hashing is a deterministic process which means that it can never increase the randomness. But of course it can decrease the randomness: if you hash a 200 bit random value with some hash algorithms which only outputs 160 bits (like SHA-1) then of course the resulting value can never have 200 bits randomness.



          But as long as the number of input bits is significantly lower than the output size of the hash it will not reduce the randomness, providing a cryptographic hash is used. If the input size is exactly the same as the input size as in your example the resulting randomness is likely not significantly decreased when using a cryptographic hash. And you are right that collision resistance does not matter for this.






          share|improve this answer
























          • Thanks for such a rapid answer, Steffen. Could you elaborate on this though: "If the input size is exactly the same as the input size as in your example the resulting randomness is likely not significantly decreased when using a cryptographic hash." Why a cryptographic hash still decreases, even slightly, the entropy if the hash size in the same as the input size?

            – Sylvain Leroux
            8 hours ago








          • 2





            @SylvainLeroux: A cryptographic hash is not designed as a bijective mapping from N bit input to N bit output. This means that there will be some collision and thus the randomness will be slightly reduced.

            – Steffen Ullrich
            7 hours ago






          • 4





            @SylvainLeroux You cannot be sure that all possible 128-bit inputs have different hashes, and they probably don’t. If it is so, then, after the hash, some 128-bit outputs are impossible and others have a higher occurrence than $1 over 2^{128}$.

            – user2233709
            7 hours ago











          • Thanks for the clarification Steffen & @user2233709

            – Sylvain Leroux
            7 hours ago


















          7














          Hashing is a deterministic process which means that it can never increase the randomness. But of course it can decrease the randomness: if you hash a 200 bit random value with some hash algorithms which only outputs 160 bits (like SHA-1) then of course the resulting value can never have 200 bits randomness.



          But as long as the number of input bits is significantly lower than the output size of the hash it will not reduce the randomness, providing a cryptographic hash is used. If the input size is exactly the same as the input size as in your example the resulting randomness is likely not significantly decreased when using a cryptographic hash. And you are right that collision resistance does not matter for this.






          share|improve this answer
























          • Thanks for such a rapid answer, Steffen. Could you elaborate on this though: "If the input size is exactly the same as the input size as in your example the resulting randomness is likely not significantly decreased when using a cryptographic hash." Why a cryptographic hash still decreases, even slightly, the entropy if the hash size in the same as the input size?

            – Sylvain Leroux
            8 hours ago








          • 2





            @SylvainLeroux: A cryptographic hash is not designed as a bijective mapping from N bit input to N bit output. This means that there will be some collision and thus the randomness will be slightly reduced.

            – Steffen Ullrich
            7 hours ago






          • 4





            @SylvainLeroux You cannot be sure that all possible 128-bit inputs have different hashes, and they probably don’t. If it is so, then, after the hash, some 128-bit outputs are impossible and others have a higher occurrence than $1 over 2^{128}$.

            – user2233709
            7 hours ago











          • Thanks for the clarification Steffen & @user2233709

            – Sylvain Leroux
            7 hours ago
















          7












          7








          7







          Hashing is a deterministic process which means that it can never increase the randomness. But of course it can decrease the randomness: if you hash a 200 bit random value with some hash algorithms which only outputs 160 bits (like SHA-1) then of course the resulting value can never have 200 bits randomness.



          But as long as the number of input bits is significantly lower than the output size of the hash it will not reduce the randomness, providing a cryptographic hash is used. If the input size is exactly the same as the input size as in your example the resulting randomness is likely not significantly decreased when using a cryptographic hash. And you are right that collision resistance does not matter for this.






          share|improve this answer













          Hashing is a deterministic process which means that it can never increase the randomness. But of course it can decrease the randomness: if you hash a 200 bit random value with some hash algorithms which only outputs 160 bits (like SHA-1) then of course the resulting value can never have 200 bits randomness.



          But as long as the number of input bits is significantly lower than the output size of the hash it will not reduce the randomness, providing a cryptographic hash is used. If the input size is exactly the same as the input size as in your example the resulting randomness is likely not significantly decreased when using a cryptographic hash. And you are right that collision resistance does not matter for this.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 8 hours ago









          Steffen UllrichSteffen Ullrich

          116k13201266




          116k13201266













          • Thanks for such a rapid answer, Steffen. Could you elaborate on this though: "If the input size is exactly the same as the input size as in your example the resulting randomness is likely not significantly decreased when using a cryptographic hash." Why a cryptographic hash still decreases, even slightly, the entropy if the hash size in the same as the input size?

            – Sylvain Leroux
            8 hours ago








          • 2





            @SylvainLeroux: A cryptographic hash is not designed as a bijective mapping from N bit input to N bit output. This means that there will be some collision and thus the randomness will be slightly reduced.

            – Steffen Ullrich
            7 hours ago






          • 4





            @SylvainLeroux You cannot be sure that all possible 128-bit inputs have different hashes, and they probably don’t. If it is so, then, after the hash, some 128-bit outputs are impossible and others have a higher occurrence than $1 over 2^{128}$.

            – user2233709
            7 hours ago











          • Thanks for the clarification Steffen & @user2233709

            – Sylvain Leroux
            7 hours ago





















          • Thanks for such a rapid answer, Steffen. Could you elaborate on this though: "If the input size is exactly the same as the input size as in your example the resulting randomness is likely not significantly decreased when using a cryptographic hash." Why a cryptographic hash still decreases, even slightly, the entropy if the hash size in the same as the input size?

            – Sylvain Leroux
            8 hours ago








          • 2





            @SylvainLeroux: A cryptographic hash is not designed as a bijective mapping from N bit input to N bit output. This means that there will be some collision and thus the randomness will be slightly reduced.

            – Steffen Ullrich
            7 hours ago






          • 4





            @SylvainLeroux You cannot be sure that all possible 128-bit inputs have different hashes, and they probably don’t. If it is so, then, after the hash, some 128-bit outputs are impossible and others have a higher occurrence than $1 over 2^{128}$.

            – user2233709
            7 hours ago











          • Thanks for the clarification Steffen & @user2233709

            – Sylvain Leroux
            7 hours ago



















          Thanks for such a rapid answer, Steffen. Could you elaborate on this though: "If the input size is exactly the same as the input size as in your example the resulting randomness is likely not significantly decreased when using a cryptographic hash." Why a cryptographic hash still decreases, even slightly, the entropy if the hash size in the same as the input size?

          – Sylvain Leroux
          8 hours ago







          Thanks for such a rapid answer, Steffen. Could you elaborate on this though: "If the input size is exactly the same as the input size as in your example the resulting randomness is likely not significantly decreased when using a cryptographic hash." Why a cryptographic hash still decreases, even slightly, the entropy if the hash size in the same as the input size?

          – Sylvain Leroux
          8 hours ago






          2




          2





          @SylvainLeroux: A cryptographic hash is not designed as a bijective mapping from N bit input to N bit output. This means that there will be some collision and thus the randomness will be slightly reduced.

          – Steffen Ullrich
          7 hours ago





          @SylvainLeroux: A cryptographic hash is not designed as a bijective mapping from N bit input to N bit output. This means that there will be some collision and thus the randomness will be slightly reduced.

          – Steffen Ullrich
          7 hours ago




          4




          4





          @SylvainLeroux You cannot be sure that all possible 128-bit inputs have different hashes, and they probably don’t. If it is so, then, after the hash, some 128-bit outputs are impossible and others have a higher occurrence than $1 over 2^{128}$.

          – user2233709
          7 hours ago





          @SylvainLeroux You cannot be sure that all possible 128-bit inputs have different hashes, and they probably don’t. If it is so, then, after the hash, some 128-bit outputs are impossible and others have a higher occurrence than $1 over 2^{128}$.

          – user2233709
          7 hours ago













          Thanks for the clarification Steffen & @user2233709

          – Sylvain Leroux
          7 hours ago







          Thanks for the clarification Steffen & @user2233709

          – Sylvain Leroux
          7 hours ago




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Information Security Stack Exchange!


          • 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%2fsecurity.stackexchange.com%2fquestions%2f203318%2fis-there-a-loss-of-entropy-by-hashing-an-n-bit-random-key-to-produce-an-n-bit-ha%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

          Why is a white electrical wire connected to 2 black wires?

          Waikiki

          What are all the squawk codes?