Created
September 13, 2023 08:25
-
-
Save eiryu/4d04683edfe4667888a8edf32ad66808 to your computer and use it in GitHub Desktop.
IRKitの `POST /wifi` にリクエストする際の文字列を生成するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://hakataminamiwebservice.hatenablog.com/entry/2022/12/13/155403 | |
"use strict"; | |
// Original Source Code: http://jsdo.it/mash/keyserializer-test | |
// MIT License | |
// This Source Code is transpiled from https://github.com/bouzuya/node-irkit/blob/master/src/irkit-device-key-serializer.ts typescript | |
// to javascript, and add a little change. | |
// CRC-8-Dallas/Maxim | |
function crc8n(u8, crcinit) { | |
if (u8 < 0x00 || u8 > 0xFF) { | |
throw new Error('only input less than 1byte as number'); | |
} | |
var crc = crcinit ^ u8; | |
for (var i = 0; i < 8; i++) { | |
var poly = (crc & 0x80) === 0x80; | |
crc = (crc << 1) ^ (poly ? 0x31 : 0x00); // 0x31 = X^8+X^5+X^4+X^0 | |
} | |
return crc & 0xFF; | |
} | |
// tslint:enable | |
function crc8s(data, size, crcinit) { | |
var crc = crcinit; | |
for (var i = 0; i < size; i++) { | |
var u8 = i < data.length ? data.charCodeAt(i) : 0x00; // 0 filled | |
crc = crc8n(u8, crc); | |
} | |
return crc; | |
} | |
function hex(s) { | |
var ret = ''; | |
for (var i = 0; i < s.length; i++) { | |
ret += s.charCodeAt(i).toString(16).toUpperCase(); | |
} | |
return ret; | |
} | |
function serializeSecurity(security) { | |
return security.toString(); | |
} | |
function serializeSSID(ssid) { | |
return hex(ssid); | |
} | |
function serializePassword(password) { | |
return hex(password); | |
} | |
function serializeWEPPassword(password) { | |
return serializePassword(hex(password)); | |
} | |
function serializeDevicekey(devicekey) { | |
return devicekey; | |
} | |
var Security; | |
(function (Security) { | |
Security[Security["NONE"] = 0] = "NONE"; | |
Security[Security["WEP"] = 2] = "WEP"; | |
Security[Security["WPA_WPA2"] = 8] = "WPA_WPA2"; | |
})(Security || (Security = {})); | |
function serializeCRC(obj) { | |
var crc = 0x00; | |
crc = crc8n(obj.security, crc); | |
crc = crc8s(obj.ssid, 33, crc); | |
crc = crc8s(obj.password, 64, crc); | |
crc = crc8n(1, crc); // wifi_is_set | |
crc = crc8n(0, crc); // wifi_was_valid | |
crc = crc8s(obj.devicekey, 33, crc); | |
return crc.toString(16).toUpperCase(); | |
} | |
function serialize(_a) { | |
var devicekey = _a.devicekey, password = _a.password, security = _a.security, ssid = _a.ssid; | |
return [ | |
serializeSecurity(security), | |
serializeSSID(ssid), | |
security === Security.WEP | |
? serializeWEPPassword(password) | |
: serializePassword(password), | |
serializeDevicekey(devicekey), | |
'2', | |
'', | |
'', | |
'', | |
'', | |
'', | |
serializeCRC({ | |
devicekey: devicekey, | |
password: password, | |
security: security, | |
ssid: ssid | |
}) | |
].join('/'); | |
} | |
// ここに自分のwifi 設定を入力する | |
let obj = { | |
devicekey: "", // ブランクでOK | |
password: "", // home wifi passward (not IRKit wifi password) | |
security: 8, // WPA_WPA2:8, WEP: 2 | |
ssid: "" // wifi ssid (not IRKit wifi ssid) | |
} | |
// | |
let serializedObj = serialize(obj); | |
console.log({ serializedObj }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment