# Hashing algorithms [closed]

How can you tell which hashing algorithm is being used on a system? We are using 2048 bit encryption and wondering how to tell if hashing algorithm SHA-1 with SHA-2 is being used.

## closed as unclear what you're asking by Xander, TildalWave, Adi, Steve, GillesMar 28 '14 at 21:07

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

Well, first of, hashing does not actually help in encryption (strictly speaking, it can, with OAEP padding, but I strongly doubt that applies to your case). I suppose that what you call "encryption" is actually a digital signature. Digital signatures are unfortunately often described as "encryption with the private key", which is a flawed analogy, since it works only for RSA, and does not actually work for RSA, only "sort-of". It is an historical pedagogical gimmick which has gone wrong decades ago, and is endlessly parroted by hordes of bloggers and ill-informed teachers, leaving the common bystander stuck in a gloomy swamp of mental confusion.

Therefore, assuming that you really want to talk about signatures, your question becomes clearer. You are using RSA-based signatures, and a RSA signature generation or verification begins with hashing; the RSA magic with the modular exponentiation comes only afterwards, and works with the hash value.

Since whoever verifies the signature must also compute the hash value, which hash function to use is not normally a secret. Most signature formats include a header which specifies it. For instance, if you look at a X.509 certificate and decode it with OpenSSL, the command-line tool will print out the signature value with something like this:

``````\$ openssl x509 -text -noout -in cert.pem
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 14070511318992561002 (0xc3447f7ef0aa236a)
(...)
Signature Algorithm: sha1WithRSAEncryption
93:00:8e:ac:16:77:3c:dc:ba:89:fc:63:5f:74:c7:bb:72:3a:
6c:af:26:eb:62:04:5b:65:37:cf:c2:41:8e:de:03:db:20:c4:
``````

The `sha1WithRSAEncryption` states quite clearly that in this case, RSA is being used with SHA-1 as underlying hash function.

If you are doing some reverse-engineering and such a header does not appear to be available, you may still infer it from a signature value, provided that you have the public key around, and are not afraid to do some computation on big integers. Namely, most RSA signatures out there will use PKCS#1, and more precisely the "old-style v1.5 padding". With this padding, a signature generation does thus:

1. The message m is hashed into h(m).
2. h(m) is stored in a structure which includes an explicit header identifying the hash function.
3. The resulting structure is left-padded by adding, in that order, a byte of value 0x00, a byte of value 0x01, many bytes of value 0xFF, then a byte of value 0x00.
4. The resulting padded string is interpreted as a big integer modulo n (n is the RSA modulus, part of the key pair), and subject to the modular exponentiation which is at the core of RSA.

Using a given signature s, and the public key (n, e) (n is the "modulus", e is the "public exponent"), you can recompute se mod n and this should yield the "padded sequence", which is recognizable due to its long sequence of 0xFF bytes; and then you can see the structure header which identifies the hash function, exactly the information which you are after.

When I must do such things, I use my Linux-based laptop because it comes with the `dc` tool, a nifty command-line calculator which handles big integers (including modular exponentiation) and hexadecimal input/output.

In any case, see PKCS#1 for details. You cannot hope to do such investigations seriously without acquiring some reasonable grasp of the inner functioning of RSA, and PKCS#1 is a good place to start.

Anyway, since both the signature generation system, and the signature verification system, must agree on the hash function to use, this gives you two places where you can look for the information.

You cannot, by looking at a hashed sequence, determine what kind of hash did generate it. If you could, that would strongly hint at a non-randomness - even worse, recognizability - of the hash, which would make it less useful for security purposes.

You can however tell between a SHA-1 or SHA-2 hashed value from its length. SHA-1 is 160 bits (20 bytes) in size, while SHA-2 may go from 224 to 512 bits.

sha-1 is 40 characters in the hexidecimal representation: 9BE083742A3494DD4DEE4904451AECE5394F5BAA

sha-2 will be longer. sha-256 is one example of sha-2 and has hexidecimal representation of 32 characters:

C3F41471B1E8C1505C8E4AC766E972FC108262EAA45CC1F30FAC8154B0BBF79D

sha-512 is also sha-2 and it is, well, however many this is (128 characters):