I'm a complete cryptography beginner and I'm interested in the cryptography behind Bitcoin, hashing, key cryptography etc. I suspect that SHA-256 can only take Alpha-Numerics but I was wondering if anybody could confirm this or explain otherwise, I've scrubbed the web for answers but found nothing.
-
1$\begingroup$ SHA-256 takes a sequence of bytes. $\endgroup$– SEJPMDec 4, 2016 at 10:12
-
$\begingroup$ Forget Bitcoins for a while. If you assume nonsensical things and "found nothing" for one of the most well-known algos, you need more basics. $\endgroup$– deviantfanDec 4, 2016 at 13:46
1 Answer
SHA-256, as part of the Secure Hash Standard or SHS is defined to operate on bits, to be precise in Section 6.2: SHA-256:
SHA-256 may be used to hash a message, $M$, having a length of $l$ bits, where $0 \leq l < 2^{64}$.
That said, you won't easily find an implementation that does precisely that; usually only bytes (or, by another name) octets are taken.
This is not just true of SHA-256 but also for many other algorithms such as the derived HMAC algorithm, block cipher modes of encryption, RSA (as specified in PKCS#1), etc..
In some sense 8 bit bytes are the atoms of computers. You can still split them up into bits (protons, neutrons and electrons), but the result is difficult to handle. So almost all cryptographic API's are defined on bytes instead of bits, while the algorithm is usually described using bits.
To handle any other data you need to encode the data as bytes. The reason to use SHA-256 is to generate a semi-unique value for a large input that cannot be converted back to the original value.
This means that the encoding of the data should be unique as well; this is called a canonical encoding. One example of this is ASN.1/DER, which is used to sign the X5.09 certificates used in your web browser.
Alphanumeric data can be converted to bytes using a character-encoding such as ASCII or UTF-8. Of course the encoding should be known in advance or you might get different values for the same string, e.g. if one is encoded using Windows-1252 and the other in UTF-16LE, to name just two random other character-encodings.
Note that some runtime environments contain SHA-2 implementations that implicitly convert characters to bytes. A platform itself may also have a default character representation (e.g. PHP).
Furthermore, the output of SHA-2 may also be converted to a hexadecimal string or something similar. This is however a decision by the runtime/API designer and not of the SHA-2 algorithm; the SHA-2 algorithm is defined to operate on bits.