Calculating HMAC

I’ve been trying to learn how HMAC is calculated but seem to be missing something. According to this wiki page, calling HMAC-SHA1 on an empty key and message should result in a value of fbdb1d1b18aa6c08324b7d64b71fb76370690e1d.

I’ve confirmed it with:

$   echo -n "" | openssl dgst -sha1 -hmac ""  (stdin)= fbdb1d1b18aa6c08324b7d64b71fb76370690e1d 

Following the pseudocode in the wiki page, I tried running this in bash:

ipad=$  (printf '\x36%.0s' {1..64})  opad=$  (printf '\x5C%.0s' {1..64})  echo -n $  opad$  (echo -n $  ipad | sha1sum | awk '{print $  1}') | sha1sum 

I thought since the key is empty, the inner padded key will just be 64 bytes of \x36 characters and \x5C for the outer padded key. And since the message is also empty, HMAC can be calculated via SHA1(o_key_pad||SHA1(i_key_pad)). But it returns a different result. What am I missing?

Calculating sha256 returns also “-bash: command not”

I’m trying to find a SHA256 hash of a string:

$   fdsafdsa | shasum -a 256  

While it returns a hash, it also throws an error:

-bash: fdsafdsa: command not found e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  - 

I’ve tried using quotes — to no avail. How to get rid of “-bash: fdsafdsa: command not found”?

Calculating the best exchange rate for a given currency based on direct conversion vs multiple currency conversion

I am not sure this is the best place to ask this question. Anyways, here it goes. I live in Mauritius and i am travelling to Tokyo soon. So i would like to exchange some currencies before leaving Mauritius, the issue is i am unable to find a simple (mathematical) function that will help me determine what is the best option for me:

Option 1: I exchange my mauritian rupee to USD and then when in tokyo, i exchange the USD to JPY Currently the exchange rate(including all fees) is as follows: 1 USD -> MUR 34.28 and in tokyo 1 USD -> JPY : 106 JPY

And my second option is

MUR to JPY in my country 100 JPY = MUR 31.20

Is there any mathematical way/formula i could use to determine which is the best for me?

Calculating the end-to-end delay of a message sent over a network

This is a problem from a MOOC on computer networking:

We wish to send a message of size $ 150,000$ bytes over the network. There are four hops, each of length $ 20$ km and running at $ 100$ Mb/s. However, before sending we split the message into $ 1500$ byte packets. What is the end-to-end delay of the message? Use speed of light in copper $ c = 2 * 10^8$ m/s, and round your answer to the nearest integer millisecond.

HINT: Break the problem into two parts: the end-to-end delay of one packet and the delay of the rest of the message across the slowest link.

After struggling for a bit, I obtained the answer by following the hint as follows:

$ $ 4\left(\frac{1500 \ B * 8 \ (b/B)}{100 \ Mb/s} + \frac{20 \ km}{2 * 10^8 \ m/s}\right) + \left(\frac{150000}{1500} – 1\right)\left(\frac{1500 \ B * 8 \ (b/B)}{100 \ Mb/s}\right) = 12.76 \ ms $ $ The answer is correct but I don’t understand why the packetization delay only was considered for the rest of the message. Why wasn’t the propagation delay also considered for the remaining packets?

Calculating Damage from from object collision

I need to calculate the damage resulting from a crash of two objects.

I found the formula of the resulting kinetic energy:

E = m * v² / 2 

I guess I also have to use the masses or sizes of the objects as well.

It will make a difference if the spaceship collides with a mountain (which certainly will make both of them dealing heavy damage), or the spaceship is colliding with a floating tennisball in which case neither of them are dealing damage.

Do I include system dependencies when calculating Instability?

Given the formula to calculate instability…

I = (Ce / (Ca + Ce)) with Ce = outgoing dependencies, Ca = incoming dependencies, and I = Instability,

…should I include system dependencies (such as System, System.Data, System.XML, etc.) when counting outgoing dependencies (Ce)? Or, do I just count it as one outgoing dependency?

Background Info

I have been studying this topic in an academia environment. I’m starting to apply what I’ve learned, thus where this question derived. More info on the topic can be found at this link.

Calculating probability for ranked elements in set

I’d like to have a function which takes x1,x2..xn, and returns y1,y2..yn

x – product rated at range 0-100

y – probability of choosing this product by the customer

This is for computer simulation. Customer will chose product from products set (x1,x2…xn) Selection is random, but depends of probability of choice. Probability is based on product rating. If each product rating is similiar, probability of chosing each product is almost same. But if one product is much better (at least 50%) than other, then probability of chosing better product is much higher.

expected results similiar to those:

  • x1: 51 y1: 16%
  • x2: 76 y2: 84%

  • x1: 51 y1: 13.5%
  • x2: 76 y2: 84%
  • x3: 33 y3: 2.5%

  • x1: 51 y1: 9%
  • x2: 76 y2: 84%
  • x3: 49 y3: 7%

  • x1: 52 y1: 38%
  • x2: 51 y2: 32%
  • x3: 49 y3: 30%

Any ideas?

Calculating Ranks in Google Sheets

I have a spreadsheet that has three tabs:

The first tab is raw data imported from a website. it’s about 10 columns and up to 300 rows. it has a mix of numbers in decimal form and percent. it all comes over as text though

The second tab is for ranking the values in the first tab. it stores the min and max values for the rankings 3 to -3. each column from the raw data tab has its own ranking ranges.

the third tab is a mirror of the raw data tab, but instead of number values, it calculates the rank of the value in this tab.

I’m trying to figure out the best way to do the calculations. it is a lot of data and the way I am currently doing it bogs down the system tremendously.

I call a function that has two parameters – column and value. the function calculates the ranking ranges for that column and stores it in local variables in the function, then a large nested if statement compares the value to the range variables and returns the ranking to that cell. it runs for each cell in 300 rows, 10 columns. also, I have to convert the percent values from text to numeric values in the second parameter of each function call, so it is a lot of calculating.

I there an easier, more efficient way to get this ranking? Would nested if statements in the cell formula field run smoother? or should I add more tabs to get the data into a more user-friendly format before calling my function?

I get a lot of errors in the cells saying I call the function too many times

Statistics injection for calculating the cost of a query

I want to find out the cost of a query by “EXPLAIN” query in Postgres. As I know, Postgres calculate this cost by using the statistics of the table. (e.g: relpages, reltuples in pg_class or some attributes in pg_statistics) Is it possible to inject these statistics on a dummy table to get the cost without storing the data locally on the database?

How do I prevent or omit a negative value when calculating between times?

I’ve researched through the site to see if this has been answered but with no luck after reading:

  • How do I calculate a time delta in my Google Spreadsheets?
  • Calculating time difference
  • Calculating time worked per day
  • Time duration, when converted to decimal, shows negative

In Google Sheet I’m trying to measure time accounting for a break:

enter image description here

When I add the first start time I get a negative:

enter image description here

when I add a clock out it’s ok:

enter image description here

Starting the time back up it throws that time off:

enter image description here

How can I calculate the time that has an IN and OUT but omit the time when it only has an IN value?

The formula:

=ROUND((ARRAYFORMULA(IF(ISBLANK(B3:C3)=FALSE,((C3-B3)*24*60/60),""))) + ARRAYFORMULA(IF(ISBLANK(D3:E3)=FALSE,((E3-D3)*24*60/60),"")),2)