blob: b5cbd4bba7234a3ac42b5dc4ad6198a3dbe90516 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
import crypto from 'crypto'
import type { CommandMessage } from './messages.js'
export class Signer {
/**
* Calculate the signature for the message.
*
* @param message - The command message.
* @param key - The device key.
* @returns An 8-byte signature for the given message.
*/
sign(message: CommandMessage<unknown>, key: string) {
const plainText = JSON.stringify(message.payload())
return this.#hash(plainText.slice(1, -1), key)
}
/**
* Calculate the signature for the plain text.
*
* @param message - The plain text.
* @param key - The device key.
* @returns An 8-byte signature for the given text.
*/
signText(message: string, key: string) {
return this.#hash(message, key)
}
/**
* Hash the data with the MD5 algorithm.
*
* @param data - The plain text.
* @param key - The device key.
* @returns An 8-byte signature for the given data.
*/
#hash(data: string, key: string) {
return crypto
.createHash('md5')
.update(data)
.update(',')
.update(key)
.digest('hex')
}
}
|