Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby - Can I create a md5 hash of length 8?

Tags:

ruby

md5

Is it possible to create a md5 hash of 8 characters long?

like image 267
Morin Avatar asked May 02 '11 06:05

Morin


People also ask

What characters are allowed in MD5 hash?

Each MD5 hash looks like 32 numbers and letters, but each digit is in hexadecimal and represents four bits. Since a single character represents eight bits (to form a byte), the total bit count of an MD5 hash is 128 bits. Two hexadecimal characters form a byte, so 32 hexadecimal characters equal 16 bytes.

Is MD5 fixed-length?

The MD5 (message-digest algorithm) hashing algorithm is a one-way cryptographic function that accepts a message of any length as input and returns as output a fixed-length digest value to be used for authenticating the original message.

How many bytes long is an MD5 hash?

The hash size for the MD5 algorithm is 128 bits. The ComputeHash methods of the MD5 class return the hash as an array of 16 bytes. Note that some MD5 implementations produce a 32-character, hexadecimal-formatted hash.

How do I create an MD5 hash?

An MD5 hash is created by taking a string of an any length and encoding it into a 128-bit fingerprint. Encoding the same string using the MD5 algorithm will always result in the same 128-bit hash output.


2 Answers

MD5 creates 16-byte hashes. You can of course crop the string to eight characters, as with myString[0..7], but note that this not a valid MD5 hash any more.

like image 55
DarkDust Avatar answered Oct 21 '22 16:10

DarkDust


require 'digest'

Digest::MD5.hexdigest("My secret")[0...8]
like image 39
RyanScottLewis Avatar answered Oct 21 '22 16:10

RyanScottLewis