Trying to create a sha256 key and hash in Elixir (converting from python code)

Mylan Connolly

I am in the middle of creating an Elixir project that uses Google Cloud Storage. Some of our customer requirements dictate that each customer utilize their own separate encryption keys.

I can create them using the Google code provided manually, however I was wondering about automating this (mostly out of my curiosity). The python code provided by Google is:

import base64
import hashlib
import os
key = os.urandom(32)
print "Key: %sSHA256 hash: %s" % (base64.encodestring(key), base64.encodestring(hashlib.sha256(key).digest()))

I thought I put together some Elixir code to do the trick:

key = 32 |> :crypto.strong_rand_bytes |> Base.encode64
hash = :sha256 |> :crypto.hash(key) |> Base.encode64
IO.puts "Key: #{key}\nSHA256 hash: #{hash}"

However, when I try to use my Elixir-generated key and hash Google complains as so:

{
  "domain": "global",
  "extendedHelp": "https://cloud.google.com/storage/docs/encryption#customer-supplied_encryption_keys",
  "message": "Missing a SHA256 hash of the encryption key, or it is not base64 encoded, or it does not match the encryption key.",
  "reason": "customerEncryptionKeySha256IsInvalid"
}

Naturally, the Python code works, so there seems to be some difference going on here.

Anyone have any ideas as to why this is? Thanks!

Patrick Oscity

It seems that in elixir, you are hashing the base64 encoded key, while the original python implementation hashes the raw bytes of the key.

The following should work:

key = :crypto.strong_rand_bytes(32)
base64_key = Base.encode64(key)
base64_hash = :sha256 |> :crypto.hash(key) |> Base.encode64
IO.puts "Key: #{base64_key}\nSHA256 hash: #{base64_hash}"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

sha256 hash from public key

trying to convert Java RSA-PSS signature verification code (with SHA256 hash, SHA1 MGF hash) to Python

Why is the sha256 hash key different from Command Prompt and Powershell

Double hash SHA256 in Python

How to use sha256 hash in Python

Python SHA256 hash computation

creating a HMAC SHA256 key in python

Javascript Sha256 vs PHP Sha256: Line breaks create different hash?

How do I specify the key for computing a SHA256 hash?

Generate HMAC SHA256 hash using key in C++

Matching cryptography in C# to NodeJS - create Hash SHA256

Laravel how to create sha256 hash with salt

Port JS crypto code for hash HMAC SHA256 to PHP

Equivalent JavaScript code to generate SHA256 hash

Verifying SHA256 Hash from MySQL Database in C#

Unable to obtain the same sha256 hash in python and javascript

Java SHA256 generates different hash as in Python

How Do I Convert a Picture into a Sha256 Hash Python

How to hash SHA256 byte 00 with Python?

hash each number of number list to sha256 with hashlib python

can I use sha1,sha256 lib generated string as a hash key

how to create a sha256 from an object in javascript?

How to create a SHA-256 hash of a string in Mason code?

How to replicate hash_hmac('sha256', $key, $secret_key) function in Swift 4?

SHA256 hash calculation

typescript crypto-js how to hash data using sha256 algorithm and key

SSH: agent key RSA SHA256:[hash] returned incorrect signature type

How to encrypt using AES with sha256 hash as key crypto++

Sha256 hash generated from Java differs from OpenSSL and Sha256Sum utilities. Why?

TOP Ranking

HotTag

Archive