Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
@cosmjs/crypto
Advanced tools
@cosmjs/crypto is a JavaScript library that provides cryptographic utilities for use in blockchain applications, particularly those built on the Cosmos SDK. It includes functions for hashing, signing, and verifying data, as well as generating and managing cryptographic keys.
Hashing
This feature allows you to hash data using the SHA-256 algorithm. The code sample demonstrates how to hash a string and output the hash in hexadecimal format.
const { sha256 } = require('@cosmjs/crypto');
const data = new TextEncoder().encode('Hello, world!');
const hash = sha256(data);
console.log(Buffer.from(hash).toString('hex'));
Key Generation
This feature allows you to generate random cryptographic keys. The code sample demonstrates how to generate a 32-byte private key and output it in hexadecimal format.
const { Random } = require('@cosmjs/crypto');
const privateKey = Random.getBytes(32);
console.log(Buffer.from(privateKey).toString('hex'));
Signing
This feature allows you to sign data using the Secp256k1 algorithm. The code sample demonstrates how to sign a hashed message and output the signature in hexadecimal format.
const { Secp256k1, sha256 } = require('@cosmjs/crypto');
const privateKey = Random.getBytes(32);
const message = new TextEncoder().encode('Hello, world!');
const hash = sha256(message);
const signature = Secp256k1.createSignature(hash, privateKey);
console.log(Buffer.from(signature.toFixedLength()).toString('hex'));
Verification
This feature allows you to verify signatures using the Secp256k1 algorithm. The code sample demonstrates how to verify a signature and output whether it is valid.
const { Secp256k1, sha256 } = require('@cosmjs/crypto');
const privateKey = Random.getBytes(32);
const publicKey = Secp256k1.makeKeypair(privateKey).pubkey;
const message = new TextEncoder().encode('Hello, world!');
const hash = sha256(message);
const signature = Secp256k1.createSignature(hash, privateKey);
const isValid = Secp256k1.verifySignature(signature, hash, publicKey);
console.log(isValid);
The 'crypto' module in Node.js provides cryptographic functionality that includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions. It is a built-in module and does not require installation. Compared to @cosmjs/crypto, it is more general-purpose and not specifically tailored for blockchain applications.
TweetNaCl is a cryptographic library that provides high-level cryptographic functions, including public-key encryption, secret-key encryption, signatures, and hashing. It is known for its simplicity and security. Compared to @cosmjs/crypto, TweetNaCl is more focused on providing a minimalistic and secure API for cryptographic operations.
Elliptic is a JavaScript library for elliptic curve cryptography. It supports various elliptic curve algorithms, including secp256k1, which is commonly used in blockchain applications. Compared to @cosmjs/crypto, Elliptic offers a broader range of elliptic curve algorithms and is more specialized in elliptic curve cryptography.
This package contains low-level cryptographic functionality used in other @cosmjs libraries. Little of it is implemented here, but mainly it is a curation of external libraries along with correctness tests. We add type safety, some more checks, and a simple API to these libraries. This can also be freely imported outside of CosmJS based applications.
This package is part of the cosmjs repository, licensed under the Apache License 2.0 (see NOTICE and LICENSE).
FAQs
Cryptography resources for blockchain projects
We found that @cosmjs/crypto demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.