Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit 8cff9cf

Browse files
author
Jan Krems
committed
style: Use simpler key thingy
1 parent 534e1e4 commit 8cff9cf

1 file changed

Lines changed: 2 additions & 47 deletions

File tree

lib/node-inspect.js

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'use strict'; /* eslint no-underscore-dangle: 0 */
2323
// Our equivalent of '_debugger' in node itthis
2424
const assert = require('assert');
25-
// const crypto = require('crypto');
25+
const crypto = require('crypto');
2626
const events = require('events');
2727
const http = require('http');
2828
const Repl = require('repl');
@@ -59,51 +59,6 @@ exports.port = process.debugPort;
5959

6060
function ignoreError() {}
6161

62-
// Generate a Sec-WebSocket-* value
63-
function createSecretKey() {
64-
// How many spaces will we be inserting?
65-
var numSpaces = 1 + Math.floor(Math.random() * 12);
66-
assert.ok(1 <= numSpaces && numSpaces <= 12);
67-
68-
// What is the numerical value of our key?
69-
var keyVal = (Math.floor(
70-
Math.random() * (4294967295 / numSpaces)
71-
) * numSpaces);
72-
73-
// Our string starts with a string representation of our key
74-
var s = keyVal.toString();
75-
76-
// Insert 'numChars' worth of noise in the character ranges
77-
// [0x21, 0x2f] (14 characters) and [0x3a, 0x7e] (68 characters)
78-
var numChars = 1 + Math.floor(Math.random() * 12);
79-
assert.ok(1 <= numChars && numChars <= 12);
80-
81-
for (var i = 0; i < numChars; i++) {
82-
var pos = Math.floor(Math.random() * s.length + 1);
83-
84-
var c = Math.floor(Math.random() * (14 + 68));
85-
c = (c <= 14) ?
86-
String.fromCharCode(c + 0x21) :
87-
String.fromCharCode((c - 14) + 0x3a);
88-
89-
s = s.substring(0, pos) + c + s.substring(pos, s.length);
90-
}
91-
92-
// We shoudln't have any spaces in our value until we insert them
93-
assert.equal(s.indexOf(' '), -1);
94-
95-
// Insert 'numSpaces' worth of spaces
96-
for (var i = 0; i < numSpaces; i++) {
97-
var pos = Math.floor(Math.random() * (s.length - 1)) + 1;
98-
s = s.substring(0, pos) + ' ' + s.substring(pos, s.length);
99-
}
100-
101-
assert.notEqual(s.charAt(0), ' ');
102-
assert.notEqual(s.charAt(s.length), ' ');
103-
104-
return s;
105-
}
106-
10762
function encodeFrameHybi17(payload) {
10863
const dataLength = payload.length;
10964

@@ -295,7 +250,7 @@ class Client extends events.EventEmitter {
295250
}
296251

297252
connect(port, host) {
298-
const key1 = createSecretKey();
253+
const key1 = crypto.randomBytes(16).toString('base64');
299254

300255
const httpReq = this._http = http.request({
301256
host, port,

0 commit comments

Comments
 (0)