Compute Base64 Encoded Hash from a Given Hash

Compute base64 encoded hash from a given hash?

You're not base-64 encoding the hash. You're base64-encoding the string

(stdin)= 32cb0518343aff67ae2f9c504704db4a9679322eee50f20b53366ea0b0661cef

(followed by a newline). Note the (stdin)= at the beginning. That's part of the string. That's going to have a different value than the base64 encoding of the string 32cb0518343aff67ae2f9c504704db4a9679322eee50f20b53366ea0b0661cef (followed by a newline).

If your goal is to get binary hashes (rather than their string encodings), then use the -binary option to openssl dgst.

I'm not familiar with hashdeep, but when you say "non-encoded hash," that's not what it's generating. It's generating a hex-encoded hash. By the looks of it you're really looking for a hex-to-base64 converter. You can do that along these lines:

echo '32cb0518343aff67ae2f9c504704db4a9679322eee50f20b53366ea0b0661cef' | xxd -r -p  | base64

xxd -r -p converts a hex-encoded string into raw data.

(If you're using openssl dgst, just make sure that you're using options that don't inject (stdin)= on the front. My version of openssl doesn't do this, so I'm not certain what flag you'll need.)

Converting a hash to Base64 encoding: Should I be storing the hash as a String or Byte before encoding?

Base64 encodes bytes, the hasher produce bytes, so there's no reason to convert to String.

I suppose examples you have seen are about converting base64 starting from strings variables.

Different result are due to different encoding of toString() or getBytes() conversions.

From guava documentation you can see hashcode.toString() converts in exadecimal, while bytes.toString() is platform dependent (supposedly UTF8)?

https://guava.dev/releases/16.0/api/docs/com/google/common/hash/HashCode.html#toString()
https://guava.dev/releases/16.0/api/docs/com/google/common/hash/HashCode.html#fromString(java.lang.String)

Convert hex string hash to base64

So this function will give you the base64 encoded SHA-1 hash of a given password:

def sha64(String password) {
password.digest('SHA-1').decodeHex().encodeBase64()
}

And if you do:

println sha64('welcome2022')

It prints

wjC4KfO5XfMIRhi45M/VA/0i8NA=

MD5 Hash and Base64 encoding

An MD5 value is always 22 (useful) characters long in Base64 notation. Many Base64 algorithms will also append 2 characters of padding when encoding an MD5 hash, bringing the total to 24 characters. The padding adds no useful information and can be discarded. Only the first 22 characters matter.

Here's why:

An MD5 hash is a 128-bit value. Every character in a Base64 string contains 6 bits of information, because there are 64 possible values for the character, and it takes 6 powers of 2 to reach 64. With 6 bits of information in every character, 21 characters has 126 bits of information, and 22 characters contains 132 bits of information. Since 128 bits cannot fit within 21 characters but does fit within 22 characters (with a little room to spare), a 128-bit value will always be represented as 22 characters in Base64.

A note on the padding:

I mentioned above that many Base64 encoding algorithms add a couple of characters of padding when encoding an MD5 value. This is because Base64 represents 3 bytes of information as 4 characters. Since MD5 has 16 bytes of information, many Base64 encoding algorithms append "==" to designate that the input of 16 bytes was 2 bytes short of the next multiple of 3, which would have been 18 bytes. These 2 equal signs add no information whatsoever to the string, and can be discarded when storing.

How to compute an SHA256 hash and Base64 String encoding in JavaScript/Node

You don't need any libraries to use cryptographic functions in NodeJS.

const crypto = require('crypto');

const hash = crypto.createHash('sha256')
.update('test')
.digest('base64');
console.log(hash); // n4bQgYhMfWWaL+qgxVrQFaO/TxsrC4Is0V1sFbDwCgg=

Terraform - SHA256 hash of bytes that have been base64-encoded

The Terraform language has a string type that is defined as containing Unicode characters rather than bytes, so anything which creates a string from raw input requires that input to be UTF-8 encoded Unicode characters.

This means that it isn't possible in general to pass raw binary data from one function to another. For situations where a Terraform configuration does need to work with small binary objects, the convention is to pass them around as base64 encoding, but that works only if your configuration treats the base64 data as opaque, never needing to decode it, because there is no way to represent the decoded form.

Given that, I think in order to solve your underlying problem with Terraform you will need to take a different approach. For example, perhaps whatever external system is generating the base64 string could also generate the SHA256 checksum of it, which Terraform would then also treat as opaque rather than trying to calculate it directly. You've presented one technical solution to an unstated problem here, so I can't be any more specific about what other options might be, but the main requirement would be that Terraform will just pass through the (base64-encoded) binary data verbatim to a provider and never interrogate it directly itself.



Related Topics



Leave a reply



Submit