62

What are the differences between SHA1 and RSA? Are they just different algorithms or are they fundamentally (i.e. used for different things) different on some level.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Ted Smith
  • 9,415
  • 16
  • 50
  • 52
  • What would you mean by "fundamentally different"? – David Z Apr 09 '09 at 10:51
  • Related: [What are the differences between MD5, SHA and RSA?](http://security.stackexchange.com/questions/2298/what-are-the-differences-between-md5-sha-and-rsa), [SHA, RSA and the relation between them](http://security.stackexchange.com/questions/9260/sha-rsa-and-the-relation-between-them). – informatik01 Feb 03 '14 at 12:50

7 Answers7

112

Fundamentally different.

SHA1 is a hash algorithm, which is a one way function, turning an input of any size into a fixed-length output (160 bit in this case). A cryptographic hash function is one for which it should not be possible to find two inputs giving the same output except by brute force (for instance, with a 128-bit function you should need to try on average 2^64 message to find such a "collision" due to something called the birthday paradox - Google it for more).

In fact for SHA1 this is no longer the case - the algorithm is (in cryptographic terms at least) broken now, with a collision attack described by Xiaoyun Wang et al that beats a classic birthday attack. The SHA2 family is not broken, and a process is underway by NIST to agree on a SHA3 algorithm or family of algorithms.

Edit - Google have now generated and published an actual SHA1 collision.

RSA is an asymmetric encryption algorithm, encrypting an input into an output that can then be decrypted (contrast a hash algorithm which can't be reversed). It uses a different key for encryption (the public one) than for decryption (the private one). This can therefore be used to receive encrypted messages from others - you can publish your public key, but only you with the private key can then decrypt the messages that have been encrypted with it.

If you reverse the keys for RSA, it can be used to generate a digital signature - by encrypting something with your private key, anyone can decrypt it with the public key and, if they are sure the public key belongs to you, then they have confidence that you were the one who encrypted the original. This is normally done in conjunction with a hash function - you hash your input, then encrypt that with your private key, giving a digital signature of a fixed length for your input message.

David M
  • 71,481
  • 13
  • 158
  • 186
  • The problem with collision is traditionally called "birthday paradox", not "effect". – sharptooth Apr 09 '09 at 10:57
  • Have heard it called both, but will edit. When applied to trying to find a collision on a hash function, use of this paradox or effect is then called a "birthday attack", which is the term I'm most familiar with of the three! – David M Apr 09 '09 at 10:59
  • 12
    One more note is that hash algorithms, like SHA-1, can compute digests given data of any length as input. Asymmetric algorithms, like RSA, are limited in the length of data they can transform. For that reason, the original message is rarely signed with RSA, and instead the SHA-1 digest of the original message is signed. The recipient of the message and its signature computes the SHA-1 digest of the message, then decrypts the signature with the sender's public key and verifies that the digests exactly match. – jordanbtucker Feb 23 '13 at 07:20
  • 1
    Good explanation-do you recommend using AES-256 or RSA for e2e encryption? – ha9u63a7 Aug 03 '17 at 21:17
17

Answers before are already sufficient explanations. But i think you ask this question just because SHA and RSA often come out together.So let me explain why.

First,keep in mind that

RSA is not efficient, but SHA is.

Suppose you download Windows 7, and want to make sure if it's original Windows 7 from Microsoft. If Microsoft just encrypts Windows 7 by RSA, it will takes a really long time to do that we can't stand it at all. So Microsoft uses SHA1 on Windows 7 and generates a 160 bits long data. And then Microsoft sign this 160 bits long data with RSA (use its private key).

Then, all you need to do is that make sure your public key is from Microsoft. And then use SHA1 to generate a 160 bits long data too. And then use public key of RSA to decrypt the signature of Microsoft. Then you just compare the two 160 bits long data to see if they match.

demonguy
  • 1,977
  • 5
  • 22
  • 34
  • 1
    You shouldn't talk about encryption with a private key when you mean signatures. – CodesInChaos Jan 22 '15 at 09:41
  • 1
    What do you mean? AFAIK, the signature is just data encrypted by RSA from 128 bit long data generated by SHA (at least in this case), am i wrong? – demonguy Jan 22 '15 at 09:43
  • 1
    A signature uses the RSA private key operation, just like decryption uses the private key operation. Encryption always uses the public key and adds encryption padding like OAEP. Decryption always uses the private key and verifies the encryption padding. Signing on the other hand hashes the message, adds signature padding like PSS and then uses the private key operation. Verification happens using the public key. – CodesInChaos Jan 22 '15 at 09:50
  • Also the biggest issue with using RSA signing without hashing the message is that you can't fit more than about 200 bytes into a single block of RSA. Signing by splitting the message into blocks and applying the RSA private key operation to each block similar to ECB mode is not secure. – CodesInChaos Jan 22 '15 at 09:51
  • 1
    A fine point (and it may be inaccurate), but [SHA1 generates 160 bits, not 128](https://www.keycdn.com/support/sha1-vs-sha256/). – Al Lelopath Jun 22 '17 at 15:18
12

SHA1 is a hashing algorithm (Document and certification signing) while RSA is an encryption/decryption algorithm (Secure communications).

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
5

SHA1 is a cryptographic hash function, whereas RSA is an algorithm for encryption.

A hash function takes a piece of data and return a string of fixed length. In a cryptographic hash function all return strings have the same probability. Given only the hash number you can't determine the input, nor can you find another input that gives the same hash (excpetion with a very very small probability). Sha1 has some security flaws.

An algorithm for encryption gives takes a piece of data, but the output is not of fixed length - your encryption. Given the output (the encryption) you can (if you have the right keys) determine the input.

AnnaR
  • 3,166
  • 6
  • 35
  • 39
5

As others have commented, they are fundamentally different things serving different functions. You use RSA to scramble information into a seemingly random form, while you use SHA1 to ensure the integrity of the message (i.e. none of the bits have changed). In a security application, you will use one or both, depending on what functionality you need.

sybreon
  • 3,128
  • 18
  • 19
1

SHA is a 'one-way' encryption algorithm.It means you can't reach the input text by having output(result of algorithm).

RSA is a 'two-way' encryption decryption algorithm. It means you can reach input data(raw text) by having output (ciphered or encoded text).

Vahid Hassani
  • 676
  • 1
  • 6
  • 20
-5

The Secure Hash Algorithm (SHA) algorithm takes a message of less than 264 bits in length and produces a 160-bit message digest. The algorithm is slightly slower than MD5, but the larger message digest makes it more secure against brute-force collision and inversion attacks. The algorithm specified in the Secure Hash Standard (SHS, FIPS 180), was developed by NIST. SHA-1 is a revision to SHA that was published in 1994; the revision corrected an unpublished flaw in SHA. Its design is very similar to the MD4 family of hash functions developed by Rivest. SHA-1 is also described in the ANSI X9.30 standard.

RSA is an algorithm for public-key cryptography. It is the first algorithm known to be suitable for signing as well as encryption, and one of the first great advances in public key cryptography. RSA is widely used in electronic commerce protocols, and is believed to be secure given sufficiently long keys and the use of up-to-date implementations.

The most notable difference is that SHA is an encryption algorithm whereas RSA is both an encryption as well as signing algorithm.

On 8/16/2005 it was announced that it is possible to find a collision in SHA-1 in 2^63 operations. This research result is due to Professor Xiaoyun Wang of Tsinghua University in Beijing, together with Professors Andrew Yao and Frances Yao. It extends the work of Wang, Yin, and Yu, which demonstrated that a collision could be found in 2^69 operations. What that means is that it is easier for a collision to occur in SHA than in RSA - but notably, no two similar keys have ever been found that collided.

sangupta
  • 2,396
  • 3
  • 23
  • 37