How to perform CRC32 sum of your string in Node.js, Ruby, Go/Golang and Bash.

Bash in Ubuntu 20.04

CRC32 sum is installed by default. To perform a CRC32 sum, run the following command.

a=`mktemp`;echo -n 'hello world'>$a;crc32 $a;rm $a

You should see the same output as below if it ran successfully.

0d4a1185

Ruby

The CRC32 digest gem is not included in Ruby. To install it, run the following command to install it.

sudo gem install digest-crc

Create a file crc32sum.rb and copy the contents below into the file.

require 'digest/crc32'
str = ARGV[0]
puts Digest::CRC32.hexdigest(str)

To test if the file is working, run the following command.

ruby crc32sum.rb 'hello world'

You should see the same output as below if it ran successfully.

0d4a1185

Node.js

Assuming you already have npm installed in your Ubuntu 20.04. You’ll need to install CRC package with the following command.

npm install crc

Once completed, create a file named crc32sum.js and copy the contents below into the file.

const { crc32 } = require('crc');
var str = process.argv[2];
console.log(crc32(str).toString(16));

Now to test if the crc32sum.js file is working. Run the command below.

node crc32sum.js 'hello world'

If it ran successfully, you should see the same output.

0d4a1185

Go/Golang

The imported package are all part of Go/Golang default installation. No additional package install with “go get” necessary. Create a file “crc32sum.go” and copy the contents below into the file.

package mainimport (
"os"
"fmt"
"hash/crc32"
)
const (
// IEEE is by far and away the most common CRC-32 polynomial.
// Used by ethernet (IEEE 802.3), v.42, fddi, gzip, zip, png, ...
IEEE = 0xedb88320
// Castagnoli's polynomial, used in iSCSI.
// Has better error detection characteristics than IEEE.
// https://dx.doi.org/10.1109/26.231911
Castagnoli = 0x82f63b78
// Koopman's polynomial.
// Also has better error detection characteristics than IEEE.
// https://dx.doi.org/10.1109/DSN.2002.1028931
Koopman = 0xeb31d82e
)
func main() {
crc32q := crc32.MakeTable(IEEE)
str := os.Args[1]
fmt.Printf("%08x\n", crc32.Checksum([]byte(str), crc32q))
}

To test if the file is working, run the following command.

go run crc32sum.go 'hello world'

You should see the same output as below if it ran successfully.

0d4a1185

< Back to all the stories I had written

Written by

A software engineer, a rock climbing, diving & rollerblading enthusiast, a husband, a father.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store