or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

assertions.mdasync-operations.mdbuffer-operations.mddata-structures.mdencoding-hashing.mdevent-handling.mdindex.mdperformance-monitoring.mdutility-functions.md
tile.json

encoding-hashing.mddocs/

Encoding & Hashing

Base64 encoding utilities and file hashing functions with platform-specific optimizations for both Node.js and Browser environments.

Capabilities

Base64 Encoding

Utilities for converting between UTF-8 and Base64 encodings.

/**
 * Converts base64-encoded string to UTF-8 string
 * @param str - Base64 encoded string
 * @returns UTF-8 decoded string
 * @deprecated Moved to @fluidframework-internal/client-utils
 */
function fromBase64ToUtf8(str: string): string;

/**
 * Converts UTF-8 string to base64-encoded string
 * @param str - UTF-8 string to encode
 * @returns Base64 encoded string
 * @deprecated Moved to @fluidframework-internal/client-utils
 */
function fromUtf8ToBase64(str: string): string;

/**
 * Convenience function to convert unknown encoding to UTF-8
 * Avoids buffer copies when no conversion is needed
 * @param str - Input string
 * @param encoding - Source encoding (optional)
 * @returns UTF-8 string
 */
function toUtf8(str: string, encoding?: string): string;

Usage Examples:

import { fromBase64ToUtf8, fromUtf8ToBase64, toUtf8 } from "@fluidframework/common-utils";

// Base64 encoding/decoding
const original = "Hello, World! ๐ŸŒ";
const encoded = fromUtf8ToBase64(original);
console.log(encoded); // "SGVsbG8sIFdvcmxkISDwn42N"

const decoded = fromBase64ToUtf8(encoded);
console.log(decoded === original); // true

// Safe UTF-8 conversion
const utf8String = toUtf8("Hello World"); // No conversion needed
const convertedString = toUtf8(someStringFromApi, "iso-8859-1"); // Converts if needed

File Hashing

Cryptographic hashing functions for binary data with algorithm support.

/**
 * Async hash function for binary data
 * Browser: Uses crypto.subtle (SHA-1/SHA-256), falls back to Node implementation in insecure contexts
 * Node.js: Uses sha.js library for SHA-1/SHA-256
 * @param file - Binary data to hash
 * @param algorithm - Hash algorithm ("SHA-1" or "SHA-256")
 * @returns Promise resolving to hex-encoded hash string
 */
function hashFile(file: ArrayBufferLike, algorithm?: "SHA-1" | "SHA-256"): Promise<string>;

/**
 * Creates GitHub-compatible hash with blob prefix and size
 * Format: hash("blob " + file.byteLength + "\0" + file_contents)
 * @param file - Binary data to hash
 * @returns Promise resolving to GitHub-compatible hash string
 */
function gitHashFile(file: ArrayBufferLike): Promise<string>;

Usage Examples:

import { hashFile, gitHashFile } from "@fluidframework/common-utils";

// Hash file data
const fileData = new Uint8Array([1, 2, 3, 4, 5]);

// Standard SHA-256 hash
const sha256Hash = await hashFile(fileData, "SHA-256");
console.log(sha256Hash); // "74f81fe167d99b4cb41d6d0ccda82278caee9f3e2f25d5e5a3936ff3dcec60d0"

// SHA-1 hash (default)
const sha1Hash = await hashFile(fileData);
console.log(sha1Hash); // "11966ab9c099f8fabefbf4124c098b78e1bd897c"

// GitHub-compatible hash
const gitHash = await gitHashFile(fileData);
console.log(gitHash); // Includes blob prefix: "blob 5\x00..." + data

// Hash text content
const textEncoder = new TextEncoder();
const textData = textEncoder.encode("Hello World");
const textHash = await hashFile(textData, "SHA-256");

Advanced Usage

import { hashFile, gitHashFile, stringToBuffer } from "@fluidframework/common-utils";

// Hash string content
async function hashString(content: string, algorithm: "SHA-1" | "SHA-256" = "SHA-1"): Promise<string> {
  const buffer = stringToBuffer(content, "utf8");
  return hashFile(buffer, algorithm);
}

// Verify file integrity
async function verifyFileHash(fileData: ArrayBufferLike, expectedHash: string): Promise<boolean> {
  const actualHash = await hashFile(fileData, "SHA-256");
  return actualHash === expectedHash;
}

// Create content-addressable storage key
async function createContentKey(data: ArrayBufferLike): Promise<string> {
  const hash = await hashFile(data, "SHA-256");
  return hash.substring(0, 16); // First 16 chars as key
}

// Usage examples
const content = "Important document content";
const hash = await hashString(content, "SHA-256");

const buffer = stringToBuffer(content);
const isValid = await verifyFileHash(buffer, hash);
const storageKey = await createContentKey(buffer);

Platform Differences

Browser Environment

  • Uses Web Crypto API (crypto.subtle) for hashing when available
  • Falls back to Node.js implementation in insecure contexts (HTTP)
  • Supports modern browsers with SubtleCrypto support

Node.js Environment

  • Uses sha.js library for reliable cross-platform hashing
  • Consistent behavior across all Node.js versions
  • No dependency on native crypto modules

Security Considerations

  • Browser: crypto.subtle is only available in secure contexts (HTTPS)
  • Hash Algorithms: SHA-1 is deprecated for cryptographic security but still used for compatibility
  • Git Compatibility: gitHashFile produces hashes compatible with Git's object storage format

Both environments provide identical APIs and produce the same hash outputs for the same inputs.