paint-brush
Hashing: What is it and How Does it Improve Website and App Search?by@algolia
781 reads
781 reads

Hashing: What is it and How Does it Improve Website and App Search?

by AlgoliaMay 4th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Hashing is a cryptographic technique that converts data into a fixed-size string of characters. Hashing is like a computer-science badge of identity — a sort of digital passport for data. In most cases, it’s used for applications in which data integrity and authentication are vital. Encryption is best for data confidentiality (as the primary concern).
featured image - Hashing: What is it and How Does it Improve Website and App Search?
Algolia HackerNoon profile picture


Hashing.


Yep, you read that right.


Not hashtags. Not golden, crisp-on-the-outside, melty-on-the-inside hash browns.


Hashing. And if you’re wondering what on Earth that is, you’re not alone.


Hash browns and hashing certainly conjure up wildly different images — or in the case of hashing, no image at all. Hashing isn’t a commonly used term that’s familiar to many people, but it’s still an integral part of modern computing.

Our Data-driven World

You may have noticed that there’s a lot of data on the Web, and the amounts are only growing every day. Much of that data needs to be compressed and stored in ways that make sense for servers. And in terms of user privacy, much of it needs to be kept safe from bad actors. We need fail-safe cybersecurity to protect it.


Enter hashing: a cryptographic technique that converts data into a fixed-size string of characters, which is known as a hash. Hashing is like a computer-science badge of identity — a sort of digital passport for data.


Used for organizing data and keeping it safe, hash codes are the fingerprint smudges that pepper our online files.


Every fixed-length hash is unique to the data it represents. If that data is tampered with, for example during transmission between servers, the hash value changes. This makes hashing a reliable method for ensuring that data is authentic and has been protected from unauthorized access.

Hashing Use Cases

Hashing isn’t always necessary. In most cases, it’s used for applications in which data integrity and authentication are vital. In other cases, encryption and data compression can be used to protect the confidentiality of data and to reduce the size of data files.


To hash or not to hash? It usually depends on the specific application-related goals. As a general rule:


  • Hashing is the best option for data integrity and authentication
  • Encryption is best for data confidentiality (as the primary concern)
  • Compression is good if you’re concerned only about file-size reduction


Here are some common use cases for hashing:

Password Storage and Authentication

Imagine that you have a locked treasure chest in your attic filled with steaming hash browns. You want to make sure no one can find them. You hide the key under your pillow, but then a thought crosses your mind… what if in the worst-case scenario, someone finds the key?


You decide that another layer of protection is needed. Grabbing a kitchen knife, you open the chest and cut up all the hash browns into unrecognizable shapes.


Pardon the goofy example, but this is essentially how hashing password security works. Using hashing algorithms, passwords are transformed into unrecognizable strings of letters and numbers, shielding the original passwords from view. If a bad actor gains access to a database, the passwords are still protected by their hash values and they’re irretrievable by the hacker.

Digital Signatures

In the Middle Ages, wax or clay seals were used to protect the authenticity of letters. To ensure that letters weren’t tampered with, the sender would melt hot wax or clay onto the flap. A signet ring or stamp was pressed into it to leave a signature and stamp of authenticity. If a letter arrived with a broken seal, the recipient knew it had been tampered with.


In the same way, hash values are like seals for digital documents. A hash value that is not identical to the one on the original document is a clear giveaway of unauthorized access.

File Verification

A large-scale drive-by-download attack is a bit like a drive-by shooting. It can happen before you know it, and rock you (and your file security) to the core.


It’s also a key malware strategy for attackers, with downloaders accounting for 41% of attacks. With such a large proportion of potential information-retrieval attacks coming in through downloads, hashing helps protect user devices and their contents from malicious code.


As with digital signatures, hash values serve as an intermediary between end-to-end download and device. A file that doesn’t match its original hash value will be blocked, preventing any malware from entering the device.

Hash Functions

How do you make hash browns?


  1. Grate potatoes
  2. Fry until crispy and golden brown
  3. Season with salt and pepper


That’s it. And in the same spirit, hashing has a process behind it, known as the hash function, an algorithm that takes specific data as input and produces a hash value at the other end. Even the slightest change in the input data will result in a different hash value.


A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called hash values, hash codes, digests, or simply hashes.


As Wikipedia explains, a hash table “uses a hash function to compute an index, also called a hash code, into an array of buckets or slots, from which the desired value can be found. During lookup, the key is hashed and the resulting hash indicates where the corresponding value is stored.”

Types of Hash Function

