Reading Books To Learn Tech

Reading Tech Books In Public

For my first entry starting out this blog & my entire idea, I’d like to talk about making everything public.   I want to show you that even Software Developers (like me) who’ve been professionals for 30 years still stumble.

I have 30 Years of IT (Information Technology) experience which includes more than 20 years of Software Development.

Learning To Read Tech Documentation

I remember learning to read the cryptic Windows API documentation to learn how to create a new window.

void CreateWindowA(
  [in, optional]  lpClassName,
  [in, optional]  lpWindowName,
  [in]            dwStyle,
  [in]            x,
  [in]            y,
  [in]            nWidth,
  [in]            nHeight,
  [in, optional]  hWndParent,
  [in, optional]  hMenu,
  [in, optional]  hInstance,
  [in, optional]  lpParam
);

So I’ve been around the block and seen a few things, but I still stumble a lot when reading and learning new concepts.

I’m reading the book, Real-World Cryptography (Manning Publishers), by David Wong.  It is an interesting book but it misses quite a bit.

Author Leaves Me Behind A Lot

This book is really interesting & I’m going to keep going but I keep stumbling as the author explains a concept so quickly that I have no idea what he is talking about.  I’m still thinking about his last sentence and he is moving on to the next concept.

Sample Code

At one point in the third chapter the author provides sample code which consist of two functions written in Rust. The first function is supposed to generate a MAC (Message Authentication Code) and I decide to try the code out.

I open up my trusty code editor (Visual Studio Code, of course) and type the code in.

Yes, I decide to actually type the code because you learn more by doing the work.

Here’s what the author provides :

use sha2::Sha256;
use hmac::{Hmac, Mac};

fn send_message(key: &[u8], message: &[u8]) -> Vec{
    let mut mac = Hmac::::new(key.into());
    mac.update(message);
    mac.finalize().into_bytes().to_vec()
}

Missing Parts

However, there is a lot of missing parts to this.

Main Function

Of course if you want to try this out you need a main function where you’ll call the send_message() function.

Missing Crates (References to Libraries)

You are also missing the two required crates (which you’ll have to add to your project’s cargo.toml.

It takes me a bit (I’m new to Rust) but I finally find the crates & remember the format required to add them to the toml file.

[dependencies]
sha2 = "0.10.2"
hmac = "0.12.1"

It builds at this point, but now the problem is that I don’t know how to actually call the function.  I’m not sure what the input is supposed to be formatted.

Trying It

So I make some guesses to get it working.

I call it like the following:

send_message(&[59,64,38,99], &[33,55,65,68]);

I have to really stare at the error message I get from the Rust compiler:

thread ‘main’ panicked at ‘assertion failed: `(left == right)`
left: `4`,
right: `64`’, C:\Users\1rade\.cargo\registry\src\github.com-1ecc6299db9ec823\generic-array-0.14.5\src\lib.rs:559:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Finally, I notice the one clue:  the left is four bytes (this is probably the first argument).

Then I try another thing. I decide to try to convert the input from a string to bytes to make the input easier.

send_message(b"1234567", b"abcdef");

I compile and see the following:

thread ‘main’ panicked at ‘assertion failed: `(left == right)`
left: `7`,
right: `64`’,

So, yes, that confirms that I can convert a string to bytes & pass it in and that the left 7 value is the number of bytes.  However, it still fails.  But my next guess is that is because the left is only 7 and it wants 64 bytes.  I create a 64 byte string on the left to pass in and try it again.

send_message(
  b"garbage here padd it out for 64 bytes see what happens 123456789",
   b"extra garbage")
It worked!

My Point: Technical Authors Have To Provide Details

Technical authors have to provide all the details of what they are explaining but there is often a lot of missing info.
Don’t give up and don’t feel like you are the only one this happens to.  It’s happening to all of us out here.

Leave a comment

Your email address will not be published. Required fields are marked *