We studied Hashes in ruby, where each symbol has a value
_hash = {name: "Bilal Haider"}
where :name is key, that has a value "Bilal Haider"
Similarly cryptographic Hash is a key-value pair but is different from regular ruby Hashes.
cryptoMD5Hash = {:4443eb2b819498eb5f30980424591c43 => "bilal is awesome coder :)"}
Cryptography in Ruby
In this article, we are going to learn a little bit about cryptography in general, and how ruby helps us to utilize cryptography in our progams.
What is cryptography ?
Cryptography is a technique of hiding information from the world, so that only the person can read or process it, who is intended to do so.
Unlike encryption where input text is encrypted and can also be decrypted back to original text, via a decrypt method or function, a cryptographic hash is one way process, there exists no method which can convert a cryptographic hash back to original text.
This property of Cryptographic Hash makes it near impossible to crack original text from Hashed version of it.
Cryptography is done via a cryptographic hash method or function, which takes input information, and converts the information into a fixed size string of text.
A cryptographic hash (sometimes called ‘digest’) is a kind of ‘signature’ for a text or a data file. SHA-256 generates an almost-unique 256-bit (32-byte) signature for a text
A hash is not ‘encryption’ – it cannot be decrypted back to the original text (it is a ‘one-way’ cryptographic function, and is a fixed size for any size of source text). This makes it suitable when it is appropriate to compare ‘hashed’ versions of texts, as opposed to decrypting the text to obtain the original version.
Lets see how we can create a cryptographic hash from an input text in ruby, then we will talk more about it, and we will think of many different scenarios where we can use such a method
Ruby provides us several Classes to use different Hashing algorithms for to create cryptographic hashes, that include Md5, SHA1, SHA256 and more ..
Ruby has created a module called "Digest" which contains necessary Classes, which we can use to create cryptographic hashes..
require 'digest'
## input string to be hashed
_inputString = "bilal is awesome coder :)"
## Using MD5 Class of Digest module, which ruby provides us
_cryptoHash = Digest::MD5.new()
## Pushing input string which we want to be hashed
_cryptoHash << _inputString
## Displaying the hashed result, which hexdigest method of MD5 class generates for us
puts _cryptoHash.hexdigest
Before we talk further keep this note
MD5 Hash of "bilal is awesome coder :)" will always be "4443eb2b819498eb5f30980424591c43". This is very important quality of hash methods.
Here is a list of hash Classes which ruby's Digest module provides us, note that Bitcoin uses SHA-256 for generating Hashes.
- MD5
- A class for calculating message digests using the MD5 Message-Digest Algorithm by RSA Data Security, Inc., described in RFC1321.
- RMD160
- A class for calculating message digests using RIPEMD-160 cryptographic hash function, designed by Hans Dobbertin, Antoon Bosselaers, and Bart Preneel.
require 'digest'
_inputString = "bilal is awesome coder :)"
_cryptoHash = Digest::RMD160.new()
_cryptoHash << _inputString
puts _cryptoHash.hexdigest
- SHA-1
- A class for calculating message digests using the SHA-1 Secure Hash Algorithm by NIST (the US' National Institute of Standards and Technology), described in FIPS PUB 180-1.
require 'digest'
_inputString = "bilal is awesome coder :)"
_cryptoHash = Digest::SHA1.new()
_cryptoHash << _inputString
puts _cryptoHash.hexdigest
- SHA-2
- A meta digest provider class for SHA256, SHA384 and SHA512. FIPS 180-2 describes SHA2 family of digest algorithms. It defines three algorithms:
- one which works on chunks of 512 bits and returns a 256-bit digest (SHA256),
- one which works on chunks of 1024 bits and returns a 384-bit digest (SHA384),
- one which works on chunks of 1024 bits and returns a 512-bit digest (SHA512).
- A meta digest provider class for SHA256, SHA384 and SHA512. FIPS 180-2 describes SHA2 family of digest algorithms. It defines three algorithms:
require 'digest'
_inputString = "bilal is awesome coder :)"
_cryptoHash = Digest::SHA256.new()
_cryptoHash << _inputString
puts _cryptoHash.hexdigest
_inputString = "bilal is awesome coder :)"
_cryptoHash = Digest::SHA384.new()
_cryptoHash << _inputString
puts _cryptoHash.hexdigest
_inputString = "bilal is awesome coder :)"
_cryptoHash = Digest::SHA512.new()
_cryptoHash << _inputString
puts _cryptoHash.hexdigest
Few final notes, before we close this discussion,
Each input data of variable length will produce 512 bits long Hash, in case if you are using SHA-512 for hashing purposes..
No two different input texts, should produce same Cryptographic Hash.. which makes the hash method not usable.
at this point of time, SHA-2 is considered most secure, hashing algorithm, which can be used in your ruby programs, if you are into that kind of things :)
In future, we will make applications, which use cryptography
If you guys want to learn more about cryptography and want to explore more algorithms of cryptographic hashing,
visit this reference link :)
https://ruby-doc.org/stdlib-2.5.0/libdoc/digest/rdoc/Digest.html
me parece maravilloso
gracias
Very nice content thank you!!!!
you too into cryptography ?
Looks very interesting i will read your blog from the lesson 1, thanks for share, i will resteem this post
awesome, all the best for that :) I am sure you will learn a lot
Very nice post .....i would like to read it from the beginning
Thanks :) and all the best, you are going to learn a lot of things :)
Yea sure ......i appreciate oo