# How hard/easy will be to break a SHA-1 or SHA-256 hash if the attacker knows part of the original text?

I'm creating an algorithm to hash some text variables (20/30 characters long) to send them across an insecure channel. I'm not dealing with high security data, so no credits card numbers or any other very sensitive information.

The original text variable is composed like this:

{secret-client-code,public-random-code}


Then that is hashed and sent across the channel. However, the public random code is sent raw in the channel, so an attacker will know it. Does this make my hashing vulnerable to an attacker considering that the random code is about 30% of the whole variable? (I can change this if needed.)

-
Similar question (with no answer though): crypto.stackexchange.com/q/6441/6961 – e-sushi Oct 5 '13 at 18:30
What is your objective? Your objective is likely not creating an algorithm to hash some text variables, since that objective is directly met by applying SHA-1 or SHA-256. If your objective is limited to send text over an insecure channel, there's nothing to prevent you from doing that. We are left guessing that your objective likely is ensuring the origin, or/and protecting the integrity, or/and the confidentiality, of some of the text sent, by mean of hashing. Also we do not know what we can assume the sender and receiver initially have as key material. – fgrieu Oct 6 '13 at 10:05
It depends on how long and random the secret and public data are. Looks like password hashing to me and if the channel is insecure you might want to consider other options. I agree with fgrieu's comment. – rath Oct 6 '13 at 23:21

It's probably a really bad idea to try and roll-your-own here. If you have to ask a question like this, you would probably just be better off using shrink-wrapped solution like SSL rather than trying to make your own protocol. There are a lot of tricky problems around getting these kinds of things right.

However, if you insist on doing this yourself, there are constructions that allow you to do what you want to achieve here.

What you want is a message authentication code (MAC). You can MAC the hash of the variables and then send this across the insecure channel.

In order to do this safely, you need to use a construction like HMAC. You should particularly use such a construction if you are using SHA-1/SHA-256 as they suffer from message extension attacks.

You also need to guard against replays; that is, where an attacker resends a previously seen message on the channel with the hope of repeating an action or causing some other nefarious effect.

These are just a flavour of the kind of bear-traps that can spring when trying to design your own protocol!

-
Doesn't HMAC contain a method to avoid extension attacks? – Maarten Bodewes Oct 5 '13 at 20:16
@owlstead It does. However, the fact that such attacks exist against SHA-1 and SHA-256 but not SHA-3 is probably a reason to prefer SHA-3 over SHA-1 and SHA-256. – Simon Johnson Oct 5 '13 at 20:24
Nevermind, I misread. It's easy to misread that part of the answer though. I'll place a tiny edit if you don't mind... – Maarten Bodewes Oct 5 '13 at 20:32
Sorry, removed mention of SHA-3 as well, as it does not seem to be fully defined (NIST is likely to tweak it a bit more) – Maarten Bodewes Oct 5 '13 at 20:34