# What these lines of code do?

**URL:** <https://www.ruby-forum.com/t/what-these-lines-of-code-do/126928>\
**Category:** Ruby\
**Created:** [January 23, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/what-these-lines-of-code-do/126928 "2008-01-23T15:06:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [January 23, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/what-these-lines-of-code-do/126928/1 "2008-01-23T15:06:38Z")

</div>

# What this is doing? I mean Digest.new(‘sha1’) returns what?

digest = OpenSSL::Digest::Digest.new(‘sha1’)

# Is it so that we say HMAC.digest to use the SHA1 algorithm?

hmac = OpenSSL::HMAC.digest(digest, SECRET\_KEY, uri)

# What the chomp method does?

signature = Base64.encode64(hmac).chomp

Asking these questions as they are not documented in the Ruby  
documentation.

Regards,  
Mohsin

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [January 23, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/what-these-lines-of-code-do/126928/2 "2008-01-23T15:41:23Z")

</div>

On Jan 23, 2008 3:05 PM, MohsinHijazee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> documentation.
> 
> Regards,  
> Mohsin

[http://ruby-doc.org/stdlib/libdoc/openssl/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/openssl/rdoc/index.html)  
[http://ruby-doc.org/core/classes/String.html](http://ruby-doc.org/core/classes/String.html)

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [January 23, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/what-these-lines-of-code-do/126928/3 "2008-01-23T16:05:35Z")

</div>

On Jan 23, 8:40 am, Jano S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Asking these questions as they are not documented in the Ruby  
> > documentation.
> 
> > Regards,  
> > Mohsin
> 
> [http://ruby-doc.org/stdlib/libdoc/openssl/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/openssl/rdoc/index.html)

There really isn’t any useful documentation at that URL. You can see  
the source of some methods, but there’s still no explanation of terms  
or how everything fits together. There are also some important classes  
missing (SSLContext, Certificate).

[Cryptographic hash function - Wikipedia](http://en.wikipedia.org/wiki/Cryptographic_hash_function) might be your  
best bet for the two OpenSSL questions. Once you know what the various  
terms mean, you can apply that knowledge to any crypto library.

You can also find some info in the docs at [openssl.org](http://openssl.org), but they  
aren’t Ruby specific and won’t help much if you don’t have at least  
some knowledge of the terms involved. I’ve found the O’Reilly book  
‘Network Security with OpenSSL’ to be pretty helpful, but it isn’t  
Ruby specific either. There are also some simple examples packaged  
with the Ruby source in ruby-1.8.6/sample/openssl that are helpful in  
fitting the pieces together.

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [January 24, 2008, 7:15am UTC](https://www.ruby-forum.com/t/what-these-lines-of-code-do/126928/4 "2008-01-24T07:15:29Z")

</div>

On Jan 23, 8:02 pm, yermej [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > hmac = OpenSSL::HMAC.digest(digest, SECRET\_KEY, uri)  
> > > [http://ruby-doc.org/stdlib/libdoc/openssl/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/openssl/rdoc/index.html)  
> > > You can also find some info in the docs at [openssl.org](http://openssl.org), but they  
> > > aren’t Ruby specific and won’t help much if you don’t have at least  
> > > some knowledge of the terms involved. I’ve found the O’Reilly book  
> > > ‘Network Security with OpenSSL’ to be pretty helpful, but it isn’t  
> > > Ruby specific either. There are also some simple examples packaged  
> > > with the Ruby source in ruby-1.8.6/sample/openssl that are helpful in  
> > > fitting the pieces together.  
> > > Actually, I understand the terms involved. But the problem is that  
> > > functions are not documented properly. The HMAC.digest should take two  
> > > arguments, one should be the key and the second should be the message  
> > > to be encrypted whereas its taking three arguments. In an IRB session,  
> > > even the version with two arguments complains that its being provided  
> > > with too many arguments. Anyone to clarify on this?

Regards,  
Mohsin

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [January 24, 2008, 7:16am UTC](https://www.ruby-forum.com/t/what-these-lines-of-code-do/126928/5 "2008-01-24T07:16:39Z")

</div>

On Jan 23, 8:02 pm, yermej [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > hmac = OpenSSL::HMAC.digest(digest, SECRET\_KEY, uri)  
> > > [http://ruby-doc.org/stdlib/libdoc/openssl/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/openssl/rdoc/index.html)  
> > > You can also find some info in the docs at [openssl.org](http://openssl.org), but they  
> > > aren’t Ruby specific and won’t help much if you don’t have at least  
> > > some knowledge of the terms involved. I’ve found the O’Reilly book  
> > > ‘Network Security with OpenSSL’ to be pretty helpful, but it isn’t  
> > > Ruby specific either. There are also some simple examples packaged  
> > > with the Ruby source in ruby-1.8.6/sample/openssl that are helpful in  
> > > fitting the pieces together.  
> > > Actually, I understand the terms involved. But the problem is that  
> > > functions are not documented properly. The HMAC.digest should take two  
> > > arguments, one should be the key and the second should be the message  
> > > to be encrypted whereas its taking three arguments. In an IRB session,  
> > > even the version with two arguments complains that its being provided  
> > > with too many arguments. Anyone to clarify on this?

Regards,  
Mohsin

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [January 24, 2008, 7:17am UTC](https://www.ruby-forum.com/t/what-these-lines-of-code-do/126928/6 "2008-01-24T07:17:49Z")

</div>

On Jan 23, 8:02 pm, yermej [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > hmac = OpenSSL::HMAC.digest(digest, SECRET\_KEY, uri)  
> > > [http://ruby-doc.org/stdlib/libdoc/openssl/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/openssl/rdoc/index.html)  
> > > You can also find some info in the docs at [openssl.org](http://openssl.org), but they  
> > > aren’t Ruby specific and won’t help much if you don’t have at least  
> > > some knowledge of the terms involved. I’ve found the O’Reilly book  
> > > ‘Network Security with OpenSSL’ to be pretty helpful, but it isn’t  
> > > Ruby specific either. There are also some simple examples packaged  
> > > with the Ruby source in ruby-1.8.6/sample/openssl that are helpful in  
> > > fitting the pieces together.  
> > > Actually, I understand the terms involved. But the problem is that  
> > > functions are not documented properly. The HMAC.digest should take two  
> > > arguments, one should be the key and the second should be the message  
> > > to be encrypted whereas its taking three arguments. In an IRB session,  
> > > even the version with two arguments complains that its being provided  
> > > with too many arguments. Anyone to clarify on this?

Regards,  
Mohsin

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [January 24, 2008, 7:40am UTC](https://www.ruby-forum.com/t/what-these-lines-of-code-do/126928/7 "2008-01-24T07:40:36Z")

</div>

MohsinHijazee wrote:

> documentation.  
> You should probably refer to the OpenSSL documentation, see  
> [http://www.openssl.org](http://www.openssl.org)

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [January 24, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/what-these-lines-of-code-do/126928/8 "2008-01-24T15:41:00Z")

</div>

On Jan 24, 12:13 am, MohsinHijazee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Actually, I understand the terms involved. But the problem is that  
> functions are not documented properly. The HMAC.digest should take two  
> arguments, one should be the key and the second should be the message  
> to be encrypted whereas its taking three arguments. In an IRB session,  
> even the version with two arguments complains that its being provided  
> with too many arguments. Anyone to clarify on this?

Yes, it’s true that the documentation is lacking. Can you post the  
code and error message you get when trying to use HMAC.digest in irb?  
On two of my systems (one Linux, on WinXP), the arity of HMAC.digest  
is three:

> OpenSSL::HMAC.method(:digest).arity  
> =\> 3

and it will take three arguments, but I don’t know how to use the  
method so I can’t be of much help. In the Ruby source, the three  
arguments are named digest, key, and data.
