-
-
Notifications
You must be signed in to change notification settings - Fork 35
crypto.DEFAULT_ENCODING #407
Copy link
Copy link
Open
Labels
good first issueGood for newcomersGood for newcomershelp wantedExtra attention is neededExtra attention is neededlet's do it
Description
Description
Since crypto.DEFAULT_ENCODING is deprecated (DEP0091) and has reached End-of-Life status in Node.js v20.0.0, we should provide a codemod to replace it.
- The codemod should remove references to
crypto.DEFAULT_ENCODING. - The codemod should replace usage with explicit encoding parameters where needed.
- The codemod should identify where the default encoding was being read or set.
- The codemod should add comments suggesting explicit encoding usage.
Additional Information
Note that crypto.DEFAULT_ENCODING was removed in Node.js v20.0.0. This property only existed for compatibility with Node.js releases prior to version 0.9.3. The property allowed changing the default encoding for crypto operations, but this practice led to unpredictable behavior.
Instead of relying on a default encoding, developers should explicitly specify the encoding in each crypto operation. This makes the code more explicit and easier to understand.
Examples
Example 1: Reading DEFAULT_ENCODING
Before:
const crypto = require("node:crypto");
const encoding = crypto.DEFAULT_ENCODING;After:
// crypto.DEFAULT_ENCODING has been removed
// Use explicit encoding in crypto operations instead
const encoding = "binary"; // or your preferred default encodingExample 2: Setting DEFAULT_ENCODING
Before:
const crypto = require("node:crypto");
crypto.DEFAULT_ENCODING = "hex";After:
// crypto.DEFAULT_ENCODING has been removed
// Always specify encoding explicitly in crypto operations
// For example: hash.digest("hex")Example 3: Using DEFAULT_ENCODING in hash operations
Before:
const crypto = require("node:crypto");
crypto.DEFAULT_ENCODING = "hex";
const hash = crypto.createHash("sha256");
hash.update("some data");
const result = hash.digest(); // Uses DEFAULT_ENCODINGAfter:
const crypto = require("node:crypto");
const hash = crypto.createHash("sha256");
hash.update("some data");
const result = hash.digest("hex"); // Explicitly specify encodingExample 4: Conditional encoding based on DEFAULT_ENCODING
Before:
const crypto = require("node:crypto");
const encoding = crypto.DEFAULT_ENCODING || "binary";
const hash = crypto.createHash("md5").update("data").digest(encoding);After:
const crypto = require("node:crypto");
const encoding = "binary"; // Specify your preferred default
const hash = crypto.createHash("md5").update("data").digest(encoding);Example 5: ESM import with DEFAULT_ENCODING
Before:
import crypto from "node:crypto";
if (crypto.DEFAULT_ENCODING === "hex") {
// do something
}After:
// crypto.DEFAULT_ENCODING has been removed
// Use explicit encoding parameters in crypto operations
// Consider using a constant for your application's default encoding
const DEFAULT_ENCODING = "hex";
if (DEFAULT_ENCODING === "hex") {
// do something
}Refs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
good first issueGood for newcomersGood for newcomershelp wantedExtra attention is neededExtra attention is neededlet's do it
Type
Projects
Status
🔖 Todo