Regex
regex
January 13, 20232 min read

Regex for SHA512 string

In this article let's understand how we can create a regex for SHA512 strings and how it can be matched.

SHA-512 (Secure Hash Algorithm 512-bit) is a cryptographic hash function that generates a fixed-size, 128-character (512-bit) output from any input data. It is commonly used to generate digital signatures for electronic documents and to verify the integrity of data, such as files downloaded from the internet.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Let's look at regex expression for base64 strings. We will try a few approaches right from simple approach to RFC or advanced approach.

Regex to match SHA512 string

Regular Expression for SHA512 String can be represented as-

/^[a-fA-F0-9]{128}$/gm

Alternatively you can use the i modifier in the regex takes care of the UPPERCASE letters.

/^[a-f0-9]{128}$/i

Test string examples for the above regex-

Input StringMatch Output
ThisIsNotSHA512does not match
a1f0b78b8c1320690327800e3a5de10e7dbba7b6c752e702193a395a52c727b6a1f0b78b8c1320690327800e3a5de10e7dbba7b6c752e702193a395a52c727b6matches
b32433a2e42321239baca23bf9c77889fccc5483fc142a31cdf07d115e44fc79b32433a2e42321239baca23bf9c77889fccc5483fc142a31cdf07d115e44fc79matches
zxcvbncvwfrqghdfjgyetwrteyrutituregfwfeghrgjhkjgfdsdghjdoes not match

Here is a detailed explanation of the above regex-

/^[a-fA-F0-9]{128}$/gm

^ asserts position at start of a line
Match a single character present in the list below [a-fA-F0-9]
{128} matches the previous token exactly 128 times
a-f matches a single character in the range between a (index 97) and f (index 102) (case sensitive)
A-F matches a single character in the range between A (index 65) and F (index 70) (case sensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
$ asserts position at the end of a line
/gm matches the characters /gm literally (case sensitive)
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Hope this article was useful to match SHA512 regex pattern.

Share this blog
Tagged in :
regex
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
January 13, 20232 min read
Next Article
January 13, 20232 min read