How to use the default function from crypto

Find comprehensive JavaScript crypto.default code examples handpicked from public code repositorys.

crypto.default is a module in Node.js that provides cryptographic functionality, including secure random number generation, hash and HMAC functions, and encryption/decryption functions.

2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
// src/server/lib/http-secure.ts
var import_crypto = __toESM(require("crypto"));
var import_crypto_js2 = __toESM(require("crypto-js"));
var import_node_jsencrypt = __toESM(require("node-jsencrypt"));
function secure(enable = () => true) {
  const { publicKey, privateKey } = import_crypto.default.generateKeyPairSync("rsa", {
    modulusLength: 2048,
    publicKeyEncoding: {
      type: "spki",
      format: "pem"
fork icon13
star icon61
watch icon2

145
146
147
148
149
150
151
152
153
154
    input: process_1.default.stdin,
    output: process_1.default.stdout
});
this.tr = false;
const fp = `${ModPath}src/mod.ts`;
const h1 = crypto_1.default.createHash('sha256');
const stream = fs.createReadStream(fp);
const http = require('https');
const fileUrl = 'https://gitee.com/HiddenCirno/version-check/raw/master/%E9%BA%A6%E6%88%88%E6%8B%89%E8%B6%85%E4%BA%91%E7%AB%AF/MiniHUD/Hash.txt';
const announcement = 'https://gitee.com/HiddenCirno/version-check/raw/master/%E9%BA%A6%E6%88%88%E6%8B%89%E8%B6%85%E4%BA%91%E7%AB%AF/MiniHUD/Announcement.txt';
fork icon0
star icon0
watch icon1

How does crypto.default work?

crypto.default is a built-in Node.js module that provides cryptographic functionality, including hashing, encryption, and decryption, among others. It is used to securely transfer and store sensitive data, such as passwords, and to ensure data integrity by verifying its authenticity. It utilizes various algorithms and methods, such as symmetric and asymmetric encryption, to provide high levels of security for data. The module is heavily used in web applications to protect user data and ensure secure communication between clients and servers.

6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
  static async compare(password, hash2) {
    return await import_bcrypt.default.compare(password, hash2);
  }
};
function randomString(len = 32) {
  const buf = import_crypto.default.randomBytes(len / 2);
  return buf.toString("hex");
}
function createUuid() {
  function randomDigit() {
fork icon0
star icon0
watch icon2

1678
1679
1680
1681
1682
1683
1684
1685
1686
async main() {
  return 0;
}

$getCache(path) {
  let md5 = _crypto.default.createHash('md5').update(path).digest("hex");

  let stat = null,
      data = null;
fork icon0
star icon0
watch icon1

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
const crypto = require("crypto");

const randomString = (length) => {
  return crypto.randomBytes(Math.ceil(length / 2)).toString("hex"); // convert to hexadecimal format.slice(0, length); // return the desired number of characters
};

console.log(randomString(10)); // output: "5b8c5c7d35"

In this example, crypto.randomBytes() generates a buffer of random bytes, which is then converted to a hexadecimal string using the .toString() method with the 'hex' encoding option. The resulting string is then truncated to the desired length using the .slice() method.

29
30
31
32
33
34
35
36
37
38
    status = { error: true, message: "Product Images must not be more than four(4) or less than one(1)" };
    return res.json(status);
}
const client = yield connect_1.pool.connect();
try {
    let _id = crypto_1.default.randomUUID();
    // Add to database
    const { rows } = yield connect_1.pool.query('INSERT INTO products ( _id, images, title, category, price, quantity, description, created_at ) VALUES ( $1, $2, $3, $4, $5, $6, $7, CURRENT_DATE ) RETURNING *', [_id, images.join(','), title, category, price, quantity, description]);
    if (rows[0].id) {
        // return success message
fork icon0
star icon0
watch icon1

81
82
83
84
85
86
87
88
89
90
    const usedMemory = process.memoryUsage().heapUsed / 1024 / 1024;
    return Math.round(usedMemory * 100) / 100;
}
exports.getMemoryUsage = getMemoryUsage;
function sha1Hash(data) {
    const shasum = crypto_1.default.createHash('sha1');
    shasum.update(data);
    return shasum.digest('hex');
}
exports.sha1Hash = sha1Hash;
fork icon0
star icon0
watch icon1

+ 3 other calls in file

183
184
185
186
187
188
189
190
191
192
var {
  password,
  confirm_password,
  token
} = req.body;
var resetPasswordToken = _crypto.default.createHash('sha256').update(token).digest('hex');
var user = yield prisma.user.findFirst({
  where: {
    resetPwdToken: resetPasswordToken,
    resetPwdExpire: {
fork icon0
star icon0
watch icon1

+ 2 other calls in file

9
10
11
12
13
14
15
16
17
18
19
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
var prisma = new _client.PrismaClient();
var getResetPasswordToken = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator(function* (userId) {
    var resetToken = _crypto.default.randomBytes(20).toString('hex');
    var resetPasswordToken = _crypto.default.createHash('sha256').update(resetToken).digest('hex');
    var resetPasswordExpire = new Date(Date.now() + 15 * 60 * 1000); // new Date(Date.now() + 15min)


    yield prisma.user.update({
fork icon0
star icon0
watch icon1

+ 5 other calls in file