summaryrefslogtreecommitdiff
path: root/src/util.ts
diff options
context:
space:
mode:
authorLi Zhineng <[email protected]>2025-07-19 15:35:02 +0800
committerLi Zhineng <[email protected]>2025-07-19 15:35:02 +0800
commite6279ae486eef2d549a4b5dd15e4e446d2180dc7 (patch)
treeb25897278de3685d79df0a0381ceba9ca23d1ef9 /src/util.ts
parent3cbee218d16e2c7c86d3c1433fde100246da0332 (diff)
downloadairmx-e6279ae486eef2d549a4b5dd15e4e446d2180dc7.tar.gz
airmx-e6279ae486eef2d549a4b5dd15e4e446d2180dc7.zip
give up monorepo
Diffstat (limited to 'src/util.ts')
-rw-r--r--src/util.ts43
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')
+ }
+}