<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Huabing Zhao</title>
    <description>The latest articles on DEV Community by Huabing Zhao (@zhaohuabing).</description>
    <link>https://dev.to/zhaohuabing</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F113508%2F01d4c230-e031-4b57-97a6-9b316993d67d.png</url>
      <title>DEV Community: Huabing Zhao</title>
      <link>https://dev.to/zhaohuabing</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zhaohuabing"/>
    <language>en</language>
    <item>
      <title>     "Cryptographic Hash Function"</title>
      <dc:creator>Huabing Zhao</dc:creator>
      <pubDate>Sun, 23 Dec 2018 12:56:19 +0000</pubDate>
      <link>https://dev.to/zhaohuabing/-----cryptographic-hash-function-3710</link>
      <guid>https://dev.to/zhaohuabing/-----cryptographic-hash-function-3710</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This series of articles are my notes of “Bitcoin and Cryptocurrency Technologies” online course originally published on my blog &lt;a href="https://zhaohuabing.com"&gt;zhaohuabing.com&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Hash Function
&lt;/h2&gt;

&lt;p&gt;Hash function is a mathematical function:&lt;em&gt;H(X)=Y&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;H:  A hash function which takes an input value and calculates an output value&lt;/li&gt;
&lt;li&gt;X: Input of the hash function, it could be any data of any length&lt;/li&gt;
&lt;li&gt;Y: Output of the hash function: a fix-size bit(, it can be 256, 384, 516 ..., Bitcoin uses 256)
&amp;lt;!--more--&amp;gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cryptographic Properties
&lt;/h2&gt;

&lt;p&gt;A hash function which is used for cryptographic purposes should have these properties:&lt;/p&gt;

&lt;h3&gt;
  
  
  Collision Free
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A hash function H is said to be collision free if:&lt;br&gt;&lt;br&gt;
It's infeasible to find two values X1 and X2, such that &lt;em&gt;X1!=X2&lt;/em&gt;, yet &lt;em&gt;H(X1)=H(X2)&lt;/em&gt;&lt;br&gt;&lt;br&gt;
Or in other words,&lt;br&gt;&lt;br&gt;
It's infeasible to find two inputs which can produce the same outputs. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explaination:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The collision does exist because the inputs can be any data and the outputs are only 2 to 256 possibilities. &lt;/p&gt;

&lt;p&gt;But for a good hash function, it's just impossible to find them in an acceptable time frame even use all the computers to solve this together on the earth.&lt;/p&gt;

&lt;p&gt;We can use this property of hash functions to create a digest for a given data.  By comparing the hash digests, we can tell if a big file is modified or corrupted during a transmission, which is often used in downloading a software.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hiding
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A hash function H is hiding if:&lt;br&gt;&lt;br&gt;
when a secret value R is chosen from a highly spread-out distribution that, then given the hash result of &lt;em&gt;H( R/|X)&lt;/em&gt;, it is infeasible to find X.  /| means concatenation of two strings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem We Want to Solve:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We want a hash function that it's infeasible to find out the input by the output of a hash function.&lt;/p&gt;

&lt;p&gt;The problem is that if there are only a few values of inputs, it will be very easy to figure out what the input is by the output by simply trying all the possible values of inputs and see if they match the output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Concatenating input with a random R which is randomly chosen from a highly spread-out distribution like this: &lt;em&gt;H( R/|X)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With R appended to the input, now it's infeasible to figure out what input is by just traversing all the values because there're too many possibilities.&lt;/p&gt;

&lt;p&gt;R is used to hide the input, by using R, the Hash function can hide the input while exposing the output.&lt;/p&gt;

&lt;h4&gt;
  
  
  Two Uses of Hiding Property
&lt;/h4&gt;

&lt;h5&gt;
  
  
  1. Commitment
&lt;/h5&gt;

