generate_random_bytes
Create cryptographically secure random bytes for encryption or data security. Specify length and encoding (hex, base64, binary) for tailored output. Ideal for secure key generation or sensitive data handling.
Instructions
Generate cryptographically secure random bytes
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| encoding | No | Output encoding format | hex |
| length | No | Number of random bytes to generate |
Implementation Reference
- src/index.ts:428-466 (handler)The core handler function that generates cryptographically secure random bytes using Node.js crypto.randomBytes. Supports length (1-1024 bytes, default 32) and encoding (hex, base64, binary). Returns JSON with the result.private async generateRandomBytes(args: any) { const length = args.length ?? 32; const encoding = args.encoding ?? "hex"; if (length < 1 || length > 1024) { throw new Error("Length must be between 1 and 1024"); } const bytes = randomBytes(length); let result: string; switch (encoding) { case "hex": result = bytes.toString("hex"); break; case "base64": result = bytes.toString("base64"); break; case "binary": result = bytes.toString("binary"); break; default: throw new Error("Invalid encoding. Must be 'hex', 'base64', or 'binary'"); } return { content: [ { type: "text", text: JSON.stringify({ type: "random_bytes", value: result, parameters: { length, encoding }, timestamp: new Date().toISOString(), }, null, 2), }, ], }; }
- src/index.ts:113-131 (schema)Input schema for the generate_random_bytes tool, defining parameters: length (integer, 1-1024, default 32) and encoding (string enum: hex/base64/binary, default hex).inputSchema: { type: "object", properties: { length: { type: "integer", description: "Number of random bytes to generate", default: 32, minimum: 1, maximum: 1024, }, encoding: { type: "string", description: "Output encoding format", enum: ["hex", "base64", "binary"], default: "hex", }, }, required: [], },
- src/index.ts:110-132 (registration)Tool registration in the ListTools response, including name, description, and reference to inputSchema.{ name: "generate_random_bytes", description: "Generate cryptographically secure random bytes", inputSchema: { type: "object", properties: { length: { type: "integer", description: "Number of random bytes to generate", default: 32, minimum: 1, maximum: 1024, }, encoding: { type: "string", description: "Output encoding format", enum: ["hex", "base64", "binary"], default: "hex", }, }, required: [], }, },
- src/index.ts:253-254 (registration)Dispatch case in the CallToolRequestSchema handler that routes calls to the generateRandomBytes method.case "generate_random_bytes": return await this.generateRandomBytes(args);