16
16
17
17
import { createCipheriv , createDecipheriv , getCiphers } from "node:crypto" ;
18
18
import { McosEncryptionPair } from "rusty-motors-shared" ;
19
- import { ServerError } from "rusty-motors-shared" ;
20
19
21
20
/**
22
21
* This function creates a new encryption pair for use with the game server
@@ -25,27 +24,26 @@ import { ServerError } from "rusty-motors-shared";
25
24
* @returns {McosEncryptionPair } The encryption pair
26
25
*/
27
26
export function createCommandEncryptionPair ( key : string ) : McosEncryptionPair {
28
- if ( key . length < 16 ) {
29
- const err = new ServerError ( "Key too short" ) ;
30
- throw err ;
31
- }
27
+ if ( key . length < 16 ) {
28
+ throw Error ( "Key too short" ) ;
29
+ }
32
30
33
- const sKey = key . slice ( 0 , 16 ) ;
31
+ const sKey = key . slice ( 0 , 16 ) ;
34
32
35
- // Deepcode ignore HardcodedSecret: This uses an empty IV
36
- const desIV = Buffer . alloc ( 8 ) ;
33
+ // Deepcode ignore HardcodedSecret: This uses an empty IV
34
+ const desIV = Buffer . alloc ( 8 ) ;
37
35
38
- const gsCipher = createCipheriv ( "des-cbc" , Buffer . from ( sKey , "hex" ) , desIV ) ;
39
- gsCipher . setAutoPadding ( false ) ;
36
+ const gsCipher = createCipheriv ( "des-cbc" , Buffer . from ( sKey , "hex" ) , desIV ) ;
37
+ gsCipher . setAutoPadding ( false ) ;
40
38
41
- const gsDecipher = createDecipheriv (
42
- "des-cbc" ,
43
- Buffer . from ( sKey , "hex" ) ,
44
- desIV ,
45
- ) ;
46
- gsDecipher . setAutoPadding ( false ) ;
39
+ const gsDecipher = createDecipheriv (
40
+ "des-cbc" ,
41
+ Buffer . from ( sKey , "hex" ) ,
42
+ desIV ,
43
+ ) ;
44
+ gsDecipher . setAutoPadding ( false ) ;
47
45
48
- return new McosEncryptionPair ( gsCipher , gsDecipher ) ;
46
+ return new McosEncryptionPair ( gsCipher , gsDecipher ) ;
49
47
}
50
48
51
49
/**
@@ -56,18 +54,17 @@ export function createCommandEncryptionPair(key: string): McosEncryptionPair {
56
54
* @throws Error if the key is too short
57
55
*/
58
56
export function createDataEncryptionPair ( key : string ) : McosEncryptionPair {
59
- if ( key . length < 16 ) {
60
- const err = new ServerError ( "Key too short" ) ;
61
- throw err ;
62
- }
57
+ if ( key . length < 16 ) {
58
+ throw Error ( "Key too short" ) ;
59
+ }
63
60
64
- const stringKey = Buffer . from ( key , "hex" ) ;
61
+ const stringKey = Buffer . from ( key , "hex" ) ;
65
62
66
- // File deepcode ignore InsecureCipher: RC4 is the encryption algorithum used here, file deepcode ignore HardcodedSecret: A blank IV is used here
67
- const tsCipher = createCipheriv ( "rc4" , stringKey . subarray ( 0 , 16 ) , "" ) ;
68
- const tsDecipher = createDecipheriv ( "rc4" , stringKey . subarray ( 0 , 16 ) , "" ) ;
63
+ // File deepcode ignore InsecureCipher: RC4 is the encryption algorithum used here, file deepcode ignore HardcodedSecret: A blank IV is used here
64
+ const tsCipher = createCipheriv ( "rc4" , stringKey . subarray ( 0 , 16 ) , "" ) ;
65
+ const tsDecipher = createDecipheriv ( "rc4" , stringKey . subarray ( 0 , 16 ) , "" ) ;
69
66
70
- return new McosEncryptionPair ( tsCipher , tsDecipher ) ;
67
+ return new McosEncryptionPair ( tsCipher , tsDecipher ) ;
71
68
}
72
69
73
70
/**
@@ -77,13 +74,8 @@ export function createDataEncryptionPair(key: string): McosEncryptionPair {
77
74
* @throws Error if the server does not support the legacy ciphers
78
75
*/
79
76
export function verifyLegacyCipherSupport ( ) {
80
- const cipherList = getCiphers ( ) ;
81
- if ( ! cipherList . includes ( "des-cbc" ) ) {
82
- const err = new ServerError ( "DES-CBC cipher not available" ) ;
83
- throw err ;
84
- }
85
- if ( ! cipherList . includes ( "rc4" ) ) {
86
- const err = new ServerError ( "RC4 cipher not available" ) ;
87
- throw err ;
88
- }
77
+ const cipherList = getCiphers ( ) ;
78
+ if ( ! cipherList . includes ( "des-cbc" ) || ! cipherList . includes ( "rc4" ) ) {
79
+ throw Error ( "Legacy ciphers not available" ) ;
80
+ }
89
81
}
0 commit comments