diff options
| author | Li Zhineng <[email protected]> | 2025-07-19 15:35:02 +0800 |
|---|---|---|
| committer | Li Zhineng <[email protected]> | 2025-07-19 15:35:02 +0800 |
| commit | e6279ae486eef2d549a4b5dd15e4e446d2180dc7 (patch) | |
| tree | b25897278de3685d79df0a0381ceba9ca23d1ef9 /src/util.ts | |
| parent | 3cbee218d16e2c7c86d3c1433fde100246da0332 (diff) | |
| download | airmx-e6279ae486eef2d549a4b5dd15e4e446d2180dc7.tar.gz airmx-e6279ae486eef2d549a4b5dd15e4e446d2180dc7.zip | |
give up monorepo
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') + } +} |