People have their own ways of making hash browns. And just as with air fryers and gas stoves, different functions can be utilized to produce a hash value. These different hash functions are used in different applications depending on security requirements and other functionality (e.g., digital signatures, file verification).


Here are a few hashing techniques:


  • MD5 produces a 128-bit hash value. This function was widely used in the past but has a vulnerability to collisions (which we’ll discuss next).
  • SHA-1 produces a 160-bit hash value. It’s been replaced by the more secure SHA-2 and SHA-3 families.
  • SHA-256 and SHA-512 produce 256- and 512-bit hash values. These functions are primarily used for digital signatures and certificates.
  • bcrypt is useful for password encryption, as it’s resistant to brute-force attacks.

Hash Collisions

A “collision” in hashing isn’t as deadly as it sounds. A collision happens when two different hash functions generate the same hash code. Rather than mangled cars, the outcome of a computer collision often has no impact. In simple terms, it’s like having two identical digital fingerprints, or the same keys for two different houses.

Hash Collision Resolution

With hashing, the aim is always to reduce the number of collisions, as these pose risks to both the integrity of the hashing system and the security of data. Here’s why.


Undermining the integrity of hashing

The possibility of collisions in the hashing method is a fatal flaw. It undermines the integrity of the system and potentially compromises security, making it difficult to detect unauthorized changes to data. If two identical hash codes exist within a database — if that possibility exists — then it can slow down data retrieval and compromise the authenticity of files.

A security risk to data

When there’s a high risk of collisions in a hash function, it poses a small security risk to data. Attackers are able to exploit this vulnerability in the system, creating “malicious” different inputs that can produce the same hash code, and then using these to gain access to a server or application. Identical hash codes also disturb the authenticity of data in a database and are more likely to produce leakages. So it’s vital that hash functions contain a low probability of collisions in order to fortify data as well as possible.

Hashing in the Blockchain

Imagine you have a magic set of Lego bricks, which stick together as you build. On each brick is written a big number in black marker pen. On a square red brick, the number 9. On a long, blue brick, 134. You’re building a tower, and as you click the bricks together, they fuse permanently. As you build, you realize you’re not just creating a tower but a series of numbers, indelibly stuck together: 9-134-45-6-09-3267-67.


The blockchain is like this tower, except instead of bricks you have blocks (units of data), and instead of numbers you have hash codes. When the blocks in a blockchain are connected, the data is difficult to remove or change.


Hashing plays an important role in the blockchain for several reasons:


  • Hash codes are unique and irreplaceable, and so ensure the integrity of data
  • Transactions can be verified fast, with the hash codes used for reference in each case
  • There’s a high level of security against tampering: any attempt to alter the data in a block results in a change in the hash code, which is then detected by the network

Hashing in Search Technology

Search engine databases are typically vast and contain massive amounts of data. People’s entered search queries can vary substantially. When a user enters a search term in a search box on a website or in an app, the search algorithm has to spring into action and:


  1. Recognize the search term
  2. Identify the units of data in the database it relates to
  3. Instantly offer up the relevant results from the database


To speed up the data-retrieval process and make the search results more accurate, artificial intelligence–aided search engines like Algolia utilize hashing algorithms. When a user enters a search term, the algorithm (hash function) creates a unique hash code, which is linked to a relevant piece of data in the search engine database. Once created, this hash can be quickly searched and matched to the search term, allowing the search engine to provide accurate search results more quickly.


In recent years, hashing has become crucial for quickly generating accurate long-tail search queries that hit the mark.


So, there you have it: hashing in all its digital-fingerprint glory. Not nearly as enticing to imagine as hash browns, but (dare we say) more important.

Neural-based hashes are lowering the barrier for search technology. Neural hashing is a technique that allows us to compress vectors without losing information. Neural hashing makes vector-based search happen as fast as keyword search.


Neural search encompasses interconnected-node-based “thinking” on the part of algorithmic components known as neural networks. For instance, a convolutional neural network, or CNN, a network architecture for deep learning, excels at making sense of search queries. It’s flexible, and it works well when system training data and input continually change, as happens all the time in ecommerce. Added bonus: instead of making and updating rules for a machine learning model, you can start with a trained neural network, and then the model can become progressively better “educated,” for instance, in terms of semantics.


Like to know more about how Algolia can help you improve your search functionality and conversion metrics while keeping your data secure and authenticated? We look forward to hearing from you so we can give you the rundown on successful — and profitable — search and discovery optimization for your site or app.


Also published here.