diff options
Diffstat (limited to 'src/util.ts')
| -rw-r--r-- | src/util.ts | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 0000000..b5cbd4b --- /dev/null +++ b/src/util.ts @@ -0,0 +1,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') + } +} |