&lt;p&gt;This use of hiding property is explained in the lecture.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We want to make a commitment, keep it as a secret, and reveal it later to others.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requirements:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The commitment can't be seen until it's revealed&lt;/li&gt;
&lt;li&gt;The commitment can't be changed.&lt;/li&gt;
&lt;li&gt;Other people can verify the commitment once it's revealed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;hash(message/|key)=commitment&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Message: the commitment we want to make, which may only have a few values.&lt;/li&gt;
&lt;li&gt;Key is a generated value from a spread-out distribution used to hide the message&lt;/li&gt;
&lt;li&gt;commitment: the hash of message concatenated with the key&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You want to make a commitment, the message, to others. It could be any message.&lt;/li&gt;
&lt;li&gt;You choose a generated key which is used to hide the message.&lt;/li&gt;
&lt;li&gt;You get the hash of the key message combination.&lt;/li&gt;
&lt;li&gt;You publish the hash result, which is the commitment, to others and keep the key and message only to yourself. So other people know you have made a commitment, but they don't know what exactly it is.&lt;/li&gt;
&lt;li&gt;After a while, you decide to reveal the commitment, so you publish the key and message.&lt;/li&gt;
&lt;li&gt;Other people can use the hash function &lt;em&gt;hash(message/|key)&lt;/em&gt; to calculate the hash result, compare it with the hash(commitment) you previously published. If it's the same, they can verify that you didn't change the commitment you have made.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt; Because a key is used to hide the message, other people can't figure out what's the message before you reveal it.&lt;/li&gt;
&lt;li&gt;Because of collision-free property, you can't find a message' such that &lt;em&gt;hash(message'/|key)=hash(message/|key)&lt;/em&gt;, so it's impossible to change the committed message after publishing it.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  2. Secure Password
&lt;/h5&gt;

&lt;p&gt;Another common use of hiding property of hash is to secure passwords.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A website needs to verify the user password when user login. Instead of storing the password in the system, a more secure approach is just storing the hash of the password and compare the hash to verify the user.  By this way, the user password won't be at risk even the system is broken by attackers because the attackers can't get the password by the hash.&lt;/p&gt;

&lt;p&gt;But there's still a problem, many people tend to use simple words as their passwords. Attackers can make a long list of common passwords used by people, calculate the hash of these passwords in advance, and use these hashes to attack the system to figure out what's the password. It's called a rainbow attack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use a randomly generated 'salt' to safeguard the password.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;hash(password/|salt)=output&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To solve this problem, we can append a generated random value to the password, this value is often called 'salt'. Salt is saved along with the hashed password in the system. So the system can get the hash out of the combination of user password and salt, compare it with the stored hash to verify user identity.&lt;/p&gt;

&lt;p&gt;By appending a salt to the password, attackers can no longer use a pre-calculated password-hash map to attack the system. Even two users happened to choose the same string as their passwords, the hashes stored in the system are different because their salts are different, which is randomly generated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This example is excerpted from &lt;a href="https://en.wikipedia.org/wiki/Salt_(cryptography)"&gt;wikipedia&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Username&lt;/th&gt;
&lt;th&gt;Password&lt;/th&gt;
&lt;th&gt;Salt value&lt;/th&gt;
&lt;th&gt;String to be hashed&lt;/th&gt;
&lt;th&gt;Hashed value = SHA256 (Password + Salt value)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;user1&lt;/td&gt;
&lt;td&gt;password123&lt;/td&gt;
&lt;td&gt;E1F53135E559C253&lt;/td&gt;
&lt;td&gt;password123+E1F53135E559C253&lt;/td&gt;
&lt;td&gt;72AE25495A7981C40622D49F9A52E4F1565C90F048F59027BD9C8C8900D5C3D8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;user2&lt;/td&gt;
&lt;td&gt;password123&lt;/td&gt;
&lt;td&gt;84B03D034B409D4E&lt;/td&gt;
&lt;td&gt;password123+84B03D034B409D4E&lt;/td&gt;
&lt;td&gt;B4B6603ABC670967E99C7E7F1389E40CD16E78AD38EB1468EC2AA1E62B8BED3A&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As the table above illustrates, different salt values will create completely different hashed values, even when the plaintext passwords are exactly the same. Additionally, dictionary attacks are mitigated to a degree as an attacker cannot practically precompute the hashes. However, a salt cannot protect against common or easily guessed passwords because the attacker can still combine the salt with all the possible password in the dictionary and try to match the hash of the combinations with the hashed value stored in the attached target. The salt just makes the attack more difficult because attackers need two additional steps: 1. find out the salt of the attacked target 2. Calculate the hash every time&lt;/p&gt;

&lt;h3&gt;
  
  
  Puzzle-Friendly
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A hash function H is said to be puzzle-friendly if:&lt;br&gt;&lt;br&gt;
Given an R which is chosen from a highly spread-out distribution and a target set Y.&lt;br&gt;&lt;br&gt;
Try to find a solution X such that &lt;em&gt;H(R/|X) $$/in$$ Y&lt;/em&gt;.&lt;br&gt;&lt;br&gt;
There is no solving strategy to find X much better than just trying every possible value of X.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Puzzle-friendly property is used for Bitcoin mining. The miner needs to find out a specific number R, which is concatenated with the data of the block, and the hash of the combination should fall into a certain range. The first one who solves this puzzle can add the outstanding transaction into the blockchain and get Bitcoin as the reward.&lt;/p&gt;

&lt;p&gt;Bitcoin Minding Puzzle: find R such that &lt;em&gt;H(R/|BlockData) $$/in$$ ValidRange&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  SHA-256
&lt;/h2&gt;

&lt;p&gt;SHA-256 is the hash function used in Bitcoin which has all the three needed properties.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6Jywk8zr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://img.zhaohuabing.com/in-post/2018-05-09-cryptocurrency-week1-cryptographic-hash-function/sha-256.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6Jywk8zr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://img.zhaohuabing.com/in-post/2018-05-09-cryptocurrency-week1-cryptographic-hash-function/sha-256.PNG" alt="SHA-256"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cryptocurrency</category>
      <category>blockchain</category>
      <category>bitcoin</category>
    </item>
    <item>
      <title>     "My Journey of Bitcoin and Cryptocurrency Technologies Online Course Studying"</title>
      <dc:creator>Huabing Zhao</dc:creator>
      <pubDate>Sun, 23 Dec 2018 12:50:25 +0000</pubDate>
      <link>https://dev.to/zhaohuabing/-----my-journey-of-bitcoin-and-cryptocurrency-technologies-online-course-studying-59dm</link>
      <guid>https://dev.to/zhaohuabing/-----my-journey-of-bitcoin-and-cryptocurrency-technologies-online-course-studying-59dm</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This series of articles are my notes of “Bitcoin and Cryptocurrency Technologies” online course originally published on my blog &lt;a href="https://zhaohuabing.com"&gt;zhaohuabing.com&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How did I get into this?
&lt;/h2&gt;

&lt;p&gt;I have noticed the buzzwords “Bitcoin” and “Blockchain” for a while. There are lots of articles, news, and talks around them. It seems that many people believe that cryptocurrency is the future of online payment, some even claim that blockchain would become the fundamental technology of the next generation of the Internet.&lt;/p&gt;

&lt;p&gt;Given its popularity, I can’t help thinking that should I also invest in it? Maybe investing money in Bitcoin or other cryptocurrencies is too risky now, but at least I could try to learn the technologies behind these. So one day I could use this knowledge to help me estimate a potential cryptocurrency investment opportunity, or maybe find a job :-)&lt;/p&gt;

