Skip to content

Files

Latest commit

 

History

History
59 lines (40 loc) · 4.86 KB

binary_data_hex_string.md

File metadata and controls

59 lines (40 loc) · 4.86 KB

Cross-platform cryptography

Binary data to a hex string & back

The hex string is second most encoding to represent or transport binary data.

Why is the hex string not so often in use?

The answer is easy: each byte is converted into 2 characters (character set 0..9 and a..f) so this encoding doubles the size of data.

Why is a hex string so often used?

Especially when in debugging state of development phase the programmer wants to see what is in a variable and that can be better done than using a Base64 encoding or using decimals.

How does it work?

As a byte has a range of 256 numbers (decimal 0..255) the number is pushed into a 16 number base (compared to our decimal number space of 10) so we need to hexadecimal characters to get the value of one byte.

My implementations will provide the character in low or high chars, depending on the default of the framework.

This article focuses on the hex string encoding, if you like to know more about the Base64 representation of binary data see my article Base64 encoding & decoding.

⚠️ Security warning ⚠️

This is a serious warning regarding the security of the programs shown in these article series. Always keep in mind my disclaimer regarding my programs: All programs are for educational purposes and are not intended to use in production or any other programs where a secure solution is needed. The programs do not have proper exceptional/error handling and in some cases they use insecure key lengths or other methods that are insecure. Never ever use the programs in real life unless checked by a qualified professional cryptographer.

The following links provide the solution in code and within an online compiler that runs the code. Kindly note that three articles use the same example: Base64 encoding & decoding, binary data to a hex string & back and SHA256 hashing.

Language available Online-compiler
Java replit.com CpcJavaGenerateSha256Base64Hex#Main.java
PHP replit.com CspPhpGenerateSha256Base64Hex
C# replit.com CpcCsharpGenerateSha256Base64Hex#main.cs
Javascript CryptoJs replit.com CpcCryptoJsGenerateSha256Base64Hex
NodeJS Crypto replit.com CpcNodeJsGenerateSha256Base64Hex#index.js
NodeJS forge replit.com CpcNodeJsGenerateSha256Base64Hex#index.js
Webcrypto your browser WebcryptoGenerateSha256Base64Hex.html
Python replit.com CpcPythonGenerateSha256Base64Hex#main.java
Go replit.com CpcGoGenerateSha256Base64Hex#Main.go
Dart *1) no online compiler available

*1) you need the external library pointycastle version 3.1.1

This is an output:

Generate a SHA-256 hash, Base64 en- and decoding and hex conversions
plaintext:                The quick brown fox jumps over the lazy dog
sha256Value (hex) length: 32 data: d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592
sha256Value (base64):     16j7swfXgJRpypq8sAguT41WUeRtPNt2LQLQvzfJ5ZI=
sha256Base64 decoded to a byte array:
sha256Value (hex) length: 32 data: d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592
sha256HexString converted to a byte array:
sha256Value (hex) length: 32 data: d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592

Last update: Oct. 18th 2021

Back to the main page: readme.md