What is tweetnacl-util?
The tweetnacl-util npm package provides utility functions for encoding and decoding data to and from various formats, such as Base64 and UTF-8, which are commonly used in cryptographic operations. It is a companion to the tweetnacl package, which is a cryptographic library.
What are tweetnacl-util's main functionalities?
Encoding to Base64
This feature allows you to encode a UTF-8 string into a Base64 string. This is useful for encoding data that needs to be transmitted over media that are designed to deal with textual data.
const naclUtil = require('tweetnacl-util');
const message = 'Hello, World!';
const encodedMessage = naclUtil.encodeBase64(naclUtil.decodeUTF8(message));
console.log(encodedMessage);
Decoding from Base64
This feature allows you to decode a Base64 string back into a UTF-8 string. This is useful for decoding data that was encoded for safe transmission over text-based protocols.
const naclUtil = require('tweetnacl-util');
const encodedMessage = 'SGVsbG8sIFdvcmxkIQ==';
const decodedMessage = naclUtil.encodeUTF8(naclUtil.decodeBase64(encodedMessage));
console.log(decodedMessage);
Encoding to UTF-8
This feature allows you to encode a string into a UTF-8 byte array. This is useful for preparing data for cryptographic operations that require byte arrays.
const naclUtil = require('tweetnacl-util');
const message = 'Hello, World!';
const utf8Bytes = naclUtil.decodeUTF8(message);
console.log(utf8Bytes);
Decoding from UTF-8
This feature allows you to decode a UTF-8 byte array back into a string. This is useful for converting data back into a human-readable format after cryptographic operations.
const naclUtil = require('tweetnacl-util');
const utf8Bytes = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]);
const message = naclUtil.encodeUTF8(utf8Bytes);
console.log(message);
Other packages similar to tweetnacl-util
base64-js
The base64-js package provides utilities for encoding and decoding data in Base64 format. It is similar to tweetnacl-util's Base64 encoding and decoding functionalities but does not include UTF-8 encoding and decoding.
text-encoding
The text-encoding package provides a polyfill for the TextEncoder and TextDecoder APIs, which are used for encoding and decoding text in various formats, including UTF-8. It offers similar functionality to tweetnacl-util's UTF-8 encoding and decoding features.
buffer
The buffer package provides a way to handle binary data in Node.js. It includes methods for encoding and decoding data in various formats, including Base64 and UTF-8. It offers a broader range of functionalities compared to tweetnacl-util.
String encoding utilities extracted from early versions of https://github.com/dchest/tweetnacl-js
Notice
Encoding/decoding functions in this package are correct,
however their performance and wide compatibility with uncommon runtimes is not
something that is considered important compared to the simplicity and size of
implementation. For example, they don't work under
React Native.
Instead of this package, I strongly recommend using my StableLib packages:
-
@stablelib/utf8 for UTF-8
encoding/decoding (note that the names of operations are reversed compared to
this package): npm install @stablelib/utf8
-
@stablelib/base64 for
constant-time Base64 encoding/decoding: npm install @stablelib/base64
Installation
Use a package manager:
Bower:
$ bower install tweetnacl-util
NPM:
$ npm install tweetnacl-util
or download source code.
Usage
To make keep backward compatibility with code that used nacl.util
previously
included with TweetNaCl.js, just include it as usual:
<script src="nacl.min.js"></script>
<script src="nacl-util.min.js"></script>
<script>
// nacl.util functions are now available, e.g.:
// nacl.util.decodeUTF8
</script>
When using CommonJS:
var nacl = require('tweetnacl');
nacl.util = require('tweetnacl-util');
Documentation
nacl.util.decodeUTF8(string)
Decodes string and returns Uint8Array
of bytes.
nacl.util.encodeUTF8(array)
Encodes Uint8Array
or Array
of bytes into string.
nacl.util.decodeBase64(string)
Decodes Base-64 encoded string and returns Uint8Array
of bytes.
nacl.util.encodeBase64(array)
Encodes Uint8Array
or Array
of bytes into string using Base-64 encoding.