&lt;h2&gt;
  
  
  What's my finding of blockchain and cryptocurrency learning materials?
&lt;/h2&gt;

&lt;p&gt;I did some searches and found so many materials about Bitcoin and Blockchain. Some of them did good jobs at explaining parts of the whole picture, But it’s a big topic so I think a systematic learning path makes more sense. &lt;/p&gt;

&lt;p&gt;Finally, I found this amazing “Bitcoin and Cryptocurrency Technologies” online course. It's created by the professors of Princeton University. The course has a series of well-organized lecture videos explaining the technologies behind Bitcoin from the very beginning to more advanced topics. It also has programming practices after each lecture so you can get hands in the codes and get a better understanding of the theories you got from the videos.&lt;/p&gt;

&lt;p&gt;I encourage anyone who is interested in cryptocurrency to attend this wonderful online course. You will not only get a chance to learn the theories and technical details behind the popular Bitcoin but also even be able to create your own version of cryptocurrency after finishing this course!  The last important thing is that it's totally free!  What you need to do to gain all of these is just watching the course videos and try to practice and finish your programming assignments. The course is mobile friendly, so you can even watch the course on your mobile phone when commuting on the subway, that's exactly what I'm doing, a great way to make use of the fragmented time.&lt;/p&gt;

&lt;p&gt;You can find the online course here: &lt;a href="https://www.coursera.org/learn/cryptocurrency"&gt;Bitcoin and Cryptocurrency Technologies&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/zhaohuabing"&gt;Example codes on GitHub&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cryptocurrency</category>
      <category>blockchain</category>
      <category>bitcoin</category>
    </item>
  </channel>
</rss>
