diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/.gitignore b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/.gitignore deleted file mode 100644 index 5225be2..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -node_modules/ -miniprogram_npm/ diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/README.md b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/README.md deleted file mode 100644 index ea42453..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# ESP Config WeChat -A WeChat application for provisioning ESP smart devices. - -## Implemented modules -[BluFi](https://github.com/espressif/esp-idf/tree/master/examples/bluetooth/blufi) diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/app.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/app.js deleted file mode 100644 index 4655336..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/app.js +++ /dev/null @@ -1,22 +0,0 @@ -//app.js -App({ - data: { - service_uuid: "0000FFFF-0000-1000-8000-00805F9B34FB", - characteristic_write_uuid: "0000FF01-0000-1000-8000-00805F9B34FB", - characteristic_read_uuid: "0000FF02-0000-1000-8000-00805F9B34FB", - name: "BLUFI", - mtu: 19, - constMtu: 128, - md5Key: "", - platform: '', - sequenceControl: -1, - sequenceNumber: -1 - }, - - onLaunch: function () { - - }, - globalData: { - userInfo: null - } -}) \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/app.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/app.json deleted file mode 100644 index fc7cc3c..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/app.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "pages": [ - "pages/index/index", - "pages/blueDevices/blueDevices", - "pages/blueWifi/blueWifi", - "pages/blueConnect/blueConnect" - ], - "window": { - "backgroundTextStyle": "light", - "navigationBarBackgroundColor": "#4d9efb", - "navigationBarTitleText": "WeChat", - "navigationBarTextStyle": "white" - }, - "sitemapLocation": "sitemap.json" -} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/app.wxss b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/app.wxss deleted file mode 100644 index 1b8be7d..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/app.wxss +++ /dev/null @@ -1,119 +0,0 @@ -/**app.wxss**/ -.container { - height: 100%; - display: flex; - padding: 0 10px; - flex-direction: column; - align-items: center; - justify-content: space-between; - box-sizing: border-box; -} -.item { - display: flex; - height: 130rpx; - width: 100%; - align-items: center; - border-bottom: 1px solid #eee; -} -.item-img { - flex: 0 0 100rpx; - height: 100rpx; - display: flex; - justify-content: center; - align-items: center; - border-radius: 100%; -} -.bluetooth-img { - height: 80rpx; - width: 80rpx; -} -.item-name { - flex: 1; - padding-left: 10px; - display: flex; - flex-direction: column; - font-size: 32rpx; -} -.text-rssi { - font-size: 28rpx; - color: #999; -} -.btn { - width: 100%; - height: 90rpx; - line-height: 90rpx; - background: #4d9efb; - border: none; - margin-bottom: 30rpx; - font-size: 14px; - color: #fff; -} -.font14 { - font-size: 14px; -} -.font16 { - font-size: 16px; -} -.textcenter { - text-align: center; -} -.width100 { - width: 100%; -} -.margintop20 { - margin-top: 20px; -} -.margintop10 { - margin-top: 10px; -} -.modal-mask{ - width: 100%; - height: 100%; - position: fixed; - top: 0; - left: 0; - background: rgba(0,0,0,.7); - overflow: hidden; - color: #ffffff; - z-index:900; -} -.modal-dialog{ - background-color: #f9f9f9; - /*background: rgba(0,0,0,.7);*/ - opacity: 0.95; - position: fixed; - z-index: 999; - top: 20%; - left: 10%; - overflow: hidden; - width: 80%; - color: #000; - border-radius: 26rpx; -} -.modal-dialog input{ - margin: 15px 0; - padding: 0 20px; - font-size: 32rpx; -} -.modal-btn-wrapper{ - display: flex; - flex-direction: row; - height: 100rpx; - line-height:90rpx; - border-top: 2rpx solid rgba(7,17,27,0.1); -} -.cancel-btn, .confirm-btn{ - flex: 1; - height: 100rpx; - line-height: 100rpx; - text-align: center; - font-size: 32rpx; -} -.cancel-btn{ - border-right: 2rpx solid rgba(7,17,27,0.1); -} -.modal-title{ - text-align: center; - padding-top: 10px; -} - diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/aes.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/aes.js deleted file mode 100644 index ec49bc5..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/aes.js +++ /dev/null @@ -1,802 +0,0 @@ -(function(root) { - "use strict"; - - function checkInt(value) { - return (parseInt(value) === value); - } - - function checkInts(arrayish) { - if (!checkInt(arrayish.length)) { return false; } - - for (var i = 0; i < arrayish.length; i++) { - if (!checkInt(arrayish[i]) || arrayish[i] < 0 || arrayish[i] > 255) { - return false; - } - } - - return true; - } - - function coerceArray(arg, copy) { - - // ArrayBuffer view - if (arg.buffer && ArrayBuffer.isView(arg) && arg.name === 'Uint8Array') { - - if (copy) { - if (arg.slice) { - arg = arg.slice(); - } else { - arg = Array.prototype.slice.call(arg); - } - } - - return arg; - } - - // It's an array; check it is a valid representation of a byte - if (Array.isArray(arg)) { - if (!checkInts(arg)) { - throw new Error('Array contains invalid value: ' + arg); - } - - return new Uint8Array(arg); - } - - // Something else, but behaves like an array (maybe a Buffer? Arguments?) - if (checkInt(arg.length) && checkInts(arg)) { - return new Uint8Array(arg); - } - - throw new Error('unsupported array-like object'); - } - - function createArray(length) { - return new Uint8Array(length); - } - - function copyArray(sourceArray, targetArray, targetStart, sourceStart, sourceEnd) { - if (sourceStart != null || sourceEnd != null) { - if (sourceArray.slice) { - sourceArray = sourceArray.slice(sourceStart, sourceEnd); - } else { - sourceArray = Array.prototype.slice.call(sourceArray, sourceStart, sourceEnd); - } - } - targetArray.set(sourceArray, targetStart); - } - - - - var convertUtf8 = (function() { - function toBytes(text) { - var result = [], i = 0; - text = encodeURI(text); - while (i < text.length) { - var c = text.charCodeAt(i++); - - // if it is a % sign, encode the following 2 bytes as a hex value - if (c === 37) { - result.push(parseInt(text.substr(i, 2), 16)) - i += 2; - - // otherwise, just the actual byte - } else { - result.push(c) - } - } - - return coerceArray(result); - } - - function fromBytes(bytes) { - var result = [], i = 0; - - while (i < bytes.length) { - var c = bytes[i]; - - if (c < 128) { - result.push(String.fromCharCode(c)); - i++; - } else if (c > 191 && c < 224) { - result.push(String.fromCharCode(((c & 0x1f) << 6) | (bytes[i + 1] & 0x3f))); - i += 2; - } else { - result.push(String.fromCharCode(((c & 0x0f) << 12) | ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f))); - i += 3; - } - } - - return result.join(''); - } - - return { - toBytes: toBytes, - fromBytes: fromBytes, - } - })(); - - var convertHex = (function() { - function toBytes(text) { - var result = []; - for (var i = 0; i < text.length; i += 2) { - result.push(parseInt(text.substr(i, 2), 16)); - } - - return result; - } - - // http://ixti.net/development/javascript/2011/11/11/base64-encodedecode-of-utf8-in-browser-with-js.html - var Hex = '0123456789abcdef'; - - function fromBytes(bytes) { - var result = []; - for (var i = 0; i < bytes.length; i++) { - var v = bytes[i]; - result.push(Hex[(v & 0xf0) >> 4] + Hex[v & 0x0f]); - } - return result.join(''); - } - - return { - toBytes: toBytes, - fromBytes: fromBytes, - } - })(); - - - // Number of rounds by keysize - var numberOfRounds = {16: 10, 24: 12, 32: 14} - - // Round constant words - var rcon = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91]; - - // S-box and Inverse S-box (S is for Substitution) - var S = [0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16]; - var Si =[0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d]; - - // Transformations for encryption - var T1 = [0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554, 0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a, 0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87, 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b, 0x41adadec, 0xb3d4d467, 0x5fa2a2fd, 0x45afafea, 0x239c9cbf, 0x53a4a4f7, 0xe4727296, 0x9bc0c05b, 0x75b7b7c2, 0xe1fdfd1c, 0x3d9393ae, 0x4c26266a, 0x6c36365a, 0x7e3f3f41, 0xf5f7f702, 0x83cccc4f, 0x6834345c, 0x51a5a5f4, 0xd1e5e534, 0xf9f1f108, 0xe2717193, 0xabd8d873, 0x62313153, 0x2a15153f, 0x0804040c, 0x95c7c752, 0x46232365, 0x9dc3c35e, 0x30181828, 0x379696a1, 0x0a05050f, 0x2f9a9ab5, 0x0e070709, 0x24121236, 0x1b80809b, 0xdfe2e23d, 0xcdebeb26, 0x4e272769, 0x7fb2b2cd, 0xea75759f, 0x1209091b, 0x1d83839e, 0x582c2c74, 0x341a1a2e, 0x361b1b2d, 0xdc6e6eb2, 0xb45a5aee, 0x5ba0a0fb, 0xa45252f6, 0x763b3b4d, 0xb7d6d661, 0x7db3b3ce, 0x5229297b, 0xdde3e33e, 0x5e2f2f71, 0x13848497, 0xa65353f5, 0xb9d1d168, 0x00000000, 0xc1eded2c, 0x40202060, 0xe3fcfc1f, 0x79b1b1c8, 0xb65b5bed, 0xd46a6abe, 0x8dcbcb46, 0x67bebed9, 0x7239394b, 0x944a4ade, 0x984c4cd4, 0xb05858e8, 0x85cfcf4a, 0xbbd0d06b, 0xc5efef2a, 0x4faaaae5, 0xedfbfb16, 0x864343c5, 0x9a4d4dd7, 0x66333355, 0x11858594, 0x8a4545cf, 0xe9f9f910, 0x04020206, 0xfe7f7f81, 0xa05050f0, 0x783c3c44, 0x259f9fba, 0x4ba8a8e3, 0xa25151f3, 0x5da3a3fe, 0x804040c0, 0x058f8f8a, 0x3f9292ad, 0x219d9dbc, 0x70383848, 0xf1f5f504, 0x63bcbcdf, 0x77b6b6c1, 0xafdada75, 0x42212163, 0x20101030, 0xe5ffff1a, 0xfdf3f30e, 0xbfd2d26d, 0x81cdcd4c, 0x180c0c14, 0x26131335, 0xc3ecec2f, 0xbe5f5fe1, 0x359797a2, 0x884444cc, 0x2e171739, 0x93c4c457, 0x55a7a7f2, 0xfc7e7e82, 0x7a3d3d47, 0xc86464ac, 0xba5d5de7, 0x3219192b, 0xe6737395, 0xc06060a0, 0x19818198, 0x9e4f4fd1, 0xa3dcdc7f, 0x44222266, 0x542a2a7e, 0x3b9090ab, 0x0b888883, 0x8c4646ca, 0xc7eeee29, 0x6bb8b8d3, 0x2814143c, 0xa7dede79, 0xbc5e5ee2, 0x160b0b1d, 0xaddbdb76, 0xdbe0e03b, 0x64323256, 0x743a3a4e, 0x140a0a1e, 0x924949db, 0x0c06060a, 0x4824246c, 0xb85c5ce4, 0x9fc2c25d, 0xbdd3d36e, 0x43acacef, 0xc46262a6, 0x399191a8, 0x319595a4, 0xd3e4e437, 0xf279798b, 0xd5e7e732, 0x8bc8c843, 0x6e373759, 0xda6d6db7, 0x018d8d8c, 0xb1d5d564, 0x9c4e4ed2, 0x49a9a9e0, 0xd86c6cb4, 0xac5656fa, 0xf3f4f407, 0xcfeaea25, 0xca6565af, 0xf47a7a8e, 0x47aeaee9, 0x10080818, 0x6fbabad5, 0xf0787888, 0x4a25256f, 0x5c2e2e72, 0x381c1c24, 0x57a6a6f1, 0x73b4b4c7, 0x97c6c651, 0xcbe8e823, 0xa1dddd7c, 0xe874749c, 0x3e1f1f21, 0x964b4bdd, 0x61bdbddc, 0x0d8b8b86, 0x0f8a8a85, 0xe0707090, 0x7c3e3e42, 0x71b5b5c4, 0xcc6666aa, 0x904848d8, 0x06030305, 0xf7f6f601, 0x1c0e0e12, 0xc26161a3, 0x6a35355f, 0xae5757f9, 0x69b9b9d0, 0x17868691, 0x99c1c158, 0x3a1d1d27, 0x279e9eb9, 0xd9e1e138, 0xebf8f813, 0x2b9898b3, 0x22111133, 0xd26969bb, 0xa9d9d970, 0x078e8e89, 0x339494a7, 0x2d9b9bb6, 0x3c1e1e22, 0x15878792, 0xc9e9e920, 0x87cece49, 0xaa5555ff, 0x50282878, 0xa5dfdf7a, 0x038c8c8f, 0x59a1a1f8, 0x09898980, 0x1a0d0d17, 0x65bfbfda, 0xd7e6e631, 0x844242c6, 0xd06868b8, 0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a]; - var T2 = [0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b, 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5, 0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b, 0x19e7fefe, 0x62b5d7d7, 0xe64dabab, 0x9aec7676, 0x458fcaca, 0x9d1f8282, 0x4089c9c9, 0x87fa7d7d, 0x15effafa, 0xebb25959, 0xc98e4747, 0x0bfbf0f0, 0xec41adad, 0x67b3d4d4, 0xfd5fa2a2, 0xea45afaf, 0xbf239c9c, 0xf753a4a4, 0x96e47272, 0x5b9bc0c0, 0xc275b7b7, 0x1ce1fdfd, 0xae3d9393, 0x6a4c2626, 0x5a6c3636, 0x417e3f3f, 0x02f5f7f7, 0x4f83cccc, 0x5c683434, 0xf451a5a5, 0x34d1e5e5, 0x08f9f1f1, 0x93e27171, 0x73abd8d8, 0x53623131, 0x3f2a1515, 0x0c080404, 0x5295c7c7, 0x65462323, 0x5e9dc3c3, 0x28301818, 0xa1379696, 0x0f0a0505, 0xb52f9a9a, 0x090e0707, 0x36241212, 0x9b1b8080, 0x3ddfe2e2, 0x26cdebeb, 0x694e2727, 0xcd7fb2b2, 0x9fea7575, 0x1b120909, 0x9e1d8383, 0x74582c2c, 0x2e341a1a, 0x2d361b1b, 0xb2dc6e6e, 0xeeb45a5a, 0xfb5ba0a0, 0xf6a45252, 0x4d763b3b, 0x61b7d6d6, 0xce7db3b3, 0x7b522929, 0x3edde3e3, 0x715e2f2f, 0x97138484, 0xf5a65353, 0x68b9d1d1, 0x00000000, 0x2cc1eded, 0x60402020, 0x1fe3fcfc, 0xc879b1b1, 0xedb65b5b, 0xbed46a6a, 0x468dcbcb, 0xd967bebe, 0x4b723939, 0xde944a4a, 0xd4984c4c, 0xe8b05858, 0x4a85cfcf, 0x6bbbd0d0, 0x2ac5efef, 0xe54faaaa, 0x16edfbfb, 0xc5864343, 0xd79a4d4d, 0x55663333, 0x94118585, 0xcf8a4545, 0x10e9f9f9, 0x06040202, 0x81fe7f7f, 0xf0a05050, 0x44783c3c, 0xba259f9f, 0xe34ba8a8, 0xf3a25151, 0xfe5da3a3, 0xc0804040, 0x8a058f8f, 0xad3f9292, 0xbc219d9d, 0x48703838, 0x04f1f5f5, 0xdf63bcbc, 0xc177b6b6, 0x75afdada, 0x63422121, 0x30201010, 0x1ae5ffff, 0x0efdf3f3, 0x6dbfd2d2, 0x4c81cdcd, 0x14180c0c, 0x35261313, 0x2fc3ecec, 0xe1be5f5f, 0xa2359797, 0xcc884444, 0x392e1717, 0x5793c4c4, 0xf255a7a7, 0x82fc7e7e, 0x477a3d3d, 0xacc86464, 0xe7ba5d5d, 0x2b321919, 0x95e67373, 0xa0c06060, 0x98198181, 0xd19e4f4f, 0x7fa3dcdc, 0x66442222, 0x7e542a2a, 0xab3b9090, 0x830b8888, 0xca8c4646, 0x29c7eeee, 0xd36bb8b8, 0x3c281414, 0x79a7dede, 0xe2bc5e5e, 0x1d160b0b, 0x76addbdb, 0x3bdbe0e0, 0x56643232, 0x4e743a3a, 0x1e140a0a, 0xdb924949, 0x0a0c0606, 0x6c482424, 0xe4b85c5c, 0x5d9fc2c2, 0x6ebdd3d3, 0xef43acac, 0xa6c46262, 0xa8399191, 0xa4319595, 0x37d3e4e4, 0x8bf27979, 0x32d5e7e7, 0x438bc8c8, 0x596e3737, 0xb7da6d6d, 0x8c018d8d, 0x64b1d5d5, 0xd29c4e4e, 0xe049a9a9, 0xb4d86c6c, 0xfaac5656, 0x07f3f4f4, 0x25cfeaea, 0xafca6565, 0x8ef47a7a, 0xe947aeae, 0x18100808, 0xd56fbaba, 0x88f07878, 0x6f4a2525, 0x725c2e2e, 0x24381c1c, 0xf157a6a6, 0xc773b4b4, 0x5197c6c6, 0x23cbe8e8, 0x7ca1dddd, 0x9ce87474, 0x213e1f1f, 0xdd964b4b, 0xdc61bdbd, 0x860d8b8b, 0x850f8a8a, 0x90e07070, 0x427c3e3e, 0xc471b5b5, 0xaacc6666, 0xd8904848, 0x05060303, 0x01f7f6f6, 0x121c0e0e, 0xa3c26161, 0x5f6a3535, 0xf9ae5757, 0xd069b9b9, 0x91178686, 0x5899c1c1, 0x273a1d1d, 0xb9279e9e, 0x38d9e1e1, 0x13ebf8f8, 0xb32b9898, 0x33221111, 0xbbd26969, 0x70a9d9d9, 0x89078e8e, 0xa7339494, 0xb62d9b9b, 0x223c1e1e, 0x92158787, 0x20c9e9e9, 0x4987cece, 0xffaa5555, 0x78502828, 0x7aa5dfdf, 0x8f038c8c, 0xf859a1a1, 0x80098989, 0x171a0d0d, 0xda65bfbf, 0x31d7e6e6, 0xc6844242, 0xb8d06868, 0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f, 0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616]; - var T3 = [0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b, 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5, 0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b, 0xfe19e7fe, 0xd762b5d7, 0xabe64dab, 0x769aec76, 0xca458fca, 0x829d1f82, 0xc94089c9, 0x7d87fa7d, 0xfa15effa, 0x59ebb259, 0x47c98e47, 0xf00bfbf0, 0xadec41ad, 0xd467b3d4, 0xa2fd5fa2, 0xafea45af, 0x9cbf239c, 0xa4f753a4, 0x7296e472, 0xc05b9bc0, 0xb7c275b7, 0xfd1ce1fd, 0x93ae3d93, 0x266a4c26, 0x365a6c36, 0x3f417e3f, 0xf702f5f7, 0xcc4f83cc, 0x345c6834, 0xa5f451a5, 0xe534d1e5, 0xf108f9f1, 0x7193e271, 0xd873abd8, 0x31536231, 0x153f2a15, 0x040c0804, 0xc75295c7, 0x23654623, 0xc35e9dc3, 0x18283018, 0x96a13796, 0x050f0a05, 0x9ab52f9a, 0x07090e07, 0x12362412, 0x809b1b80, 0xe23ddfe2, 0xeb26cdeb, 0x27694e27, 0xb2cd7fb2, 0x759fea75, 0x091b1209, 0x839e1d83, 0x2c74582c, 0x1a2e341a, 0x1b2d361b, 0x6eb2dc6e, 0x5aeeb45a, 0xa0fb5ba0, 0x52f6a452, 0x3b4d763b, 0xd661b7d6, 0xb3ce7db3, 0x297b5229, 0xe33edde3, 0x2f715e2f, 0x84971384, 0x53f5a653, 0xd168b9d1, 0x00000000, 0xed2cc1ed, 0x20604020, 0xfc1fe3fc, 0xb1c879b1, 0x5bedb65b, 0x6abed46a, 0xcb468dcb, 0xbed967be, 0x394b7239, 0x4ade944a, 0x4cd4984c, 0x58e8b058, 0xcf4a85cf, 0xd06bbbd0, 0xef2ac5ef, 0xaae54faa, 0xfb16edfb, 0x43c58643, 0x4dd79a4d, 0x33556633, 0x85941185, 0x45cf8a45, 0xf910e9f9, 0x02060402, 0x7f81fe7f, 0x50f0a050, 0x3c44783c, 0x9fba259f, 0xa8e34ba8, 0x51f3a251, 0xa3fe5da3, 0x40c08040, 0x8f8a058f, 0x92ad3f92, 0x9dbc219d, 0x38487038, 0xf504f1f5, 0xbcdf63bc, 0xb6c177b6, 0xda75afda, 0x21634221, 0x10302010, 0xff1ae5ff, 0xf30efdf3, 0xd26dbfd2, 0xcd4c81cd, 0x0c14180c, 0x13352613, 0xec2fc3ec, 0x5fe1be5f, 0x97a23597, 0x44cc8844, 0x17392e17, 0xc45793c4, 0xa7f255a7, 0x7e82fc7e, 0x3d477a3d, 0x64acc864, 0x5de7ba5d, 0x192b3219, 0x7395e673, 0x60a0c060, 0x81981981, 0x4fd19e4f, 0xdc7fa3dc, 0x22664422, 0x2a7e542a, 0x90ab3b90, 0x88830b88, 0x46ca8c46, 0xee29c7ee, 0xb8d36bb8, 0x143c2814, 0xde79a7de, 0x5ee2bc5e, 0x0b1d160b, 0xdb76addb, 0xe03bdbe0, 0x32566432, 0x3a4e743a, 0x0a1e140a, 0x49db9249, 0x060a0c06, 0x246c4824, 0x5ce4b85c, 0xc25d9fc2, 0xd36ebdd3, 0xacef43ac, 0x62a6c462, 0x91a83991, 0x95a43195, 0xe437d3e4, 0x798bf279, 0xe732d5e7, 0xc8438bc8, 0x37596e37, 0x6db7da6d, 0x8d8c018d, 0xd564b1d5, 0x4ed29c4e, 0xa9e049a9, 0x6cb4d86c, 0x56faac56, 0xf407f3f4, 0xea25cfea, 0x65afca65, 0x7a8ef47a, 0xaee947ae, 0x08181008, 0xbad56fba, 0x7888f078, 0x256f4a25, 0x2e725c2e, 0x1c24381c, 0xa6f157a6, 0xb4c773b4, 0xc65197c6, 0xe823cbe8, 0xdd7ca1dd, 0x749ce874, 0x1f213e1f, 0x4bdd964b, 0xbddc61bd, 0x8b860d8b, 0x8a850f8a, 0x7090e070, 0x3e427c3e, 0xb5c471b5, 0x66aacc66, 0x48d89048, 0x03050603, 0xf601f7f6, 0x0e121c0e, 0x61a3c261, 0x355f6a35, 0x57f9ae57, 0xb9d069b9, 0x86911786, 0xc15899c1, 0x1d273a1d, 0x9eb9279e, 0xe138d9e1, 0xf813ebf8, 0x98b32b98, 0x11332211, 0x69bbd269, 0xd970a9d9, 0x8e89078e, 0x94a73394, 0x9bb62d9b, 0x1e223c1e, 0x87921587, 0xe920c9e9, 0xce4987ce, 0x55ffaa55, 0x28785028, 0xdf7aa5df, 0x8c8f038c, 0xa1f859a1, 0x89800989, 0x0d171a0d, 0xbfda65bf, 0xe631d7e6, 0x42c68442, 0x68b8d068, 0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f, 0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16]; - var T4 = [0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6, 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491, 0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56, 0xfefe19e7, 0xd7d762b5, 0xababe64d, 0x76769aec, 0xcaca458f, 0x82829d1f, 0xc9c94089, 0x7d7d87fa, 0xfafa15ef, 0x5959ebb2, 0x4747c98e, 0xf0f00bfb, 0xadadec41, 0xd4d467b3, 0xa2a2fd5f, 0xafafea45, 0x9c9cbf23, 0xa4a4f753, 0x727296e4, 0xc0c05b9b, 0xb7b7c275, 0xfdfd1ce1, 0x9393ae3d, 0x26266a4c, 0x36365a6c, 0x3f3f417e, 0xf7f702f5, 0xcccc4f83, 0x34345c68, 0xa5a5f451, 0xe5e534d1, 0xf1f108f9, 0x717193e2, 0xd8d873ab, 0x31315362, 0x15153f2a, 0x04040c08, 0xc7c75295, 0x23236546, 0xc3c35e9d, 0x18182830, 0x9696a137, 0x05050f0a, 0x9a9ab52f, 0x0707090e, 0x12123624, 0x80809b1b, 0xe2e23ddf, 0xebeb26cd, 0x2727694e, 0xb2b2cd7f, 0x75759fea, 0x09091b12, 0x83839e1d, 0x2c2c7458, 0x1a1a2e34, 0x1b1b2d36, 0x6e6eb2dc, 0x5a5aeeb4, 0xa0a0fb5b, 0x5252f6a4, 0x3b3b4d76, 0xd6d661b7, 0xb3b3ce7d, 0x29297b52, 0xe3e33edd, 0x2f2f715e, 0x84849713, 0x5353f5a6, 0xd1d168b9, 0x00000000, 0xeded2cc1, 0x20206040, 0xfcfc1fe3, 0xb1b1c879, 0x5b5bedb6, 0x6a6abed4, 0xcbcb468d, 0xbebed967, 0x39394b72, 0x4a4ade94, 0x4c4cd498, 0x5858e8b0, 0xcfcf4a85, 0xd0d06bbb, 0xefef2ac5, 0xaaaae54f, 0xfbfb16ed, 0x4343c586, 0x4d4dd79a, 0x33335566, 0x85859411, 0x4545cf8a, 0xf9f910e9, 0x02020604, 0x7f7f81fe, 0x5050f0a0, 0x3c3c4478, 0x9f9fba25, 0xa8a8e34b, 0x5151f3a2, 0xa3a3fe5d, 0x4040c080, 0x8f8f8a05, 0x9292ad3f, 0x9d9dbc21, 0x38384870, 0xf5f504f1, 0xbcbcdf63, 0xb6b6c177, 0xdada75af, 0x21216342, 0x10103020, 0xffff1ae5, 0xf3f30efd, 0xd2d26dbf, 0xcdcd4c81, 0x0c0c1418, 0x13133526, 0xecec2fc3, 0x5f5fe1be, 0x9797a235, 0x4444cc88, 0x1717392e, 0xc4c45793, 0xa7a7f255, 0x7e7e82fc, 0x3d3d477a, 0x6464acc8, 0x5d5de7ba, 0x19192b32, 0x737395e6, 0x6060a0c0, 0x81819819, 0x4f4fd19e, 0xdcdc7fa3, 0x22226644, 0x2a2a7e54, 0x9090ab3b, 0x8888830b, 0x4646ca8c, 0xeeee29c7, 0xb8b8d36b, 0x14143c28, 0xdede79a7, 0x5e5ee2bc, 0x0b0b1d16, 0xdbdb76ad, 0xe0e03bdb, 0x32325664, 0x3a3a4e74, 0x0a0a1e14, 0x4949db92, 0x06060a0c, 0x24246c48, 0x5c5ce4b8, 0xc2c25d9f, 0xd3d36ebd, 0xacacef43, 0x6262a6c4, 0x9191a839, 0x9595a431, 0xe4e437d3, 0x79798bf2, 0xe7e732d5, 0xc8c8438b, 0x3737596e, 0x6d6db7da, 0x8d8d8c01, 0xd5d564b1, 0x4e4ed29c, 0xa9a9e049, 0x6c6cb4d8, 0x5656faac, 0xf4f407f3, 0xeaea25cf, 0x6565afca, 0x7a7a8ef4, 0xaeaee947, 0x08081810, 0xbabad56f, 0x787888f0, 0x25256f4a, 0x2e2e725c, 0x1c1c2438, 0xa6a6f157, 0xb4b4c773, 0xc6c65197, 0xe8e823cb, 0xdddd7ca1, 0x74749ce8, 0x1f1f213e, 0x4b4bdd96, 0xbdbddc61, 0x8b8b860d, 0x8a8a850f, 0x707090e0, 0x3e3e427c, 0xb5b5c471, 0x6666aacc, 0x4848d890, 0x03030506, 0xf6f601f7, 0x0e0e121c, 0x6161a3c2, 0x35355f6a, 0x5757f9ae, 0xb9b9d069, 0x86869117, 0xc1c15899, 0x1d1d273a, 0x9e9eb927, 0xe1e138d9, 0xf8f813eb, 0x9898b32b, 0x11113322, 0x6969bbd2, 0xd9d970a9, 0x8e8e8907, 0x9494a733, 0x9b9bb62d, 0x1e1e223c, 0x87879215, 0xe9e920c9, 0xcece4987, 0x5555ffaa, 0x28287850, 0xdfdf7aa5, 0x8c8c8f03, 0xa1a1f859, 0x89898009, 0x0d0d171a, 0xbfbfda65, 0xe6e631d7, 0x4242c684, 0x6868b8d0, 0x4141c382, 0x9999b029, 0x2d2d775a, 0x0f0f111e, 0xb0b0cb7b, 0x5454fca8, 0xbbbbd66d, 0x16163a2c]; - - // Transformations for decryption - var T5 = [0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393, 0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f, 0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1, 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6, 0x038f5fe7, 0x15929c95, 0xbf6d7aeb, 0x955259da, 0xd4be832d, 0x587421d3, 0x49e06929, 0x8ec9c844, 0x75c2896a, 0xf48e7978, 0x99583e6b, 0x27b971dd, 0xbee14fb6, 0xf088ad17, 0xc920ac66, 0x7dce3ab4, 0x63df4a18, 0xe51a3182, 0x97513360, 0x62537f45, 0xb16477e0, 0xbb6bae84, 0xfe81a01c, 0xf9082b94, 0x70486858, 0x8f45fd19, 0x94de6c87, 0x527bf8b7, 0xab73d323, 0x724b02e2, 0xe31f8f57, 0x6655ab2a, 0xb2eb2807, 0x2fb5c203, 0x86c57b9a, 0xd33708a5, 0x302887f2, 0x23bfa5b2, 0x02036aba, 0xed16825c, 0x8acf1c2b, 0xa779b492, 0xf307f2f0, 0x4e69e2a1, 0x65daf4cd, 0x0605bed5, 0xd134621f, 0xc4a6fe8a, 0x342e539d, 0xa2f355a0, 0x058ae132, 0xa4f6eb75, 0x0b83ec39, 0x4060efaa, 0x5e719f06, 0xbd6e1051, 0x3e218af9, 0x96dd063d, 0xdd3e05ae, 0x4de6bd46, 0x91548db5, 0x71c45d05, 0x0406d46f, 0x605015ff, 0x1998fb24, 0xd6bde997, 0x894043cc, 0x67d99e77, 0xb0e842bd, 0x07898b88, 0xe7195b38, 0x79c8eedb, 0xa17c0a47, 0x7c420fe9, 0xf8841ec9, 0x00000000, 0x09808683, 0x322bed48, 0x1e1170ac, 0x6c5a724e, 0xfd0efffb, 0x0f853856, 0x3daed51e, 0x362d3927, 0x0a0fd964, 0x685ca621, 0x9b5b54d1, 0x24362e3a, 0x0c0a67b1, 0x9357e70f, 0xb4ee96d2, 0x1b9b919e, 0x80c0c54f, 0x61dc20a2, 0x5a774b69, 0x1c121a16, 0xe293ba0a, 0xc0a02ae5, 0x3c22e043, 0x121b171d, 0x0e090d0b, 0xf28bc7ad, 0x2db6a8b9, 0x141ea9c8, 0x57f11985, 0xaf75074c, 0xee99ddbb, 0xa37f60fd, 0xf701269f, 0x5c72f5bc, 0x44663bc5, 0x5bfb7e34, 0x8b432976, 0xcb23c6dc, 0xb6edfc68, 0xb8e4f163, 0xd731dcca, 0x42638510, 0x13972240, 0x84c61120, 0x854a247d, 0xd2bb3df8, 0xaef93211, 0xc729a16d, 0x1d9e2f4b, 0xdcb230f3, 0x0d8652ec, 0x77c1e3d0, 0x2bb3166c, 0xa970b999, 0x119448fa, 0x47e96422, 0xa8fc8cc4, 0xa0f03f1a, 0x567d2cd8, 0x223390ef, 0x87494ec7, 0xd938d1c1, 0x8ccaa2fe, 0x98d40b36, 0xa6f581cf, 0xa57ade28, 0xdab78e26, 0x3fadbfa4, 0x2c3a9de4, 0x5078920d, 0x6a5fcc9b, 0x547e4662, 0xf68d13c2, 0x90d8b8e8, 0x2e39f75e, 0x82c3aff5, 0x9f5d80be, 0x69d0937c, 0x6fd52da9, 0xcf2512b3, 0xc8ac993b, 0x10187da7, 0xe89c636e, 0xdb3bbb7b, 0xcd267809, 0x6e5918f4, 0xec9ab701, 0x834f9aa8, 0xe6956e65, 0xaaffe67e, 0x21bccf08, 0xef15e8e6, 0xbae79bd9, 0x4a6f36ce, 0xea9f09d4, 0x29b07cd6, 0x31a4b2af, 0x2a3f2331, 0xc6a59430, 0x35a266c0, 0x744ebc37, 0xfc82caa6, 0xe090d0b0, 0x33a7d815, 0xf104984a, 0x41ecdaf7, 0x7fcd500e, 0x1791f62f, 0x764dd68d, 0x43efb04d, 0xccaa4d54, 0xe49604df, 0x9ed1b5e3, 0x4c6a881b, 0xc12c1fb8, 0x4665517f, 0x9d5eea04, 0x018c355d, 0xfa877473, 0xfb0b412e, 0xb3671d5a, 0x92dbd252, 0xe9105633, 0x6dd64713, 0x9ad7618c, 0x37a10c7a, 0x59f8148e, 0xeb133c89, 0xcea927ee, 0xb761c935, 0xe11ce5ed, 0x7a47b13c, 0x9cd2df59, 0x55f2733f, 0x1814ce79, 0x73c737bf, 0x53f7cdea, 0x5ffdaa5b, 0xdf3d6f14, 0x7844db86, 0xcaaff381, 0xb968c43e, 0x3824342c, 0xc2a3405f, 0x161dc372, 0xbce2250c, 0x283c498b, 0xff0d9541, 0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742]; - var T6 = [0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e, 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303, 0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c, 0xfc4fe5d7, 0xd7c52acb, 0x80263544, 0x8fb562a3, 0x49deb15a, 0x6725ba1b, 0x9845ea0e, 0xe15dfec0, 0x02c32f75, 0x12814cf0, 0xa38d4697, 0xc66bd3f9, 0xe7038f5f, 0x9515929c, 0xebbf6d7a, 0xda955259, 0x2dd4be83, 0xd3587421, 0x2949e069, 0x448ec9c8, 0x6a75c289, 0x78f48e79, 0x6b99583e, 0xdd27b971, 0xb6bee14f, 0x17f088ad, 0x66c920ac, 0xb47dce3a, 0x1863df4a, 0x82e51a31, 0x60975133, 0x4562537f, 0xe0b16477, 0x84bb6bae, 0x1cfe81a0, 0x94f9082b, 0x58704868, 0x198f45fd, 0x8794de6c, 0xb7527bf8, 0x23ab73d3, 0xe2724b02, 0x57e31f8f, 0x2a6655ab, 0x07b2eb28, 0x032fb5c2, 0x9a86c57b, 0xa5d33708, 0xf2302887, 0xb223bfa5, 0xba02036a, 0x5ced1682, 0x2b8acf1c, 0x92a779b4, 0xf0f307f2, 0xa14e69e2, 0xcd65daf4, 0xd50605be, 0x1fd13462, 0x8ac4a6fe, 0x9d342e53, 0xa0a2f355, 0x32058ae1, 0x75a4f6eb, 0x390b83ec, 0xaa4060ef, 0x065e719f, 0x51bd6e10, 0xf93e218a, 0x3d96dd06, 0xaedd3e05, 0x464de6bd, 0xb591548d, 0x0571c45d, 0x6f0406d4, 0xff605015, 0x241998fb, 0x97d6bde9, 0xcc894043, 0x7767d99e, 0xbdb0e842, 0x8807898b, 0x38e7195b, 0xdb79c8ee, 0x47a17c0a, 0xe97c420f, 0xc9f8841e, 0x00000000, 0x83098086, 0x48322bed, 0xac1e1170, 0x4e6c5a72, 0xfbfd0eff, 0x560f8538, 0x1e3daed5, 0x27362d39, 0x640a0fd9, 0x21685ca6, 0xd19b5b54, 0x3a24362e, 0xb10c0a67, 0x0f9357e7, 0xd2b4ee96, 0x9e1b9b91, 0x4f80c0c5, 0xa261dc20, 0x695a774b, 0x161c121a, 0x0ae293ba, 0xe5c0a02a, 0x433c22e0, 0x1d121b17, 0x0b0e090d, 0xadf28bc7, 0xb92db6a8, 0xc8141ea9, 0x8557f119, 0x4caf7507, 0xbbee99dd, 0xfda37f60, 0x9ff70126, 0xbc5c72f5, 0xc544663b, 0x345bfb7e, 0x768b4329, 0xdccb23c6, 0x68b6edfc, 0x63b8e4f1, 0xcad731dc, 0x10426385, 0x40139722, 0x2084c611, 0x7d854a24, 0xf8d2bb3d, 0x11aef932, 0x6dc729a1, 0x4b1d9e2f, 0xf3dcb230, 0xec0d8652, 0xd077c1e3, 0x6c2bb316, 0x99a970b9, 0xfa119448, 0x2247e964, 0xc4a8fc8c, 0x1aa0f03f, 0xd8567d2c, 0xef223390, 0xc787494e, 0xc1d938d1, 0xfe8ccaa2, 0x3698d40b, 0xcfa6f581, 0x28a57ade, 0x26dab78e, 0xa43fadbf, 0xe42c3a9d, 0x0d507892, 0x9b6a5fcc, 0x62547e46, 0xc2f68d13, 0xe890d8b8, 0x5e2e39f7, 0xf582c3af, 0xbe9f5d80, 0x7c69d093, 0xa96fd52d, 0xb3cf2512, 0x3bc8ac99, 0xa710187d, 0x6ee89c63, 0x7bdb3bbb, 0x09cd2678, 0xf46e5918, 0x01ec9ab7, 0xa8834f9a, 0x65e6956e, 0x7eaaffe6, 0x0821bccf, 0xe6ef15e8, 0xd9bae79b, 0xce4a6f36, 0xd4ea9f09, 0xd629b07c, 0xaf31a4b2, 0x312a3f23, 0x30c6a594, 0xc035a266, 0x37744ebc, 0xa6fc82ca, 0xb0e090d0, 0x1533a7d8, 0x4af10498, 0xf741ecda, 0x0e7fcd50, 0x2f1791f6, 0x8d764dd6, 0x4d43efb0, 0x54ccaa4d, 0xdfe49604, 0xe39ed1b5, 0x1b4c6a88, 0xb8c12c1f, 0x7f466551, 0x049d5eea, 0x5d018c35, 0x73fa8774, 0x2efb0b41, 0x5ab3671d, 0x5292dbd2, 0x33e91056, 0x136dd647, 0x8c9ad761, 0x7a37a10c, 0x8e59f814, 0x89eb133c, 0xeecea927, 0x35b761c9, 0xede11ce5, 0x3c7a47b1, 0x599cd2df, 0x3f55f273, 0x791814ce, 0xbf73c737, 0xea53f7cd, 0x5b5ffdaa, 0x14df3d6f, 0x867844db, 0x81caaff3, 0x3eb968c4, 0x2c382434, 0x5fc2a340, 0x72161dc3, 0x0cbce225, 0x8b283c49, 0x41ff0d95, 0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1, 0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857]; - var T7 = [0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27, 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3, 0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502, 0xd7fc4fe5, 0xcbd7c52a, 0x44802635, 0xa38fb562, 0x5a49deb1, 0x1b6725ba, 0x0e9845ea, 0xc0e15dfe, 0x7502c32f, 0xf012814c, 0x97a38d46, 0xf9c66bd3, 0x5fe7038f, 0x9c951592, 0x7aebbf6d, 0x59da9552, 0x832dd4be, 0x21d35874, 0x692949e0, 0xc8448ec9, 0x896a75c2, 0x7978f48e, 0x3e6b9958, 0x71dd27b9, 0x4fb6bee1, 0xad17f088, 0xac66c920, 0x3ab47dce, 0x4a1863df, 0x3182e51a, 0x33609751, 0x7f456253, 0x77e0b164, 0xae84bb6b, 0xa01cfe81, 0x2b94f908, 0x68587048, 0xfd198f45, 0x6c8794de, 0xf8b7527b, 0xd323ab73, 0x02e2724b, 0x8f57e31f, 0xab2a6655, 0x2807b2eb, 0xc2032fb5, 0x7b9a86c5, 0x08a5d337, 0x87f23028, 0xa5b223bf, 0x6aba0203, 0x825ced16, 0x1c2b8acf, 0xb492a779, 0xf2f0f307, 0xe2a14e69, 0xf4cd65da, 0xbed50605, 0x621fd134, 0xfe8ac4a6, 0x539d342e, 0x55a0a2f3, 0xe132058a, 0xeb75a4f6, 0xec390b83, 0xefaa4060, 0x9f065e71, 0x1051bd6e, 0x8af93e21, 0x063d96dd, 0x05aedd3e, 0xbd464de6, 0x8db59154, 0x5d0571c4, 0xd46f0406, 0x15ff6050, 0xfb241998, 0xe997d6bd, 0x43cc8940, 0x9e7767d9, 0x42bdb0e8, 0x8b880789, 0x5b38e719, 0xeedb79c8, 0x0a47a17c, 0x0fe97c42, 0x1ec9f884, 0x00000000, 0x86830980, 0xed48322b, 0x70ac1e11, 0x724e6c5a, 0xfffbfd0e, 0x38560f85, 0xd51e3dae, 0x3927362d, 0xd9640a0f, 0xa621685c, 0x54d19b5b, 0x2e3a2436, 0x67b10c0a, 0xe70f9357, 0x96d2b4ee, 0x919e1b9b, 0xc54f80c0, 0x20a261dc, 0x4b695a77, 0x1a161c12, 0xba0ae293, 0x2ae5c0a0, 0xe0433c22, 0x171d121b, 0x0d0b0e09, 0xc7adf28b, 0xa8b92db6, 0xa9c8141e, 0x198557f1, 0x074caf75, 0xddbbee99, 0x60fda37f, 0x269ff701, 0xf5bc5c72, 0x3bc54466, 0x7e345bfb, 0x29768b43, 0xc6dccb23, 0xfc68b6ed, 0xf163b8e4, 0xdccad731, 0x85104263, 0x22401397, 0x112084c6, 0x247d854a, 0x3df8d2bb, 0x3211aef9, 0xa16dc729, 0x2f4b1d9e, 0x30f3dcb2, 0x52ec0d86, 0xe3d077c1, 0x166c2bb3, 0xb999a970, 0x48fa1194, 0x642247e9, 0x8cc4a8fc, 0x3f1aa0f0, 0x2cd8567d, 0x90ef2233, 0x4ec78749, 0xd1c1d938, 0xa2fe8cca, 0x0b3698d4, 0x81cfa6f5, 0xde28a57a, 0x8e26dab7, 0xbfa43fad, 0x9de42c3a, 0x920d5078, 0xcc9b6a5f, 0x4662547e, 0x13c2f68d, 0xb8e890d8, 0xf75e2e39, 0xaff582c3, 0x80be9f5d, 0x937c69d0, 0x2da96fd5, 0x12b3cf25, 0x993bc8ac, 0x7da71018, 0x636ee89c, 0xbb7bdb3b, 0x7809cd26, 0x18f46e59, 0xb701ec9a, 0x9aa8834f, 0x6e65e695, 0xe67eaaff, 0xcf0821bc, 0xe8e6ef15, 0x9bd9bae7, 0x36ce4a6f, 0x09d4ea9f, 0x7cd629b0, 0xb2af31a4, 0x23312a3f, 0x9430c6a5, 0x66c035a2, 0xbc37744e, 0xcaa6fc82, 0xd0b0e090, 0xd81533a7, 0x984af104, 0xdaf741ec, 0x500e7fcd, 0xf62f1791, 0xd68d764d, 0xb04d43ef, 0x4d54ccaa, 0x04dfe496, 0xb5e39ed1, 0x881b4c6a, 0x1fb8c12c, 0x517f4665, 0xea049d5e, 0x355d018c, 0x7473fa87, 0x412efb0b, 0x1d5ab367, 0xd25292db, 0x5633e910, 0x47136dd6, 0x618c9ad7, 0x0c7a37a1, 0x148e59f8, 0x3c89eb13, 0x27eecea9, 0xc935b761, 0xe5ede11c, 0xb13c7a47, 0xdf599cd2, 0x733f55f2, 0xce791814, 0x37bf73c7, 0xcdea53f7, 0xaa5b5ffd, 0x6f14df3d, 0xdb867844, 0xf381caaf, 0xc43eb968, 0x342c3824, 0x405fc2a3, 0xc372161d, 0x250cbce2, 0x498b283c, 0x9541ff0d, 0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456, 0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8]; - var T8 = [0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a, 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b, 0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5, 0xe5d7fc4f, 0x2acbd7c5, 0x35448026, 0x62a38fb5, 0xb15a49de, 0xba1b6725, 0xea0e9845, 0xfec0e15d, 0x2f7502c3, 0x4cf01281, 0x4697a38d, 0xd3f9c66b, 0x8f5fe703, 0x929c9515, 0x6d7aebbf, 0x5259da95, 0xbe832dd4, 0x7421d358, 0xe0692949, 0xc9c8448e, 0xc2896a75, 0x8e7978f4, 0x583e6b99, 0xb971dd27, 0xe14fb6be, 0x88ad17f0, 0x20ac66c9, 0xce3ab47d, 0xdf4a1863, 0x1a3182e5, 0x51336097, 0x537f4562, 0x6477e0b1, 0x6bae84bb, 0x81a01cfe, 0x082b94f9, 0x48685870, 0x45fd198f, 0xde6c8794, 0x7bf8b752, 0x73d323ab, 0x4b02e272, 0x1f8f57e3, 0x55ab2a66, 0xeb2807b2, 0xb5c2032f, 0xc57b9a86, 0x3708a5d3, 0x2887f230, 0xbfa5b223, 0x036aba02, 0x16825ced, 0xcf1c2b8a, 0x79b492a7, 0x07f2f0f3, 0x69e2a14e, 0xdaf4cd65, 0x05bed506, 0x34621fd1, 0xa6fe8ac4, 0x2e539d34, 0xf355a0a2, 0x8ae13205, 0xf6eb75a4, 0x83ec390b, 0x60efaa40, 0x719f065e, 0x6e1051bd, 0x218af93e, 0xdd063d96, 0x3e05aedd, 0xe6bd464d, 0x548db591, 0xc45d0571, 0x06d46f04, 0x5015ff60, 0x98fb2419, 0xbde997d6, 0x4043cc89, 0xd99e7767, 0xe842bdb0, 0x898b8807, 0x195b38e7, 0xc8eedb79, 0x7c0a47a1, 0x420fe97c, 0x841ec9f8, 0x00000000, 0x80868309, 0x2bed4832, 0x1170ac1e, 0x5a724e6c, 0x0efffbfd, 0x8538560f, 0xaed51e3d, 0x2d392736, 0x0fd9640a, 0x5ca62168, 0x5b54d19b, 0x362e3a24, 0x0a67b10c, 0x57e70f93, 0xee96d2b4, 0x9b919e1b, 0xc0c54f80, 0xdc20a261, 0x774b695a, 0x121a161c, 0x93ba0ae2, 0xa02ae5c0, 0x22e0433c, 0x1b171d12, 0x090d0b0e, 0x8bc7adf2, 0xb6a8b92d, 0x1ea9c814, 0xf1198557, 0x75074caf, 0x99ddbbee, 0x7f60fda3, 0x01269ff7, 0x72f5bc5c, 0x663bc544, 0xfb7e345b, 0x4329768b, 0x23c6dccb, 0xedfc68b6, 0xe4f163b8, 0x31dccad7, 0x63851042, 0x97224013, 0xc6112084, 0x4a247d85, 0xbb3df8d2, 0xf93211ae, 0x29a16dc7, 0x9e2f4b1d, 0xb230f3dc, 0x8652ec0d, 0xc1e3d077, 0xb3166c2b, 0x70b999a9, 0x9448fa11, 0xe9642247, 0xfc8cc4a8, 0xf03f1aa0, 0x7d2cd856, 0x3390ef22, 0x494ec787, 0x38d1c1d9, 0xcaa2fe8c, 0xd40b3698, 0xf581cfa6, 0x7ade28a5, 0xb78e26da, 0xadbfa43f, 0x3a9de42c, 0x78920d50, 0x5fcc9b6a, 0x7e466254, 0x8d13c2f6, 0xd8b8e890, 0x39f75e2e, 0xc3aff582, 0x5d80be9f, 0xd0937c69, 0xd52da96f, 0x2512b3cf, 0xac993bc8, 0x187da710, 0x9c636ee8, 0x3bbb7bdb, 0x267809cd, 0x5918f46e, 0x9ab701ec, 0x4f9aa883, 0x956e65e6, 0xffe67eaa, 0xbccf0821, 0x15e8e6ef, 0xe79bd9ba, 0x6f36ce4a, 0x9f09d4ea, 0xb07cd629, 0xa4b2af31, 0x3f23312a, 0xa59430c6, 0xa266c035, 0x4ebc3774, 0x82caa6fc, 0x90d0b0e0, 0xa7d81533, 0x04984af1, 0xecdaf741, 0xcd500e7f, 0x91f62f17, 0x4dd68d76, 0xefb04d43, 0xaa4d54cc, 0x9604dfe4, 0xd1b5e39e, 0x6a881b4c, 0x2c1fb8c1, 0x65517f46, 0x5eea049d, 0x8c355d01, 0x877473fa, 0x0b412efb, 0x671d5ab3, 0xdbd25292, 0x105633e9, 0xd647136d, 0xd7618c9a, 0xa10c7a37, 0xf8148e59, 0x133c89eb, 0xa927eece, 0x61c935b7, 0x1ce5ede1, 0x47b13c7a, 0xd2df599c, 0xf2733f55, 0x14ce7918, 0xc737bf73, 0xf7cdea53, 0xfdaa5b5f, 0x3d6f14df, 0x44db8678, 0xaff381ca, 0x68c43eb9, 0x24342c38, 0xa3405fc2, 0x1dc37216, 0xe2250cbc, 0x3c498b28, 0x0d9541ff, 0xa8017139, 0x0cb3de08, 0xb4e49cd8, 0x56c19064, 0xcb84617b, 0x32b670d5, 0x6c5c7448, 0xb85742d0]; - - // Transformations for decryption key expansion - var U1 = [0x00000000, 0x0e090d0b, 0x1c121a16, 0x121b171d, 0x3824342c, 0x362d3927, 0x24362e3a, 0x2a3f2331, 0x70486858, 0x7e416553, 0x6c5a724e, 0x62537f45, 0x486c5c74, 0x4665517f, 0x547e4662, 0x5a774b69, 0xe090d0b0, 0xee99ddbb, 0xfc82caa6, 0xf28bc7ad, 0xd8b4e49c, 0xd6bde997, 0xc4a6fe8a, 0xcaaff381, 0x90d8b8e8, 0x9ed1b5e3, 0x8ccaa2fe, 0x82c3aff5, 0xa8fc8cc4, 0xa6f581cf, 0xb4ee96d2, 0xbae79bd9, 0xdb3bbb7b, 0xd532b670, 0xc729a16d, 0xc920ac66, 0xe31f8f57, 0xed16825c, 0xff0d9541, 0xf104984a, 0xab73d323, 0xa57ade28, 0xb761c935, 0xb968c43e, 0x9357e70f, 0x9d5eea04, 0x8f45fd19, 0x814cf012, 0x3bab6bcb, 0x35a266c0, 0x27b971dd, 0x29b07cd6, 0x038f5fe7, 0x0d8652ec, 0x1f9d45f1, 0x119448fa, 0x4be30393, 0x45ea0e98, 0x57f11985, 0x59f8148e, 0x73c737bf, 0x7dce3ab4, 0x6fd52da9, 0x61dc20a2, 0xad766df6, 0xa37f60fd, 0xb16477e0, 0xbf6d7aeb, 0x955259da, 0x9b5b54d1, 0x894043cc, 0x87494ec7, 0xdd3e05ae, 0xd33708a5, 0xc12c1fb8, 0xcf2512b3, 0xe51a3182, 0xeb133c89, 0xf9082b94, 0xf701269f, 0x4de6bd46, 0x43efb04d, 0x51f4a750, 0x5ffdaa5b, 0x75c2896a, 0x7bcb8461, 0x69d0937c, 0x67d99e77, 0x3daed51e, 0x33a7d815, 0x21bccf08, 0x2fb5c203, 0x058ae132, 0x0b83ec39, 0x1998fb24, 0x1791f62f, 0x764dd68d, 0x7844db86, 0x6a5fcc9b, 0x6456c190, 0x4e69e2a1, 0x4060efaa, 0x527bf8b7, 0x5c72f5bc, 0x0605bed5, 0x080cb3de, 0x1a17a4c3, 0x141ea9c8, 0x3e218af9, 0x302887f2, 0x223390ef, 0x2c3a9de4, 0x96dd063d, 0x98d40b36, 0x8acf1c2b, 0x84c61120, 0xaef93211, 0xa0f03f1a, 0xb2eb2807, 0xbce2250c, 0xe6956e65, 0xe89c636e, 0xfa877473, 0xf48e7978, 0xdeb15a49, 0xd0b85742, 0xc2a3405f, 0xccaa4d54, 0x41ecdaf7, 0x4fe5d7fc, 0x5dfec0e1, 0x53f7cdea, 0x79c8eedb, 0x77c1e3d0, 0x65daf4cd, 0x6bd3f9c6, 0x31a4b2af, 0x3fadbfa4, 0x2db6a8b9, 0x23bfa5b2, 0x09808683, 0x07898b88, 0x15929c95, 0x1b9b919e, 0xa17c0a47, 0xaf75074c, 0xbd6e1051, 0xb3671d5a, 0x99583e6b, 0x97513360, 0x854a247d, 0x8b432976, 0xd134621f, 0xdf3d6f14, 0xcd267809, 0xc32f7502, 0xe9105633, 0xe7195b38, 0xf5024c25, 0xfb0b412e, 0x9ad7618c, 0x94de6c87, 0x86c57b9a, 0x88cc7691, 0xa2f355a0, 0xacfa58ab, 0xbee14fb6, 0xb0e842bd, 0xea9f09d4, 0xe49604df, 0xf68d13c2, 0xf8841ec9, 0xd2bb3df8, 0xdcb230f3, 0xcea927ee, 0xc0a02ae5, 0x7a47b13c, 0x744ebc37, 0x6655ab2a, 0x685ca621, 0x42638510, 0x4c6a881b, 0x5e719f06, 0x5078920d, 0x0a0fd964, 0x0406d46f, 0x161dc372, 0x1814ce79, 0x322bed48, 0x3c22e043, 0x2e39f75e, 0x2030fa55, 0xec9ab701, 0xe293ba0a, 0xf088ad17, 0xfe81a01c, 0xd4be832d, 0xdab78e26, 0xc8ac993b, 0xc6a59430, 0x9cd2df59, 0x92dbd252, 0x80c0c54f, 0x8ec9c844, 0xa4f6eb75, 0xaaffe67e, 0xb8e4f163, 0xb6edfc68, 0x0c0a67b1, 0x02036aba, 0x10187da7, 0x1e1170ac, 0x342e539d, 0x3a275e96, 0x283c498b, 0x26354480, 0x7c420fe9, 0x724b02e2, 0x605015ff, 0x6e5918f4, 0x44663bc5, 0x4a6f36ce, 0x587421d3, 0x567d2cd8, 0x37a10c7a, 0x39a80171, 0x2bb3166c, 0x25ba1b67, 0x0f853856, 0x018c355d, 0x13972240, 0x1d9e2f4b, 0x47e96422, 0x49e06929, 0x5bfb7e34, 0x55f2733f, 0x7fcd500e, 0x71c45d05, 0x63df4a18, 0x6dd64713, 0xd731dcca, 0xd938d1c1, 0xcb23c6dc, 0xc52acbd7, 0xef15e8e6, 0xe11ce5ed, 0xf307f2f0, 0xfd0efffb, 0xa779b492, 0xa970b999, 0xbb6bae84, 0xb562a38f, 0x9f5d80be, 0x91548db5, 0x834f9aa8, 0x8d4697a3]; - var U2 = [0x00000000, 0x0b0e090d, 0x161c121a, 0x1d121b17, 0x2c382434, 0x27362d39, 0x3a24362e, 0x312a3f23, 0x58704868, 0x537e4165, 0x4e6c5a72, 0x4562537f, 0x74486c5c, 0x7f466551, 0x62547e46, 0x695a774b, 0xb0e090d0, 0xbbee99dd, 0xa6fc82ca, 0xadf28bc7, 0x9cd8b4e4, 0x97d6bde9, 0x8ac4a6fe, 0x81caaff3, 0xe890d8b8, 0xe39ed1b5, 0xfe8ccaa2, 0xf582c3af, 0xc4a8fc8c, 0xcfa6f581, 0xd2b4ee96, 0xd9bae79b, 0x7bdb3bbb, 0x70d532b6, 0x6dc729a1, 0x66c920ac, 0x57e31f8f, 0x5ced1682, 0x41ff0d95, 0x4af10498, 0x23ab73d3, 0x28a57ade, 0x35b761c9, 0x3eb968c4, 0x0f9357e7, 0x049d5eea, 0x198f45fd, 0x12814cf0, 0xcb3bab6b, 0xc035a266, 0xdd27b971, 0xd629b07c, 0xe7038f5f, 0xec0d8652, 0xf11f9d45, 0xfa119448, 0x934be303, 0x9845ea0e, 0x8557f119, 0x8e59f814, 0xbf73c737, 0xb47dce3a, 0xa96fd52d, 0xa261dc20, 0xf6ad766d, 0xfda37f60, 0xe0b16477, 0xebbf6d7a, 0xda955259, 0xd19b5b54, 0xcc894043, 0xc787494e, 0xaedd3e05, 0xa5d33708, 0xb8c12c1f, 0xb3cf2512, 0x82e51a31, 0x89eb133c, 0x94f9082b, 0x9ff70126, 0x464de6bd, 0x4d43efb0, 0x5051f4a7, 0x5b5ffdaa, 0x6a75c289, 0x617bcb84, 0x7c69d093, 0x7767d99e, 0x1e3daed5, 0x1533a7d8, 0x0821bccf, 0x032fb5c2, 0x32058ae1, 0x390b83ec, 0x241998fb, 0x2f1791f6, 0x8d764dd6, 0x867844db, 0x9b6a5fcc, 0x906456c1, 0xa14e69e2, 0xaa4060ef, 0xb7527bf8, 0xbc5c72f5, 0xd50605be, 0xde080cb3, 0xc31a17a4, 0xc8141ea9, 0xf93e218a, 0xf2302887, 0xef223390, 0xe42c3a9d, 0x3d96dd06, 0x3698d40b, 0x2b8acf1c, 0x2084c611, 0x11aef932, 0x1aa0f03f, 0x07b2eb28, 0x0cbce225, 0x65e6956e, 0x6ee89c63, 0x73fa8774, 0x78f48e79, 0x49deb15a, 0x42d0b857, 0x5fc2a340, 0x54ccaa4d, 0xf741ecda, 0xfc4fe5d7, 0xe15dfec0, 0xea53f7cd, 0xdb79c8ee, 0xd077c1e3, 0xcd65daf4, 0xc66bd3f9, 0xaf31a4b2, 0xa43fadbf, 0xb92db6a8, 0xb223bfa5, 0x83098086, 0x8807898b, 0x9515929c, 0x9e1b9b91, 0x47a17c0a, 0x4caf7507, 0x51bd6e10, 0x5ab3671d, 0x6b99583e, 0x60975133, 0x7d854a24, 0x768b4329, 0x1fd13462, 0x14df3d6f, 0x09cd2678, 0x02c32f75, 0x33e91056, 0x38e7195b, 0x25f5024c, 0x2efb0b41, 0x8c9ad761, 0x8794de6c, 0x9a86c57b, 0x9188cc76, 0xa0a2f355, 0xabacfa58, 0xb6bee14f, 0xbdb0e842, 0xd4ea9f09, 0xdfe49604, 0xc2f68d13, 0xc9f8841e, 0xf8d2bb3d, 0xf3dcb230, 0xeecea927, 0xe5c0a02a, 0x3c7a47b1, 0x37744ebc, 0x2a6655ab, 0x21685ca6, 0x10426385, 0x1b4c6a88, 0x065e719f, 0x0d507892, 0x640a0fd9, 0x6f0406d4, 0x72161dc3, 0x791814ce, 0x48322bed, 0x433c22e0, 0x5e2e39f7, 0x552030fa, 0x01ec9ab7, 0x0ae293ba, 0x17f088ad, 0x1cfe81a0, 0x2dd4be83, 0x26dab78e, 0x3bc8ac99, 0x30c6a594, 0x599cd2df, 0x5292dbd2, 0x4f80c0c5, 0x448ec9c8, 0x75a4f6eb, 0x7eaaffe6, 0x63b8e4f1, 0x68b6edfc, 0xb10c0a67, 0xba02036a, 0xa710187d, 0xac1e1170, 0x9d342e53, 0x963a275e, 0x8b283c49, 0x80263544, 0xe97c420f, 0xe2724b02, 0xff605015, 0xf46e5918, 0xc544663b, 0xce4a6f36, 0xd3587421, 0xd8567d2c, 0x7a37a10c, 0x7139a801, 0x6c2bb316, 0x6725ba1b, 0x560f8538, 0x5d018c35, 0x40139722, 0x4b1d9e2f, 0x2247e964, 0x2949e069, 0x345bfb7e, 0x3f55f273, 0x0e7fcd50, 0x0571c45d, 0x1863df4a, 0x136dd647, 0xcad731dc, 0xc1d938d1, 0xdccb23c6, 0xd7c52acb, 0xe6ef15e8, 0xede11ce5, 0xf0f307f2, 0xfbfd0eff, 0x92a779b4, 0x99a970b9, 0x84bb6bae, 0x8fb562a3, 0xbe9f5d80, 0xb591548d, 0xa8834f9a, 0xa38d4697]; - var U3 = [0x00000000, 0x0d0b0e09, 0x1a161c12, 0x171d121b, 0x342c3824, 0x3927362d, 0x2e3a2436, 0x23312a3f, 0x68587048, 0x65537e41, 0x724e6c5a, 0x7f456253, 0x5c74486c, 0x517f4665, 0x4662547e, 0x4b695a77, 0xd0b0e090, 0xddbbee99, 0xcaa6fc82, 0xc7adf28b, 0xe49cd8b4, 0xe997d6bd, 0xfe8ac4a6, 0xf381caaf, 0xb8e890d8, 0xb5e39ed1, 0xa2fe8cca, 0xaff582c3, 0x8cc4a8fc, 0x81cfa6f5, 0x96d2b4ee, 0x9bd9bae7, 0xbb7bdb3b, 0xb670d532, 0xa16dc729, 0xac66c920, 0x8f57e31f, 0x825ced16, 0x9541ff0d, 0x984af104, 0xd323ab73, 0xde28a57a, 0xc935b761, 0xc43eb968, 0xe70f9357, 0xea049d5e, 0xfd198f45, 0xf012814c, 0x6bcb3bab, 0x66c035a2, 0x71dd27b9, 0x7cd629b0, 0x5fe7038f, 0x52ec0d86, 0x45f11f9d, 0x48fa1194, 0x03934be3, 0x0e9845ea, 0x198557f1, 0x148e59f8, 0x37bf73c7, 0x3ab47dce, 0x2da96fd5, 0x20a261dc, 0x6df6ad76, 0x60fda37f, 0x77e0b164, 0x7aebbf6d, 0x59da9552, 0x54d19b5b, 0x43cc8940, 0x4ec78749, 0x05aedd3e, 0x08a5d337, 0x1fb8c12c, 0x12b3cf25, 0x3182e51a, 0x3c89eb13, 0x2b94f908, 0x269ff701, 0xbd464de6, 0xb04d43ef, 0xa75051f4, 0xaa5b5ffd, 0x896a75c2, 0x84617bcb, 0x937c69d0, 0x9e7767d9, 0xd51e3dae, 0xd81533a7, 0xcf0821bc, 0xc2032fb5, 0xe132058a, 0xec390b83, 0xfb241998, 0xf62f1791, 0xd68d764d, 0xdb867844, 0xcc9b6a5f, 0xc1906456, 0xe2a14e69, 0xefaa4060, 0xf8b7527b, 0xf5bc5c72, 0xbed50605, 0xb3de080c, 0xa4c31a17, 0xa9c8141e, 0x8af93e21, 0x87f23028, 0x90ef2233, 0x9de42c3a, 0x063d96dd, 0x0b3698d4, 0x1c2b8acf, 0x112084c6, 0x3211aef9, 0x3f1aa0f0, 0x2807b2eb, 0x250cbce2, 0x6e65e695, 0x636ee89c, 0x7473fa87, 0x7978f48e, 0x5a49deb1, 0x5742d0b8, 0x405fc2a3, 0x4d54ccaa, 0xdaf741ec, 0xd7fc4fe5, 0xc0e15dfe, 0xcdea53f7, 0xeedb79c8, 0xe3d077c1, 0xf4cd65da, 0xf9c66bd3, 0xb2af31a4, 0xbfa43fad, 0xa8b92db6, 0xa5b223bf, 0x86830980, 0x8b880789, 0x9c951592, 0x919e1b9b, 0x0a47a17c, 0x074caf75, 0x1051bd6e, 0x1d5ab367, 0x3e6b9958, 0x33609751, 0x247d854a, 0x29768b43, 0x621fd134, 0x6f14df3d, 0x7809cd26, 0x7502c32f, 0x5633e910, 0x5b38e719, 0x4c25f502, 0x412efb0b, 0x618c9ad7, 0x6c8794de, 0x7b9a86c5, 0x769188cc, 0x55a0a2f3, 0x58abacfa, 0x4fb6bee1, 0x42bdb0e8, 0x09d4ea9f, 0x04dfe496, 0x13c2f68d, 0x1ec9f884, 0x3df8d2bb, 0x30f3dcb2, 0x27eecea9, 0x2ae5c0a0, 0xb13c7a47, 0xbc37744e, 0xab2a6655, 0xa621685c, 0x85104263, 0x881b4c6a, 0x9f065e71, 0x920d5078, 0xd9640a0f, 0xd46f0406, 0xc372161d, 0xce791814, 0xed48322b, 0xe0433c22, 0xf75e2e39, 0xfa552030, 0xb701ec9a, 0xba0ae293, 0xad17f088, 0xa01cfe81, 0x832dd4be, 0x8e26dab7, 0x993bc8ac, 0x9430c6a5, 0xdf599cd2, 0xd25292db, 0xc54f80c0, 0xc8448ec9, 0xeb75a4f6, 0xe67eaaff, 0xf163b8e4, 0xfc68b6ed, 0x67b10c0a, 0x6aba0203, 0x7da71018, 0x70ac1e11, 0x539d342e, 0x5e963a27, 0x498b283c, 0x44802635, 0x0fe97c42, 0x02e2724b, 0x15ff6050, 0x18f46e59, 0x3bc54466, 0x36ce4a6f, 0x21d35874, 0x2cd8567d, 0x0c7a37a1, 0x017139a8, 0x166c2bb3, 0x1b6725ba, 0x38560f85, 0x355d018c, 0x22401397, 0x2f4b1d9e, 0x642247e9, 0x692949e0, 0x7e345bfb, 0x733f55f2, 0x500e7fcd, 0x5d0571c4, 0x4a1863df, 0x47136dd6, 0xdccad731, 0xd1c1d938, 0xc6dccb23, 0xcbd7c52a, 0xe8e6ef15, 0xe5ede11c, 0xf2f0f307, 0xfffbfd0e, 0xb492a779, 0xb999a970, 0xae84bb6b, 0xa38fb562, 0x80be9f5d, 0x8db59154, 0x9aa8834f, 0x97a38d46]; - var U4 = [0x00000000, 0x090d0b0e, 0x121a161c, 0x1b171d12, 0x24342c38, 0x2d392736, 0x362e3a24, 0x3f23312a, 0x48685870, 0x4165537e, 0x5a724e6c, 0x537f4562, 0x6c5c7448, 0x65517f46, 0x7e466254, 0x774b695a, 0x90d0b0e0, 0x99ddbbee, 0x82caa6fc, 0x8bc7adf2, 0xb4e49cd8, 0xbde997d6, 0xa6fe8ac4, 0xaff381ca, 0xd8b8e890, 0xd1b5e39e, 0xcaa2fe8c, 0xc3aff582, 0xfc8cc4a8, 0xf581cfa6, 0xee96d2b4, 0xe79bd9ba, 0x3bbb7bdb, 0x32b670d5, 0x29a16dc7, 0x20ac66c9, 0x1f8f57e3, 0x16825ced, 0x0d9541ff, 0x04984af1, 0x73d323ab, 0x7ade28a5, 0x61c935b7, 0x68c43eb9, 0x57e70f93, 0x5eea049d, 0x45fd198f, 0x4cf01281, 0xab6bcb3b, 0xa266c035, 0xb971dd27, 0xb07cd629, 0x8f5fe703, 0x8652ec0d, 0x9d45f11f, 0x9448fa11, 0xe303934b, 0xea0e9845, 0xf1198557, 0xf8148e59, 0xc737bf73, 0xce3ab47d, 0xd52da96f, 0xdc20a261, 0x766df6ad, 0x7f60fda3, 0x6477e0b1, 0x6d7aebbf, 0x5259da95, 0x5b54d19b, 0x4043cc89, 0x494ec787, 0x3e05aedd, 0x3708a5d3, 0x2c1fb8c1, 0x2512b3cf, 0x1a3182e5, 0x133c89eb, 0x082b94f9, 0x01269ff7, 0xe6bd464d, 0xefb04d43, 0xf4a75051, 0xfdaa5b5f, 0xc2896a75, 0xcb84617b, 0xd0937c69, 0xd99e7767, 0xaed51e3d, 0xa7d81533, 0xbccf0821, 0xb5c2032f, 0x8ae13205, 0x83ec390b, 0x98fb2419, 0x91f62f17, 0x4dd68d76, 0x44db8678, 0x5fcc9b6a, 0x56c19064, 0x69e2a14e, 0x60efaa40, 0x7bf8b752, 0x72f5bc5c, 0x05bed506, 0x0cb3de08, 0x17a4c31a, 0x1ea9c814, 0x218af93e, 0x2887f230, 0x3390ef22, 0x3a9de42c, 0xdd063d96, 0xd40b3698, 0xcf1c2b8a, 0xc6112084, 0xf93211ae, 0xf03f1aa0, 0xeb2807b2, 0xe2250cbc, 0x956e65e6, 0x9c636ee8, 0x877473fa, 0x8e7978f4, 0xb15a49de, 0xb85742d0, 0xa3405fc2, 0xaa4d54cc, 0xecdaf741, 0xe5d7fc4f, 0xfec0e15d, 0xf7cdea53, 0xc8eedb79, 0xc1e3d077, 0xdaf4cd65, 0xd3f9c66b, 0xa4b2af31, 0xadbfa43f, 0xb6a8b92d, 0xbfa5b223, 0x80868309, 0x898b8807, 0x929c9515, 0x9b919e1b, 0x7c0a47a1, 0x75074caf, 0x6e1051bd, 0x671d5ab3, 0x583e6b99, 0x51336097, 0x4a247d85, 0x4329768b, 0x34621fd1, 0x3d6f14df, 0x267809cd, 0x2f7502c3, 0x105633e9, 0x195b38e7, 0x024c25f5, 0x0b412efb, 0xd7618c9a, 0xde6c8794, 0xc57b9a86, 0xcc769188, 0xf355a0a2, 0xfa58abac, 0xe14fb6be, 0xe842bdb0, 0x9f09d4ea, 0x9604dfe4, 0x8d13c2f6, 0x841ec9f8, 0xbb3df8d2, 0xb230f3dc, 0xa927eece, 0xa02ae5c0, 0x47b13c7a, 0x4ebc3774, 0x55ab2a66, 0x5ca62168, 0x63851042, 0x6a881b4c, 0x719f065e, 0x78920d50, 0x0fd9640a, 0x06d46f04, 0x1dc37216, 0x14ce7918, 0x2bed4832, 0x22e0433c, 0x39f75e2e, 0x30fa5520, 0x9ab701ec, 0x93ba0ae2, 0x88ad17f0, 0x81a01cfe, 0xbe832dd4, 0xb78e26da, 0xac993bc8, 0xa59430c6, 0xd2df599c, 0xdbd25292, 0xc0c54f80, 0xc9c8448e, 0xf6eb75a4, 0xffe67eaa, 0xe4f163b8, 0xedfc68b6, 0x0a67b10c, 0x036aba02, 0x187da710, 0x1170ac1e, 0x2e539d34, 0x275e963a, 0x3c498b28, 0x35448026, 0x420fe97c, 0x4b02e272, 0x5015ff60, 0x5918f46e, 0x663bc544, 0x6f36ce4a, 0x7421d358, 0x7d2cd856, 0xa10c7a37, 0xa8017139, 0xb3166c2b, 0xba1b6725, 0x8538560f, 0x8c355d01, 0x97224013, 0x9e2f4b1d, 0xe9642247, 0xe0692949, 0xfb7e345b, 0xf2733f55, 0xcd500e7f, 0xc45d0571, 0xdf4a1863, 0xd647136d, 0x31dccad7, 0x38d1c1d9, 0x23c6dccb, 0x2acbd7c5, 0x15e8e6ef, 0x1ce5ede1, 0x07f2f0f3, 0x0efffbfd, 0x79b492a7, 0x70b999a9, 0x6bae84bb, 0x62a38fb5, 0x5d80be9f, 0x548db591, 0x4f9aa883, 0x4697a38d]; - - function convertToInt32(bytes) { - var result = []; - for (var i = 0; i < bytes.length; i += 4) { - result.push( - (bytes[i ] << 24) | - (bytes[i + 1] << 16) | - (bytes[i + 2] << 8) | - bytes[i + 3] - ); - } - return result; - } - - var AES = function(key) { - if (!(this instanceof AES)) { - throw Error('AES must be instanitated with `new`'); - } - - Object.defineProperty(this, 'key', { - value: coerceArray(key, true) - }); - - this._prepare(); - } - - - AES.prototype._prepare = function() { - - var rounds = numberOfRounds[this.key.length]; - if (rounds == null) { - throw new Error('invalid key size (must be 16, 24 or 32 bytes)'); - } - - // encryption round keys - this._Ke = []; - - // decryption round keys - this._Kd = []; - - for (var i = 0; i <= rounds; i++) { - this._Ke.push([0, 0, 0, 0]); - this._Kd.push([0, 0, 0, 0]); - } - - var roundKeyCount = (rounds + 1) * 4; - var KC = this.key.length / 4; - - // convert the key into ints - var tk = convertToInt32(this.key); - - // copy values into round key arrays - var index; - for (var i = 0; i < KC; i++) { - index = i >> 2; - this._Ke[index][i % 4] = tk[i]; - this._Kd[rounds - index][i % 4] = tk[i]; - } - - // key expansion (fips-197 section 5.2) - var rconpointer = 0; - var t = KC, tt; - while (t < roundKeyCount) { - tt = tk[KC - 1]; - tk[0] ^= ((S[(tt >> 16) & 0xFF] << 24) ^ - (S[(tt >> 8) & 0xFF] << 16) ^ - (S[ tt & 0xFF] << 8) ^ - S[(tt >> 24) & 0xFF] ^ - (rcon[rconpointer] << 24)); - rconpointer += 1; - - // key expansion (for non-256 bit) - if (KC != 8) { - for (var i = 1; i < KC; i++) { - tk[i] ^= tk[i - 1]; - } - - // key expansion for 256-bit keys is "slightly different" (fips-197) - } else { - for (var i = 1; i < (KC / 2); i++) { - tk[i] ^= tk[i - 1]; - } - tt = tk[(KC / 2) - 1]; - - tk[KC / 2] ^= (S[ tt & 0xFF] ^ - (S[(tt >> 8) & 0xFF] << 8) ^ - (S[(tt >> 16) & 0xFF] << 16) ^ - (S[(tt >> 24) & 0xFF] << 24)); - - for (var i = (KC / 2) + 1; i < KC; i++) { - tk[i] ^= tk[i - 1]; - } - } - - // copy values into round key arrays - var i = 0, r, c; - while (i < KC && t < roundKeyCount) { - r = t >> 2; - c = t % 4; - this._Ke[r][c] = tk[i]; - this._Kd[rounds - r][c] = tk[i++]; - t++; - } - } - - // inverse-cipher-ify the decryption round key (fips-197 section 5.3) - for (var r = 1; r < rounds; r++) { - for (var c = 0; c < 4; c++) { - tt = this._Kd[r][c]; - this._Kd[r][c] = (U1[(tt >> 24) & 0xFF] ^ - U2[(tt >> 16) & 0xFF] ^ - U3[(tt >> 8) & 0xFF] ^ - U4[ tt & 0xFF]); - } - } - } - - AES.prototype.encrypt = function(plaintext) { - if (plaintext.length != 16) { - throw new Error('invalid plaintext size (must be 16 bytes)'); - } - - var rounds = this._Ke.length - 1; - var a = [0, 0, 0, 0]; - - // convert plaintext to (ints ^ key) - var t = convertToInt32(plaintext); - for (var i = 0; i < 4; i++) { - t[i] ^= this._Ke[0][i]; - } - - // apply round transforms - for (var r = 1; r < rounds; r++) { - for (var i = 0; i < 4; i++) { - a[i] = (T1[(t[ i ] >> 24) & 0xff] ^ - T2[(t[(i + 1) % 4] >> 16) & 0xff] ^ - T3[(t[(i + 2) % 4] >> 8) & 0xff] ^ - T4[ t[(i + 3) % 4] & 0xff] ^ - this._Ke[r][i]); - } - t = a.slice(); - } - - // the last round is special - var result = createArray(16), tt; - for (var i = 0; i < 4; i++) { - tt = this._Ke[rounds][i]; - result[4 * i ] = (S[(t[ i ] >> 24) & 0xff] ^ (tt >> 24)) & 0xff; - result[4 * i + 1] = (S[(t[(i + 1) % 4] >> 16) & 0xff] ^ (tt >> 16)) & 0xff; - result[4 * i + 2] = (S[(t[(i + 2) % 4] >> 8) & 0xff] ^ (tt >> 8)) & 0xff; - result[4 * i + 3] = (S[ t[(i + 3) % 4] & 0xff] ^ tt ) & 0xff; - } - - return result; - } - - AES.prototype.decrypt = function(ciphertext) { - if (ciphertext.length != 16) { - throw new Error('invalid ciphertext size (must be 16 bytes)'); - } - - var rounds = this._Kd.length - 1; - var a = [0, 0, 0, 0]; - - // convert plaintext to (ints ^ key) - var t = convertToInt32(ciphertext); - for (var i = 0; i < 4; i++) { - t[i] ^= this._Kd[0][i]; - } - - // apply round transforms - for (var r = 1; r < rounds; r++) { - for (var i = 0; i < 4; i++) { - a[i] = (T5[(t[ i ] >> 24) & 0xff] ^ - T6[(t[(i + 3) % 4] >> 16) & 0xff] ^ - T7[(t[(i + 2) % 4] >> 8) & 0xff] ^ - T8[ t[(i + 1) % 4] & 0xff] ^ - this._Kd[r][i]); - } - t = a.slice(); - } - - // the last round is special - var result = createArray(16), tt; - for (var i = 0; i < 4; i++) { - tt = this._Kd[rounds][i]; - result[4 * i ] = (Si[(t[ i ] >> 24) & 0xff] ^ (tt >> 24)) & 0xff; - result[4 * i + 1] = (Si[(t[(i + 3) % 4] >> 16) & 0xff] ^ (tt >> 16)) & 0xff; - result[4 * i + 2] = (Si[(t[(i + 2) % 4] >> 8) & 0xff] ^ (tt >> 8)) & 0xff; - result[4 * i + 3] = (Si[ t[(i + 1) % 4] & 0xff] ^ tt ) & 0xff; - } - - return result; - } - - - /** - * Mode Of Operation - Electonic Codebook (ECB) - */ - var ModeOfOperationECB = function(key) { - if (!(this instanceof ModeOfOperationECB)) { - throw Error('AES must be instanitated with `new`'); - } - - this.description = "Electronic Code Block"; - this.name = "ecb"; - - this._aes = new AES(key); - } - - ModeOfOperationECB.prototype.encrypt = function(plaintext) { - plaintext = coerceArray(plaintext); - - if ((plaintext.length % 16) !== 0) { - throw new Error('invalid plaintext size (must be multiple of 16 bytes)'); - } - - var ciphertext = createArray(plaintext.length); - var block = createArray(16); - - for (var i = 0; i < plaintext.length; i += 16) { - copyArray(plaintext, block, 0, i, i + 16); - block = this._aes.encrypt(block); - copyArray(block, ciphertext, i); - } - - return ciphertext; - } - - ModeOfOperationECB.prototype.decrypt = function(ciphertext) { - ciphertext = coerceArray(ciphertext); - - if ((ciphertext.length % 16) !== 0) { - throw new Error('invalid ciphertext size (must be multiple of 16 bytes)'); - } - - var plaintext = createArray(ciphertext.length); - var block = createArray(16); - - for (var i = 0; i < ciphertext.length; i += 16) { - copyArray(ciphertext, block, 0, i, i + 16); - block = this._aes.decrypt(block); - copyArray(block, plaintext, i); - } - - return plaintext; - } - - - /** - * Mode Of Operation - Cipher Block Chaining (CBC) - */ - var ModeOfOperationCBC = function(key, iv) { - if (!(this instanceof ModeOfOperationCBC)) { - throw Error('AES must be instanitated with `new`'); - } - - this.description = "Cipher Block Chaining"; - this.name = "cbc"; - - if (!iv) { - iv = createArray(16); - - } else if (iv.length != 16) { - throw new Error('invalid initialation vector size (must be 16 bytes)'); - } - - this._lastCipherblock = coerceArray(iv, true); - - this._aes = new AES(key); - } - - ModeOfOperationCBC.prototype.encrypt = function(plaintext) { - plaintext = coerceArray(plaintext); - - if ((plaintext.length % 16) !== 0) { - throw new Error('invalid plaintext size (must be multiple of 16 bytes)'); - } - - var ciphertext = createArray(plaintext.length); - var block = createArray(16); - - for (var i = 0; i < plaintext.length; i += 16) { - copyArray(plaintext, block, 0, i, i + 16); - - for (var j = 0; j < 16; j++) { - block[j] ^= this._lastCipherblock[j]; - } - - this._lastCipherblock = this._aes.encrypt(block); - copyArray(this._lastCipherblock, ciphertext, i); - } - - return ciphertext; - } - - ModeOfOperationCBC.prototype.decrypt = function(ciphertext) { - ciphertext = coerceArray(ciphertext); - - if ((ciphertext.length % 16) !== 0) { - throw new Error('invalid ciphertext size (must be multiple of 16 bytes)'); - } - - var plaintext = createArray(ciphertext.length); - var block = createArray(16); - - for (var i = 0; i < ciphertext.length; i += 16) { - copyArray(ciphertext, block, 0, i, i + 16); - block = this._aes.decrypt(block); - - for (var j = 0; j < 16; j++) { - plaintext[i + j] = block[j] ^ this._lastCipherblock[j]; - } - - copyArray(ciphertext, this._lastCipherblock, 0, i, i + 16); - } - - return plaintext; - } - - - /** - * Mode Of Operation - Cipher Feedback (CFB) - */ - var ModeOfOperationCFB = function(key, iv, segmentSize) { - if (!(this instanceof ModeOfOperationCFB)) { - throw Error('AES must be instanitated with `new`'); - } - - this.description = "Cipher Feedback"; - this.name = "cfb"; - - if (!iv) { - iv = createArray(16); - - } else if (iv.length != 16) { - throw new Error('invalid initialation vector size (must be 16 size)'); - } - - if (!segmentSize) { segmentSize = 1; } - - this.segmentSize = segmentSize; - - this._shiftRegister = coerceArray(iv, true); - - this._aes = new AES(key); - } - - ModeOfOperationCFB.prototype.encrypt = function(plaintext) { - if ((plaintext.length % this.segmentSize) != 0) { - throw new Error('invalid plaintext size (must be segmentSize bytes)'); - } - - var encrypted = coerceArray(plaintext, true); - - var xorSegment; - for (var i = 0; i < encrypted.length; i += this.segmentSize) { - xorSegment = this._aes.encrypt(this._shiftRegister); - for (var j = 0; j < this.segmentSize; j++) { - encrypted[i + j] ^= xorSegment[j]; - } - - // Shift the register - copyArray(this._shiftRegister, this._shiftRegister, 0, this.segmentSize); - copyArray(encrypted, this._shiftRegister, 16 - this.segmentSize, i, i + this.segmentSize); - } - - return encrypted; - } - - ModeOfOperationCFB.prototype.decrypt = function(ciphertext) { - if ((ciphertext.length % this.segmentSize) != 0) { - throw new Error('invalid ciphertext size (must be segmentSize bytes)'); - } - - var plaintext = coerceArray(ciphertext, true); - - var xorSegment; - for (var i = 0; i < plaintext.length; i += this.segmentSize) { - xorSegment = this._aes.encrypt(this._shiftRegister); - - for (var j = 0; j < this.segmentSize; j++) { - plaintext[i + j] ^= xorSegment[j]; - } - - // Shift the register - copyArray(this._shiftRegister, this._shiftRegister, 0, this.segmentSize); - copyArray(ciphertext, this._shiftRegister, 16 - this.segmentSize, i, i + this.segmentSize); - } - - return plaintext; - } - - /** - * Mode Of Operation - Output Feedback (OFB) - */ - var ModeOfOperationOFB = function(key, iv) { - if (!(this instanceof ModeOfOperationOFB)) { - throw Error('AES must be instanitated with `new`'); - } - - this.description = "Output Feedback"; - this.name = "ofb"; - - if (!iv) { - iv = createArray(16); - - } else if (iv.length != 16) { - throw new Error('invalid initialation vector size (must be 16 bytes)'); - } - - this._lastPrecipher = coerceArray(iv, true); - this._lastPrecipherIndex = 16; - - this._aes = new AES(key); - } - - ModeOfOperationOFB.prototype.encrypt = function(plaintext) { - var encrypted = coerceArray(plaintext, true); - - for (var i = 0; i < encrypted.length; i++) { - if (this._lastPrecipherIndex === 16) { - this._lastPrecipher = this._aes.encrypt(this._lastPrecipher); - this._lastPrecipherIndex = 0; - } - encrypted[i] ^= this._lastPrecipher[this._lastPrecipherIndex++]; - } - - return encrypted; - } - - // Decryption is symetric - ModeOfOperationOFB.prototype.decrypt = ModeOfOperationOFB.prototype.encrypt; - - - /** - * Counter object for CTR common mode of operation - */ - var Counter = function(initialValue) { - if (!(this instanceof Counter)) { - throw Error('Counter must be instanitated with `new`'); - } - - // We allow 0, but anything false-ish uses the default 1 - if (initialValue !== 0 && !initialValue) { initialValue = 1; } - - if (typeof(initialValue) === 'number') { - this._counter = createArray(16); - this.setValue(initialValue); - - } else { - this.setBytes(initialValue); - } - } - - Counter.prototype.setValue = function(value) { - if (typeof(value) !== 'number' || parseInt(value) != value) { - throw new Error('invalid counter value (must be an integer)'); - } - - // We cannot safely handle numbers beyond the safe range for integers - if (value > Number.MAX_SAFE_INTEGER) { - throw new Error('integer value out of safe range'); - } - - for (var index = 15; index >= 0; --index) { - this._counter[index] = value % 256; - value = parseInt(value / 256); - } - } - - Counter.prototype.setBytes = function(bytes) { - bytes = coerceArray(bytes, true); - - if (bytes.length != 16) { - throw new Error('invalid counter bytes size (must be 16 bytes)'); - } - - this._counter = bytes; - }; - - Counter.prototype.increment = function() { - for (var i = 15; i >= 0; i--) { - if (this._counter[i] === 255) { - this._counter[i] = 0; - } else { - this._counter[i]++; - break; - } - } - } - - - /** - * Mode Of Operation - Counter (CTR) - */ - var ModeOfOperationCTR = function(key, counter) { - if (!(this instanceof ModeOfOperationCTR)) { - throw Error('AES must be instanitated with `new`'); - } - - this.description = "Counter"; - this.name = "ctr"; - - if (!(counter instanceof Counter)) { - counter = new Counter(counter) - } - - this._counter = counter; - - this._remainingCounter = null; - this._remainingCounterIndex = 16; - - this._aes = new AES(key); - } - - ModeOfOperationCTR.prototype.encrypt = function(plaintext) { - var encrypted = coerceArray(plaintext, true); - - for (var i = 0; i < encrypted.length; i++) { - if (this._remainingCounterIndex === 16) { - this._remainingCounter = this._aes.encrypt(this._counter._counter); - this._remainingCounterIndex = 0; - this._counter.increment(); - } - encrypted[i] ^= this._remainingCounter[this._remainingCounterIndex++]; - } - - return encrypted; - } - - // Decryption is symetric - ModeOfOperationCTR.prototype.decrypt = ModeOfOperationCTR.prototype.encrypt; - - - /////////////////////// - // Padding - - // See:https://tools.ietf.org/html/rfc2315 - function pkcs7pad(data) { - data = coerceArray(data, true); - var padder = 16 - (data.length % 16); - var result = createArray(data.length + padder); - copyArray(data, result); - for (var i = data.length; i < result.length; i++) { - result[i] = padder; - } - return result; - } - - function pkcs7strip(data) { - data = coerceArray(data, true); - if (data.length < 16) { throw new Error('PKCS#7 invalid length'); } - - var padder = data[data.length - 1]; - if (padder > 16) { throw new Error('PKCS#7 padding byte out of range'); } - - var length = data.length - padder; - for (var i = 0; i < padder; i++) { - if (data[length + i] !== padder) { - throw new Error('PKCS#7 invalid padding byte'); - } - } - - var result = createArray(length); - copyArray(data, result, 0, 0, length); - return result; - } - - /////////////////////// - // Exporting - - - // The block cipher - var aesjs = { - AES: AES, - Counter: Counter, - - ModeOfOperation: { - ecb: ModeOfOperationECB, - cbc: ModeOfOperationCBC, - cfb: ModeOfOperationCFB, - ofb: ModeOfOperationOFB, - ctr: ModeOfOperationCTR - }, - - utils: { - hex: convertHex, - utf8: convertUtf8 - }, - - padding: { - pkcs7: { - pad: pkcs7pad, - strip: pkcs7strip - } - }, - - _arrayTest: { - coerceArray: coerceArray, - createArray: createArray, - copyArray: copyArray, - } - }; - - - // node.js - if (typeof exports !== 'undefined') { - module.exports = aesjs - - // RequireJS/AMD - // http://www.requirejs.org/docs/api.html - // https://github.com/amdjs/amdjs-api/wiki/AMD - } else if (typeof(define) === 'function' && define.amd) { - define(aesjs); - - // Web Browsers - } else { - - // If there was an existing library at "aesjs" make sure it's still available - if (root.aesjs) { - aesjs._aesjs = root.aesjs; - } - - root.aesjs = aesjs; - } - - -})(this); diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/crypto-dh.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/crypto-dh.js deleted file mode 100644 index a2d2ecb..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/crypto-dh.js +++ /dev/null @@ -1,42 +0,0 @@ -var generatePrime = require('./lib/generatePrime') - -var DH = require('./lib/dh') -var Buffer = require('./lib/safe-buffer').Buffer - -function getDiffieHellman(mod) { - var prime = new Buffer(primes[mod].prime, 'hex') - var gen = new Buffer(primes[mod].gen, 'hex') - - return new DH(prime, gen) -} - -var ENCODINGS = { - 'binary': true, 'hex': true, 'base64': true -} - -function createDiffieHellman(prime, enc, generator, genc) { - if (Buffer.isBuffer(enc) || ENCODINGS[enc] === undefined) { - return createDiffieHellman(prime, 'binary', enc, generator) - } - - enc = enc || 'binary' - genc = genc || 'binary' - generator = generator || new Buffer([2]) - - if (!Buffer.isBuffer(generator)) { - generator = new Buffer(generator, genc) - } - - if (typeof prime === 'number') { - return new DH(generatePrime(prime, generator), generator, true) - } - - if (!Buffer.isBuffer(prime)) { - prime = new Buffer(prime, enc) - } - - return new DH(prime, generator, true) -} - -exports.DiffieHellmanGroup = exports.createDiffieHellmanGroup = exports.getDiffieHellman = getDiffieHellman -exports.createDiffieHellman = exports.DiffieHellman = createDiffieHellman diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/base64-js.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/base64-js.js deleted file mode 100644 index 44c6766..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/base64-js.js +++ /dev/null @@ -1,151 +0,0 @@ -'use strict' - -exports.byteLength = byteLength -exports.toByteArray = toByteArray -exports.fromByteArray = fromByteArray - -var lookup = [] -var revLookup = [] -var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array - -var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' -for (var i = 0, len = code.length; i < len; ++i) { - lookup[i] = code[i] - revLookup[code.charCodeAt(i)] = i -} - -// Support decoding URL-safe base64 strings, as Node.js does. -// See: https://en.wikipedia.org/wiki/Base64#URL_applications -revLookup['-'.charCodeAt(0)] = 62 -revLookup['_'.charCodeAt(0)] = 63 - -function getLens (b64) { - var len = b64.length - - if (len % 4 > 0) { - throw new Error('Invalid string. Length must be a multiple of 4') - } - - // Trim off extra bytes after placeholder bytes are found - // See: https://github.com/beatgammit/base64-js/issues/42 - var validLen = b64.indexOf('=') - if (validLen === -1) validLen = len - - var placeHoldersLen = validLen === len - ? 0 - : 4 - (validLen % 4) - - return [validLen, placeHoldersLen] -} - -// base64 is 4/3 + up to two characters of the original data -function byteLength (b64) { - var lens = getLens(b64) - var validLen = lens[0] - var placeHoldersLen = lens[1] - return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen -} - -function _byteLength (b64, validLen, placeHoldersLen) { - return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen -} - -function toByteArray (b64) { - var tmp - var lens = getLens(b64) - var validLen = lens[0] - var placeHoldersLen = lens[1] - - var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen)) - - var curByte = 0 - - // if there are placeholders, only get up to the last complete 4 chars - var len = placeHoldersLen > 0 - ? validLen - 4 - : validLen - - for (var i = 0; i < len; i += 4) { - tmp = - (revLookup[b64.charCodeAt(i)] << 18) | - (revLookup[b64.charCodeAt(i + 1)] << 12) | - (revLookup[b64.charCodeAt(i + 2)] << 6) | - revLookup[b64.charCodeAt(i + 3)] - arr[curByte++] = (tmp >> 16) & 0xFF - arr[curByte++] = (tmp >> 8) & 0xFF - arr[curByte++] = tmp & 0xFF - } - - if (placeHoldersLen === 2) { - tmp = - (revLookup[b64.charCodeAt(i)] << 2) | - (revLookup[b64.charCodeAt(i + 1)] >> 4) - arr[curByte++] = tmp & 0xFF - } - - if (placeHoldersLen === 1) { - tmp = - (revLookup[b64.charCodeAt(i)] << 10) | - (revLookup[b64.charCodeAt(i + 1)] << 4) | - (revLookup[b64.charCodeAt(i + 2)] >> 2) - arr[curByte++] = (tmp >> 8) & 0xFF - arr[curByte++] = tmp & 0xFF - } - - return arr -} - -function tripletToBase64 (num) { - return lookup[num >> 18 & 0x3F] + - lookup[num >> 12 & 0x3F] + - lookup[num >> 6 & 0x3F] + - lookup[num & 0x3F] -} - -function encodeChunk (uint8, start, end) { - var tmp - var output = [] - for (var i = start; i < end; i += 3) { - tmp = - ((uint8[i] << 16) & 0xFF0000) + - ((uint8[i + 1] << 8) & 0xFF00) + - (uint8[i + 2] & 0xFF) - output.push(tripletToBase64(tmp)) - } - return output.join('') -} - -function fromByteArray (uint8) { - var tmp - var len = uint8.length - var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes - var parts = [] - var maxChunkLength = 16383 // must be multiple of 3 - - // go through the array every three bytes, we'll deal with trailing stuff later - for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { - parts.push(encodeChunk( - uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength) - )) - } - - // pad the end with zeros, but make sure to not forget the extra bytes - if (extraBytes === 1) { - tmp = uint8[len - 1] - parts.push( - lookup[tmp >> 2] + - lookup[(tmp << 4) & 0x3F] + - '==' - ) - } else if (extraBytes === 2) { - tmp = (uint8[len - 2] << 8) + uint8[len - 1] - parts.push( - lookup[tmp >> 10] + - lookup[(tmp >> 4) & 0x3F] + - lookup[(tmp << 2) & 0x3F] + - '=' - ) - } - - return parts.join('') -} diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/bn.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/bn.js deleted file mode 100644 index b590145..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/bn.js +++ /dev/null @@ -1,3427 +0,0 @@ -(function (module, exports) { - 'use strict'; - - // Utils - function assert (val, msg) { - if (!val) throw new Error(msg || 'Assertion failed'); - } - - // Could use `inherits` module, but don't want to move from single file - // architecture yet. - function inherits (ctor, superCtor) { - ctor.super_ = superCtor; - var TempCtor = function () {}; - TempCtor.prototype = superCtor.prototype; - ctor.prototype = new TempCtor(); - ctor.prototype.constructor = ctor; - } - - // BN - - function BN (number, base, endian) { - if (BN.isBN(number)) { - return number; - } - - this.negative = 0; - this.words = null; - this.length = 0; - - // Reduction context - this.red = null; - - if (number !== null) { - if (base === 'le' || base === 'be') { - endian = base; - base = 10; - } - - this._init(number || 0, base || 10, endian || 'be'); - } - } - if (typeof module === 'object') { - module.exports = BN; - } else { - exports.BN = BN; - } - - BN.BN = BN; - BN.wordSize = 26; - - var Buffer; - try { - Buffer = require('buffer').Buffer; - } catch (e) { - } - - BN.isBN = function isBN (num) { - if (num instanceof BN) { - return true; - } - - return num !== null && typeof num === 'object' && - num.constructor.wordSize === BN.wordSize && Array.isArray(num.words); - }; - - BN.max = function max (left, right) { - if (left.cmp(right) > 0) return left; - return right; - }; - - BN.min = function min (left, right) { - if (left.cmp(right) < 0) return left; - return right; - }; - - BN.prototype._init = function init (number, base, endian) { - if (typeof number === 'number') { - return this._initNumber(number, base, endian); - } - - if (typeof number === 'object') { - return this._initArray(number, base, endian); - } - - if (base === 'hex') { - base = 16; - } - assert(base === (base | 0) && base >= 2 && base <= 36); - - number = number.toString().replace(/\s+/g, ''); - var start = 0; - if (number[0] === '-') { - start++; - } - - if (base === 16) { - this._parseHex(number, start); - } else { - this._parseBase(number, base, start); - } - - if (number[0] === '-') { - this.negative = 1; - } - - this.strip(); - - if (endian !== 'le') return; - - this._initArray(this.toArray(), base, endian); - }; - - BN.prototype._initNumber = function _initNumber (number, base, endian) { - if (number < 0) { - this.negative = 1; - number = -number; - } - if (number < 0x4000000) { - this.words = [ number & 0x3ffffff ]; - this.length = 1; - } else if (number < 0x10000000000000) { - this.words = [ - number & 0x3ffffff, - (number / 0x4000000) & 0x3ffffff - ]; - this.length = 2; - } else { - assert(number < 0x20000000000000); // 2 ^ 53 (unsafe) - this.words = [ - number & 0x3ffffff, - (number / 0x4000000) & 0x3ffffff, - 1 - ]; - this.length = 3; - } - - if (endian !== 'le') return; - - // Reverse the bytes - this._initArray(this.toArray(), base, endian); - }; - - BN.prototype._initArray = function _initArray (number, base, endian) { - // Perhaps a Uint8Array - assert(typeof number.length === 'number'); - if (number.length <= 0) { - this.words = [ 0 ]; - this.length = 1; - return this; - } - - this.length = Math.ceil(number.length / 3); - this.words = new Array(this.length); - for (var i = 0; i < this.length; i++) { - this.words[i] = 0; - } - - var j, w; - var off = 0; - if (endian === 'be') { - for (i = number.length - 1, j = 0; i >= 0; i -= 3) { - w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16); - this.words[j] |= (w << off) & 0x3ffffff; - this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; - off += 24; - if (off >= 26) { - off -= 26; - j++; - } - } - } else if (endian === 'le') { - for (i = 0, j = 0; i < number.length; i += 3) { - w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16); - this.words[j] |= (w << off) & 0x3ffffff; - this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; - off += 24; - if (off >= 26) { - off -= 26; - j++; - } - } - } - return this.strip(); - }; - - function parseHex (str, start, end) { - var r = 0; - var len = Math.min(str.length, end); - for (var i = start; i < len; i++) { - var c = str.charCodeAt(i) - 48; - - r <<= 4; - - // 'a' - 'f' - if (c >= 49 && c <= 54) { - r |= c - 49 + 0xa; - - // 'A' - 'F' - } else if (c >= 17 && c <= 22) { - r |= c - 17 + 0xa; - - // '0' - '9' - } else { - r |= c & 0xf; - } - } - return r; - } - - BN.prototype._parseHex = function _parseHex (number, start) { - // Create possibly bigger array to ensure that it fits the number - this.length = Math.ceil((number.length - start) / 6); - this.words = new Array(this.length); - for (var i = 0; i < this.length; i++) { - this.words[i] = 0; - } - - var j, w; - // Scan 24-bit chunks and add them to the number - var off = 0; - for (i = number.length - 6, j = 0; i >= start; i -= 6) { - w = parseHex(number, i, i + 6); - this.words[j] |= (w << off) & 0x3ffffff; - // NOTE: `0x3fffff` is intentional here, 26bits max shift + 24bit hex limb - this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; - off += 24; - if (off >= 26) { - off -= 26; - j++; - } - } - if (i + 6 !== start) { - w = parseHex(number, start, i + 6); - this.words[j] |= (w << off) & 0x3ffffff; - this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; - } - this.strip(); - }; - - function parseBase (str, start, end, mul) { - var r = 0; - var len = Math.min(str.length, end); - for (var i = start; i < len; i++) { - var c = str.charCodeAt(i) - 48; - - r *= mul; - - // 'a' - if (c >= 49) { - r += c - 49 + 0xa; - - // 'A' - } else if (c >= 17) { - r += c - 17 + 0xa; - - // '0' - '9' - } else { - r += c; - } - } - return r; - } - - BN.prototype._parseBase = function _parseBase (number, base, start) { - // Initialize as zero - this.words = [ 0 ]; - this.length = 1; - - // Find length of limb in base - for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) { - limbLen++; - } - limbLen--; - limbPow = (limbPow / base) | 0; - - var total = number.length - start; - var mod = total % limbLen; - var end = Math.min(total, total - mod) + start; - - var word = 0; - for (var i = start; i < end; i += limbLen) { - word = parseBase(number, i, i + limbLen, base); - - this.imuln(limbPow); - if (this.words[0] + word < 0x4000000) { - this.words[0] += word; - } else { - this._iaddn(word); - } - } - - if (mod !== 0) { - var pow = 1; - word = parseBase(number, i, number.length, base); - - for (i = 0; i < mod; i++) { - pow *= base; - } - - this.imuln(pow); - if (this.words[0] + word < 0x4000000) { - this.words[0] += word; - } else { - this._iaddn(word); - } - } - }; - - BN.prototype.copy = function copy (dest) { - dest.words = new Array(this.length); - for (var i = 0; i < this.length; i++) { - dest.words[i] = this.words[i]; - } - dest.length = this.length; - dest.negative = this.negative; - dest.red = this.red; - }; - - BN.prototype.clone = function clone () { - var r = new BN(null); - this.copy(r); - return r; - }; - - BN.prototype._expand = function _expand (size) { - while (this.length < size) { - this.words[this.length++] = 0; - } - return this; - }; - - // Remove leading `0` from `this` - BN.prototype.strip = function strip () { - while (this.length > 1 && this.words[this.length - 1] === 0) { - this.length--; - } - return this._normSign(); - }; - - BN.prototype._normSign = function _normSign () { - // -0 = 0 - if (this.length === 1 && this.words[0] === 0) { - this.negative = 0; - } - return this; - }; - - BN.prototype.inspect = function inspect () { - return (this.red ? ''; - }; - - /* - - var zeros = []; - var groupSizes = []; - var groupBases = []; - - var s = ''; - var i = -1; - while (++i < BN.wordSize) { - zeros[i] = s; - s += '0'; - } - groupSizes[0] = 0; - groupSizes[1] = 0; - groupBases[0] = 0; - groupBases[1] = 0; - var base = 2 - 1; - while (++base < 36 + 1) { - var groupSize = 0; - var groupBase = 1; - while (groupBase < (1 << BN.wordSize) / base) { - groupBase *= base; - groupSize += 1; - } - groupSizes[base] = groupSize; - groupBases[base] = groupBase; - } - - */ - - var zeros = [ - '', - '0', - '00', - '000', - '0000', - '00000', - '000000', - '0000000', - '00000000', - '000000000', - '0000000000', - '00000000000', - '000000000000', - '0000000000000', - '00000000000000', - '000000000000000', - '0000000000000000', - '00000000000000000', - '000000000000000000', - '0000000000000000000', - '00000000000000000000', - '000000000000000000000', - '0000000000000000000000', - '00000000000000000000000', - '000000000000000000000000', - '0000000000000000000000000' - ]; - - var groupSizes = [ - 0, 0, - 25, 16, 12, 11, 10, 9, 8, - 8, 7, 7, 7, 7, 6, 6, - 6, 6, 6, 6, 6, 5, 5, - 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5 - ]; - - var groupBases = [ - 0, 0, - 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216, - 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625, - 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632, - 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149, - 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176 - ]; - - BN.prototype.toString = function toString (base, padding) { - base = base || 10; - padding = padding | 0 || 1; - - var out; - if (base === 16 || base === 'hex') { - out = ''; - var off = 0; - var carry = 0; - for (var i = 0; i < this.length; i++) { - var w = this.words[i]; - var word = (((w << off) | carry) & 0xffffff).toString(16); - carry = (w >>> (24 - off)) & 0xffffff; - if (carry !== 0 || i !== this.length - 1) { - out = zeros[6 - word.length] + word + out; - } else { - out = word + out; - } - off += 2; - if (off >= 26) { - off -= 26; - i--; - } - } - if (carry !== 0) { - out = carry.toString(16) + out; - } - while (out.length % padding !== 0) { - out = '0' + out; - } - if (this.negative !== 0) { - out = '-' + out; - } - return out; - } - - if (base === (base | 0) && base >= 2 && base <= 36) { - // var groupSize = Math.floor(BN.wordSize * Math.LN2 / Math.log(base)); - var groupSize = groupSizes[base]; - // var groupBase = Math.pow(base, groupSize); - var groupBase = groupBases[base]; - out = ''; - var c = this.clone(); - c.negative = 0; - while (!c.isZero()) { - var r = c.modn(groupBase).toString(base); - c = c.idivn(groupBase); - - if (!c.isZero()) { - out = zeros[groupSize - r.length] + r + out; - } else { - out = r + out; - } - } - if (this.isZero()) { - out = '0' + out; - } - while (out.length % padding !== 0) { - out = '0' + out; - } - if (this.negative !== 0) { - out = '-' + out; - } - return out; - } - - assert(false, 'Base should be between 2 and 36'); - }; - - BN.prototype.toNumber = function toNumber () { - var ret = this.words[0]; - if (this.length === 2) { - ret += this.words[1] * 0x4000000; - } else if (this.length === 3 && this.words[2] === 0x01) { - // NOTE: at this stage it is known that the top bit is set - ret += 0x10000000000000 + (this.words[1] * 0x4000000); - } else if (this.length > 2) { - assert(false, 'Number can only safely store up to 53 bits'); - } - return (this.negative !== 0) ? -ret : ret; - }; - - BN.prototype.toJSON = function toJSON () { - return this.toString(16); - }; - - BN.prototype.toBuffer = function toBuffer (endian, length) { - assert(typeof Buffer !== 'undefined'); - return this.toArrayLike(Buffer, endian, length); - }; - - BN.prototype.toArray = function toArray (endian, length) { - return this.toArrayLike(Array, endian, length); - }; - - BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) { - var byteLength = this.byteLength(); - var reqLength = length || Math.max(1, byteLength); - assert(byteLength <= reqLength, 'byte array longer than desired length'); - assert(reqLength > 0, 'Requested array length <= 0'); - - this.strip(); - var littleEndian = endian === 'le'; - var res = new ArrayType(reqLength); - - var b, i; - var q = this.clone(); - if (!littleEndian) { - // Assume big-endian - for (i = 0; i < reqLength - byteLength; i++) { - res[i] = 0; - } - - for (i = 0; !q.isZero(); i++) { - b = q.andln(0xff); - q.iushrn(8); - - res[reqLength - i - 1] = b; - } - } else { - for (i = 0; !q.isZero(); i++) { - b = q.andln(0xff); - q.iushrn(8); - - res[i] = b; - } - - for (; i < reqLength; i++) { - res[i] = 0; - } - } - - return res; - }; - - if (Math.clz32) { - BN.prototype._countBits = function _countBits (w) { - return 32 - Math.clz32(w); - }; - } else { - BN.prototype._countBits = function _countBits (w) { - var t = w; - var r = 0; - if (t >= 0x1000) { - r += 13; - t >>>= 13; - } - if (t >= 0x40) { - r += 7; - t >>>= 7; - } - if (t >= 0x8) { - r += 4; - t >>>= 4; - } - if (t >= 0x02) { - r += 2; - t >>>= 2; - } - return r + t; - }; - } - - BN.prototype._zeroBits = function _zeroBits (w) { - // Short-cut - if (w === 0) return 26; - - var t = w; - var r = 0; - if ((t & 0x1fff) === 0) { - r += 13; - t >>>= 13; - } - if ((t & 0x7f) === 0) { - r += 7; - t >>>= 7; - } - if ((t & 0xf) === 0) { - r += 4; - t >>>= 4; - } - if ((t & 0x3) === 0) { - r += 2; - t >>>= 2; - } - if ((t & 0x1) === 0) { - r++; - } - return r; - }; - - // Return number of used bits in a BN - BN.prototype.bitLength = function bitLength () { - var w = this.words[this.length - 1]; - var hi = this._countBits(w); - return (this.length - 1) * 26 + hi; - }; - - function toBitArray (num) { - var w = new Array(num.bitLength()); - - for (var bit = 0; bit < w.length; bit++) { - var off = (bit / 26) | 0; - var wbit = bit % 26; - - w[bit] = (num.words[off] & (1 << wbit)) >>> wbit; - } - - return w; - } - - // Number of trailing zero bits - BN.prototype.zeroBits = function zeroBits () { - if (this.isZero()) return 0; - - var r = 0; - for (var i = 0; i < this.length; i++) { - var b = this._zeroBits(this.words[i]); - r += b; - if (b !== 26) break; - } - return r; - }; - - BN.prototype.byteLength = function byteLength () { - return Math.ceil(this.bitLength() / 8); - }; - - BN.prototype.toTwos = function toTwos (width) { - if (this.negative !== 0) { - return this.abs().inotn(width).iaddn(1); - } - return this.clone(); - }; - - BN.prototype.fromTwos = function fromTwos (width) { - if (this.testn(width - 1)) { - return this.notn(width).iaddn(1).ineg(); - } - return this.clone(); - }; - - BN.prototype.isNeg = function isNeg () { - return this.negative !== 0; - }; - - // Return negative clone of `this` - BN.prototype.neg = function neg () { - return this.clone().ineg(); - }; - - BN.prototype.ineg = function ineg () { - if (!this.isZero()) { - this.negative ^= 1; - } - - return this; - }; - - // Or `num` with `this` in-place - BN.prototype.iuor = function iuor (num) { - while (this.length < num.length) { - this.words[this.length++] = 0; - } - - for (var i = 0; i < num.length; i++) { - this.words[i] = this.words[i] | num.words[i]; - } - - return this.strip(); - }; - - BN.prototype.ior = function ior (num) { - assert((this.negative | num.negative) === 0); - return this.iuor(num); - }; - - // Or `num` with `this` - BN.prototype.or = function or (num) { - if (this.length > num.length) return this.clone().ior(num); - return num.clone().ior(this); - }; - - BN.prototype.uor = function uor (num) { - if (this.length > num.length) return this.clone().iuor(num); - return num.clone().iuor(this); - }; - - // And `num` with `this` in-place - BN.prototype.iuand = function iuand (num) { - // b = min-length(num, this) - var b; - if (this.length > num.length) { - b = num; - } else { - b = this; - } - - for (var i = 0; i < b.length; i++) { - this.words[i] = this.words[i] & num.words[i]; - } - - this.length = b.length; - - return this.strip(); - }; - - BN.prototype.iand = function iand (num) { - assert((this.negative | num.negative) === 0); - return this.iuand(num); - }; - - // And `num` with `this` - BN.prototype.and = function and (num) { - if (this.length > num.length) return this.clone().iand(num); - return num.clone().iand(this); - }; - - BN.prototype.uand = function uand (num) { - if (this.length > num.length) return this.clone().iuand(num); - return num.clone().iuand(this); - }; - - // Xor `num` with `this` in-place - BN.prototype.iuxor = function iuxor (num) { - // a.length > b.length - var a; - var b; - if (this.length > num.length) { - a = this; - b = num; - } else { - a = num; - b = this; - } - - for (var i = 0; i < b.length; i++) { - this.words[i] = a.words[i] ^ b.words[i]; - } - - if (this !== a) { - for (; i < a.length; i++) { - this.words[i] = a.words[i]; - } - } - - this.length = a.length; - - return this.strip(); - }; - - BN.prototype.ixor = function ixor (num) { - assert((this.negative | num.negative) === 0); - return this.iuxor(num); - }; - - // Xor `num` with `this` - BN.prototype.xor = function xor (num) { - if (this.length > num.length) return this.clone().ixor(num); - return num.clone().ixor(this); - }; - - BN.prototype.uxor = function uxor (num) { - if (this.length > num.length) return this.clone().iuxor(num); - return num.clone().iuxor(this); - }; - - // Not ``this`` with ``width`` bitwidth - BN.prototype.inotn = function inotn (width) { - assert(typeof width === 'number' && width >= 0); - - var bytesNeeded = Math.ceil(width / 26) | 0; - var bitsLeft = width % 26; - - // Extend the buffer with leading zeroes - this._expand(bytesNeeded); - - if (bitsLeft > 0) { - bytesNeeded--; - } - - // Handle complete words - for (var i = 0; i < bytesNeeded; i++) { - this.words[i] = ~this.words[i] & 0x3ffffff; - } - - // Handle the residue - if (bitsLeft > 0) { - this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft)); - } - - // And remove leading zeroes - return this.strip(); - }; - - BN.prototype.notn = function notn (width) { - return this.clone().inotn(width); - }; - - // Set `bit` of `this` - BN.prototype.setn = function setn (bit, val) { - assert(typeof bit === 'number' && bit >= 0); - - var off = (bit / 26) | 0; - var wbit = bit % 26; - - this._expand(off + 1); - - if (val) { - this.words[off] = this.words[off] | (1 << wbit); - } else { - this.words[off] = this.words[off] & ~(1 << wbit); - } - - return this.strip(); - }; - - // Add `num` to `this` in-place - BN.prototype.iadd = function iadd (num) { - var r; - - // negative + positive - if (this.negative !== 0 && num.negative === 0) { - this.negative = 0; - r = this.isub(num); - this.negative ^= 1; - return this._normSign(); - - // positive + negative - } else if (this.negative === 0 && num.negative !== 0) { - num.negative = 0; - r = this.isub(num); - num.negative = 1; - return r._normSign(); - } - - // a.length > b.length - var a, b; - if (this.length > num.length) { - a = this; - b = num; - } else { - a = num; - b = this; - } - - var carry = 0; - for (var i = 0; i < b.length; i++) { - r = (a.words[i] | 0) + (b.words[i] | 0) + carry; - this.words[i] = r & 0x3ffffff; - carry = r >>> 26; - } - for (; carry !== 0 && i < a.length; i++) { - r = (a.words[i] | 0) + carry; - this.words[i] = r & 0x3ffffff; - carry = r >>> 26; - } - - this.length = a.length; - if (carry !== 0) { - this.words[this.length] = carry; - this.length++; - // Copy the rest of the words - } else if (a !== this) { - for (; i < a.length; i++) { - this.words[i] = a.words[i]; - } - } - - return this; - }; - - // Add `num` to `this` - BN.prototype.add = function add (num) { - var res; - if (num.negative !== 0 && this.negative === 0) { - num.negative = 0; - res = this.sub(num); - num.negative ^= 1; - return res; - } else if (num.negative === 0 && this.negative !== 0) { - this.negative = 0; - res = num.sub(this); - this.negative = 1; - return res; - } - - if (this.length > num.length) return this.clone().iadd(num); - - return num.clone().iadd(this); - }; - - // Subtract `num` from `this` in-place - BN.prototype.isub = function isub (num) { - // this - (-num) = this + num - if (num.negative !== 0) { - num.negative = 0; - var r = this.iadd(num); - num.negative = 1; - return r._normSign(); - - // -this - num = -(this + num) - } else if (this.negative !== 0) { - this.negative = 0; - this.iadd(num); - this.negative = 1; - return this._normSign(); - } - - // At this point both numbers are positive - var cmp = this.cmp(num); - - // Optimization - zeroify - if (cmp === 0) { - this.negative = 0; - this.length = 1; - this.words[0] = 0; - return this; - } - - // a > b - var a, b; - if (cmp > 0) { - a = this; - b = num; - } else { - a = num; - b = this; - } - - var carry = 0; - for (var i = 0; i < b.length; i++) { - r = (a.words[i] | 0) - (b.words[i] | 0) + carry; - carry = r >> 26; - this.words[i] = r & 0x3ffffff; - } - for (; carry !== 0 && i < a.length; i++) { - r = (a.words[i] | 0) + carry; - carry = r >> 26; - this.words[i] = r & 0x3ffffff; - } - - // Copy rest of the words - if (carry === 0 && i < a.length && a !== this) { - for (; i < a.length; i++) { - this.words[i] = a.words[i]; - } - } - - this.length = Math.max(this.length, i); - - if (a !== this) { - this.negative = 1; - } - - return this.strip(); - }; - - // Subtract `num` from `this` - BN.prototype.sub = function sub (num) { - return this.clone().isub(num); - }; - - function smallMulTo (self, num, out) { - out.negative = num.negative ^ self.negative; - var len = (self.length + num.length) | 0; - out.length = len; - len = (len - 1) | 0; - - // Peel one iteration (compiler can't do it, because of code complexity) - var a = self.words[0] | 0; - var b = num.words[0] | 0; - var r = a * b; - - var lo = r & 0x3ffffff; - var carry = (r / 0x4000000) | 0; - out.words[0] = lo; - - for (var k = 1; k < len; k++) { - // Sum all words with the same `i + j = k` and accumulate `ncarry`, - // note that ncarry could be >= 0x3ffffff - var ncarry = carry >>> 26; - var rword = carry & 0x3ffffff; - var maxJ = Math.min(k, num.length - 1); - for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { - var i = (k - j) | 0; - a = self.words[i] | 0; - b = num.words[j] | 0; - r = a * b + rword; - ncarry += (r / 0x4000000) | 0; - rword = r & 0x3ffffff; - } - out.words[k] = rword | 0; - carry = ncarry | 0; - } - if (carry !== 0) { - out.words[k] = carry | 0; - } else { - out.length--; - } - - return out.strip(); - } - - // TODO(indutny): it may be reasonable to omit it for users who don't need - // to work with 256-bit numbers, otherwise it gives 20% improvement for 256-bit - // multiplication (like elliptic secp256k1). - var comb10MulTo = function comb10MulTo (self, num, out) { - var a = self.words; - var b = num.words; - var o = out.words; - var c = 0; - var lo; - var mid; - var hi; - var a0 = a[0] | 0; - var al0 = a0 & 0x1fff; - var ah0 = a0 >>> 13; - var a1 = a[1] | 0; - var al1 = a1 & 0x1fff; - var ah1 = a1 >>> 13; - var a2 = a[2] | 0; - var al2 = a2 & 0x1fff; - var ah2 = a2 >>> 13; - var a3 = a[3] | 0; - var al3 = a3 & 0x1fff; - var ah3 = a3 >>> 13; - var a4 = a[4] | 0; - var al4 = a4 & 0x1fff; - var ah4 = a4 >>> 13; - var a5 = a[5] | 0; - var al5 = a5 & 0x1fff; - var ah5 = a5 >>> 13; - var a6 = a[6] | 0; - var al6 = a6 & 0x1fff; - var ah6 = a6 >>> 13; - var a7 = a[7] | 0; - var al7 = a7 & 0x1fff; - var ah7 = a7 >>> 13; - var a8 = a[8] | 0; - var al8 = a8 & 0x1fff; - var ah8 = a8 >>> 13; - var a9 = a[9] | 0; - var al9 = a9 & 0x1fff; - var ah9 = a9 >>> 13; - var b0 = b[0] | 0; - var bl0 = b0 & 0x1fff; - var bh0 = b0 >>> 13; - var b1 = b[1] | 0; - var bl1 = b1 & 0x1fff; - var bh1 = b1 >>> 13; - var b2 = b[2] | 0; - var bl2 = b2 & 0x1fff; - var bh2 = b2 >>> 13; - var b3 = b[3] | 0; - var bl3 = b3 & 0x1fff; - var bh3 = b3 >>> 13; - var b4 = b[4] | 0; - var bl4 = b4 & 0x1fff; - var bh4 = b4 >>> 13; - var b5 = b[5] | 0; - var bl5 = b5 & 0x1fff; - var bh5 = b5 >>> 13; - var b6 = b[6] | 0; - var bl6 = b6 & 0x1fff; - var bh6 = b6 >>> 13; - var b7 = b[7] | 0; - var bl7 = b7 & 0x1fff; - var bh7 = b7 >>> 13; - var b8 = b[8] | 0; - var bl8 = b8 & 0x1fff; - var bh8 = b8 >>> 13; - var b9 = b[9] | 0; - var bl9 = b9 & 0x1fff; - var bh9 = b9 >>> 13; - - out.negative = self.negative ^ num.negative; - out.length = 19; - /* k = 0 */ - lo = Math.imul(al0, bl0); - mid = Math.imul(al0, bh0); - mid = (mid + Math.imul(ah0, bl0)) | 0; - hi = Math.imul(ah0, bh0); - var w0 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w0 >>> 26)) | 0; - w0 &= 0x3ffffff; - /* k = 1 */ - lo = Math.imul(al1, bl0); - mid = Math.imul(al1, bh0); - mid = (mid + Math.imul(ah1, bl0)) | 0; - hi = Math.imul(ah1, bh0); - lo = (lo + Math.imul(al0, bl1)) | 0; - mid = (mid + Math.imul(al0, bh1)) | 0; - mid = (mid + Math.imul(ah0, bl1)) | 0; - hi = (hi + Math.imul(ah0, bh1)) | 0; - var w1 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w1 >>> 26)) | 0; - w1 &= 0x3ffffff; - /* k = 2 */ - lo = Math.imul(al2, bl0); - mid = Math.imul(al2, bh0); - mid = (mid + Math.imul(ah2, bl0)) | 0; - hi = Math.imul(ah2, bh0); - lo = (lo + Math.imul(al1, bl1)) | 0; - mid = (mid + Math.imul(al1, bh1)) | 0; - mid = (mid + Math.imul(ah1, bl1)) | 0; - hi = (hi + Math.imul(ah1, bh1)) | 0; - lo = (lo + Math.imul(al0, bl2)) | 0; - mid = (mid + Math.imul(al0, bh2)) | 0; - mid = (mid + Math.imul(ah0, bl2)) | 0; - hi = (hi + Math.imul(ah0, bh2)) | 0; - var w2 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w2 >>> 26)) | 0; - w2 &= 0x3ffffff; - /* k = 3 */ - lo = Math.imul(al3, bl0); - mid = Math.imul(al3, bh0); - mid = (mid + Math.imul(ah3, bl0)) | 0; - hi = Math.imul(ah3, bh0); - lo = (lo + Math.imul(al2, bl1)) | 0; - mid = (mid + Math.imul(al2, bh1)) | 0; - mid = (mid + Math.imul(ah2, bl1)) | 0; - hi = (hi + Math.imul(ah2, bh1)) | 0; - lo = (lo + Math.imul(al1, bl2)) | 0; - mid = (mid + Math.imul(al1, bh2)) | 0; - mid = (mid + Math.imul(ah1, bl2)) | 0; - hi = (hi + Math.imul(ah1, bh2)) | 0; - lo = (lo + Math.imul(al0, bl3)) | 0; - mid = (mid + Math.imul(al0, bh3)) | 0; - mid = (mid + Math.imul(ah0, bl3)) | 0; - hi = (hi + Math.imul(ah0, bh3)) | 0; - var w3 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w3 >>> 26)) | 0; - w3 &= 0x3ffffff; - /* k = 4 */ - lo = Math.imul(al4, bl0); - mid = Math.imul(al4, bh0); - mid = (mid + Math.imul(ah4, bl0)) | 0; - hi = Math.imul(ah4, bh0); - lo = (lo + Math.imul(al3, bl1)) | 0; - mid = (mid + Math.imul(al3, bh1)) | 0; - mid = (mid + Math.imul(ah3, bl1)) | 0; - hi = (hi + Math.imul(ah3, bh1)) | 0; - lo = (lo + Math.imul(al2, bl2)) | 0; - mid = (mid + Math.imul(al2, bh2)) | 0; - mid = (mid + Math.imul(ah2, bl2)) | 0; - hi = (hi + Math.imul(ah2, bh2)) | 0; - lo = (lo + Math.imul(al1, bl3)) | 0; - mid = (mid + Math.imul(al1, bh3)) | 0; - mid = (mid + Math.imul(ah1, bl3)) | 0; - hi = (hi + Math.imul(ah1, bh3)) | 0; - lo = (lo + Math.imul(al0, bl4)) | 0; - mid = (mid + Math.imul(al0, bh4)) | 0; - mid = (mid + Math.imul(ah0, bl4)) | 0; - hi = (hi + Math.imul(ah0, bh4)) | 0; - var w4 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w4 >>> 26)) | 0; - w4 &= 0x3ffffff; - /* k = 5 */ - lo = Math.imul(al5, bl0); - mid = Math.imul(al5, bh0); - mid = (mid + Math.imul(ah5, bl0)) | 0; - hi = Math.imul(ah5, bh0); - lo = (lo + Math.imul(al4, bl1)) | 0; - mid = (mid + Math.imul(al4, bh1)) | 0; - mid = (mid + Math.imul(ah4, bl1)) | 0; - hi = (hi + Math.imul(ah4, bh1)) | 0; - lo = (lo + Math.imul(al3, bl2)) | 0; - mid = (mid + Math.imul(al3, bh2)) | 0; - mid = (mid + Math.imul(ah3, bl2)) | 0; - hi = (hi + Math.imul(ah3, bh2)) | 0; - lo = (lo + Math.imul(al2, bl3)) | 0; - mid = (mid + Math.imul(al2, bh3)) | 0; - mid = (mid + Math.imul(ah2, bl3)) | 0; - hi = (hi + Math.imul(ah2, bh3)) | 0; - lo = (lo + Math.imul(al1, bl4)) | 0; - mid = (mid + Math.imul(al1, bh4)) | 0; - mid = (mid + Math.imul(ah1, bl4)) | 0; - hi = (hi + Math.imul(ah1, bh4)) | 0; - lo = (lo + Math.imul(al0, bl5)) | 0; - mid = (mid + Math.imul(al0, bh5)) | 0; - mid = (mid + Math.imul(ah0, bl5)) | 0; - hi = (hi + Math.imul(ah0, bh5)) | 0; - var w5 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w5 >>> 26)) | 0; - w5 &= 0x3ffffff; - /* k = 6 */ - lo = Math.imul(al6, bl0); - mid = Math.imul(al6, bh0); - mid = (mid + Math.imul(ah6, bl0)) | 0; - hi = Math.imul(ah6, bh0); - lo = (lo + Math.imul(al5, bl1)) | 0; - mid = (mid + Math.imul(al5, bh1)) | 0; - mid = (mid + Math.imul(ah5, bl1)) | 0; - hi = (hi + Math.imul(ah5, bh1)) | 0; - lo = (lo + Math.imul(al4, bl2)) | 0; - mid = (mid + Math.imul(al4, bh2)) | 0; - mid = (mid + Math.imul(ah4, bl2)) | 0; - hi = (hi + Math.imul(ah4, bh2)) | 0; - lo = (lo + Math.imul(al3, bl3)) | 0; - mid = (mid + Math.imul(al3, bh3)) | 0; - mid = (mid + Math.imul(ah3, bl3)) | 0; - hi = (hi + Math.imul(ah3, bh3)) | 0; - lo = (lo + Math.imul(al2, bl4)) | 0; - mid = (mid + Math.imul(al2, bh4)) | 0; - mid = (mid + Math.imul(ah2, bl4)) | 0; - hi = (hi + Math.imul(ah2, bh4)) | 0; - lo = (lo + Math.imul(al1, bl5)) | 0; - mid = (mid + Math.imul(al1, bh5)) | 0; - mid = (mid + Math.imul(ah1, bl5)) | 0; - hi = (hi + Math.imul(ah1, bh5)) | 0; - lo = (lo + Math.imul(al0, bl6)) | 0; - mid = (mid + Math.imul(al0, bh6)) | 0; - mid = (mid + Math.imul(ah0, bl6)) | 0; - hi = (hi + Math.imul(ah0, bh6)) | 0; - var w6 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w6 >>> 26)) | 0; - w6 &= 0x3ffffff; - /* k = 7 */ - lo = Math.imul(al7, bl0); - mid = Math.imul(al7, bh0); - mid = (mid + Math.imul(ah7, bl0)) | 0; - hi = Math.imul(ah7, bh0); - lo = (lo + Math.imul(al6, bl1)) | 0; - mid = (mid + Math.imul(al6, bh1)) | 0; - mid = (mid + Math.imul(ah6, bl1)) | 0; - hi = (hi + Math.imul(ah6, bh1)) | 0; - lo = (lo + Math.imul(al5, bl2)) | 0; - mid = (mid + Math.imul(al5, bh2)) | 0; - mid = (mid + Math.imul(ah5, bl2)) | 0; - hi = (hi + Math.imul(ah5, bh2)) | 0; - lo = (lo + Math.imul(al4, bl3)) | 0; - mid = (mid + Math.imul(al4, bh3)) | 0; - mid = (mid + Math.imul(ah4, bl3)) | 0; - hi = (hi + Math.imul(ah4, bh3)) | 0; - lo = (lo + Math.imul(al3, bl4)) | 0; - mid = (mid + Math.imul(al3, bh4)) | 0; - mid = (mid + Math.imul(ah3, bl4)) | 0; - hi = (hi + Math.imul(ah3, bh4)) | 0; - lo = (lo + Math.imul(al2, bl5)) | 0; - mid = (mid + Math.imul(al2, bh5)) | 0; - mid = (mid + Math.imul(ah2, bl5)) | 0; - hi = (hi + Math.imul(ah2, bh5)) | 0; - lo = (lo + Math.imul(al1, bl6)) | 0; - mid = (mid + Math.imul(al1, bh6)) | 0; - mid = (mid + Math.imul(ah1, bl6)) | 0; - hi = (hi + Math.imul(ah1, bh6)) | 0; - lo = (lo + Math.imul(al0, bl7)) | 0; - mid = (mid + Math.imul(al0, bh7)) | 0; - mid = (mid + Math.imul(ah0, bl7)) | 0; - hi = (hi + Math.imul(ah0, bh7)) | 0; - var w7 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w7 >>> 26)) | 0; - w7 &= 0x3ffffff; - /* k = 8 */ - lo = Math.imul(al8, bl0); - mid = Math.imul(al8, bh0); - mid = (mid + Math.imul(ah8, bl0)) | 0; - hi = Math.imul(ah8, bh0); - lo = (lo + Math.imul(al7, bl1)) | 0; - mid = (mid + Math.imul(al7, bh1)) | 0; - mid = (mid + Math.imul(ah7, bl1)) | 0; - hi = (hi + Math.imul(ah7, bh1)) | 0; - lo = (lo + Math.imul(al6, bl2)) | 0; - mid = (mid + Math.imul(al6, bh2)) | 0; - mid = (mid + Math.imul(ah6, bl2)) | 0; - hi = (hi + Math.imul(ah6, bh2)) | 0; - lo = (lo + Math.imul(al5, bl3)) | 0; - mid = (mid + Math.imul(al5, bh3)) | 0; - mid = (mid + Math.imul(ah5, bl3)) | 0; - hi = (hi + Math.imul(ah5, bh3)) | 0; - lo = (lo + Math.imul(al4, bl4)) | 0; - mid = (mid + Math.imul(al4, bh4)) | 0; - mid = (mid + Math.imul(ah4, bl4)) | 0; - hi = (hi + Math.imul(ah4, bh4)) | 0; - lo = (lo + Math.imul(al3, bl5)) | 0; - mid = (mid + Math.imul(al3, bh5)) | 0; - mid = (mid + Math.imul(ah3, bl5)) | 0; - hi = (hi + Math.imul(ah3, bh5)) | 0; - lo = (lo + Math.imul(al2, bl6)) | 0; - mid = (mid + Math.imul(al2, bh6)) | 0; - mid = (mid + Math.imul(ah2, bl6)) | 0; - hi = (hi + Math.imul(ah2, bh6)) | 0; - lo = (lo + Math.imul(al1, bl7)) | 0; - mid = (mid + Math.imul(al1, bh7)) | 0; - mid = (mid + Math.imul(ah1, bl7)) | 0; - hi = (hi + Math.imul(ah1, bh7)) | 0; - lo = (lo + Math.imul(al0, bl8)) | 0; - mid = (mid + Math.imul(al0, bh8)) | 0; - mid = (mid + Math.imul(ah0, bl8)) | 0; - hi = (hi + Math.imul(ah0, bh8)) | 0; - var w8 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w8 >>> 26)) | 0; - w8 &= 0x3ffffff; - /* k = 9 */ - lo = Math.imul(al9, bl0); - mid = Math.imul(al9, bh0); - mid = (mid + Math.imul(ah9, bl0)) | 0; - hi = Math.imul(ah9, bh0); - lo = (lo + Math.imul(al8, bl1)) | 0; - mid = (mid + Math.imul(al8, bh1)) | 0; - mid = (mid + Math.imul(ah8, bl1)) | 0; - hi = (hi + Math.imul(ah8, bh1)) | 0; - lo = (lo + Math.imul(al7, bl2)) | 0; - mid = (mid + Math.imul(al7, bh2)) | 0; - mid = (mid + Math.imul(ah7, bl2)) | 0; - hi = (hi + Math.imul(ah7, bh2)) | 0; - lo = (lo + Math.imul(al6, bl3)) | 0; - mid = (mid + Math.imul(al6, bh3)) | 0; - mid = (mid + Math.imul(ah6, bl3)) | 0; - hi = (hi + Math.imul(ah6, bh3)) | 0; - lo = (lo + Math.imul(al5, bl4)) | 0; - mid = (mid + Math.imul(al5, bh4)) | 0; - mid = (mid + Math.imul(ah5, bl4)) | 0; - hi = (hi + Math.imul(ah5, bh4)) | 0; - lo = (lo + Math.imul(al4, bl5)) | 0; - mid = (mid + Math.imul(al4, bh5)) | 0; - mid = (mid + Math.imul(ah4, bl5)) | 0; - hi = (hi + Math.imul(ah4, bh5)) | 0; - lo = (lo + Math.imul(al3, bl6)) | 0; - mid = (mid + Math.imul(al3, bh6)) | 0; - mid = (mid + Math.imul(ah3, bl6)) | 0; - hi = (hi + Math.imul(ah3, bh6)) | 0; - lo = (lo + Math.imul(al2, bl7)) | 0; - mid = (mid + Math.imul(al2, bh7)) | 0; - mid = (mid + Math.imul(ah2, bl7)) | 0; - hi = (hi + Math.imul(ah2, bh7)) | 0; - lo = (lo + Math.imul(al1, bl8)) | 0; - mid = (mid + Math.imul(al1, bh8)) | 0; - mid = (mid + Math.imul(ah1, bl8)) | 0; - hi = (hi + Math.imul(ah1, bh8)) | 0; - lo = (lo + Math.imul(al0, bl9)) | 0; - mid = (mid + Math.imul(al0, bh9)) | 0; - mid = (mid + Math.imul(ah0, bl9)) | 0; - hi = (hi + Math.imul(ah0, bh9)) | 0; - var w9 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w9 >>> 26)) | 0; - w9 &= 0x3ffffff; - /* k = 10 */ - lo = Math.imul(al9, bl1); - mid = Math.imul(al9, bh1); - mid = (mid + Math.imul(ah9, bl1)) | 0; - hi = Math.imul(ah9, bh1); - lo = (lo + Math.imul(al8, bl2)) | 0; - mid = (mid + Math.imul(al8, bh2)) | 0; - mid = (mid + Math.imul(ah8, bl2)) | 0; - hi = (hi + Math.imul(ah8, bh2)) | 0; - lo = (lo + Math.imul(al7, bl3)) | 0; - mid = (mid + Math.imul(al7, bh3)) | 0; - mid = (mid + Math.imul(ah7, bl3)) | 0; - hi = (hi + Math.imul(ah7, bh3)) | 0; - lo = (lo + Math.imul(al6, bl4)) | 0; - mid = (mid + Math.imul(al6, bh4)) | 0; - mid = (mid + Math.imul(ah6, bl4)) | 0; - hi = (hi + Math.imul(ah6, bh4)) | 0; - lo = (lo + Math.imul(al5, bl5)) | 0; - mid = (mid + Math.imul(al5, bh5)) | 0; - mid = (mid + Math.imul(ah5, bl5)) | 0; - hi = (hi + Math.imul(ah5, bh5)) | 0; - lo = (lo + Math.imul(al4, bl6)) | 0; - mid = (mid + Math.imul(al4, bh6)) | 0; - mid = (mid + Math.imul(ah4, bl6)) | 0; - hi = (hi + Math.imul(ah4, bh6)) | 0; - lo = (lo + Math.imul(al3, bl7)) | 0; - mid = (mid + Math.imul(al3, bh7)) | 0; - mid = (mid + Math.imul(ah3, bl7)) | 0; - hi = (hi + Math.imul(ah3, bh7)) | 0; - lo = (lo + Math.imul(al2, bl8)) | 0; - mid = (mid + Math.imul(al2, bh8)) | 0; - mid = (mid + Math.imul(ah2, bl8)) | 0; - hi = (hi + Math.imul(ah2, bh8)) | 0; - lo = (lo + Math.imul(al1, bl9)) | 0; - mid = (mid + Math.imul(al1, bh9)) | 0; - mid = (mid + Math.imul(ah1, bl9)) | 0; - hi = (hi + Math.imul(ah1, bh9)) | 0; - var w10 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w10 >>> 26)) | 0; - w10 &= 0x3ffffff; - /* k = 11 */ - lo = Math.imul(al9, bl2); - mid = Math.imul(al9, bh2); - mid = (mid + Math.imul(ah9, bl2)) | 0; - hi = Math.imul(ah9, bh2); - lo = (lo + Math.imul(al8, bl3)) | 0; - mid = (mid + Math.imul(al8, bh3)) | 0; - mid = (mid + Math.imul(ah8, bl3)) | 0; - hi = (hi + Math.imul(ah8, bh3)) | 0; - lo = (lo + Math.imul(al7, bl4)) | 0; - mid = (mid + Math.imul(al7, bh4)) | 0; - mid = (mid + Math.imul(ah7, bl4)) | 0; - hi = (hi + Math.imul(ah7, bh4)) | 0; - lo = (lo + Math.imul(al6, bl5)) | 0; - mid = (mid + Math.imul(al6, bh5)) | 0; - mid = (mid + Math.imul(ah6, bl5)) | 0; - hi = (hi + Math.imul(ah6, bh5)) | 0; - lo = (lo + Math.imul(al5, bl6)) | 0; - mid = (mid + Math.imul(al5, bh6)) | 0; - mid = (mid + Math.imul(ah5, bl6)) | 0; - hi = (hi + Math.imul(ah5, bh6)) | 0; - lo = (lo + Math.imul(al4, bl7)) | 0; - mid = (mid + Math.imul(al4, bh7)) | 0; - mid = (mid + Math.imul(ah4, bl7)) | 0; - hi = (hi + Math.imul(ah4, bh7)) | 0; - lo = (lo + Math.imul(al3, bl8)) | 0; - mid = (mid + Math.imul(al3, bh8)) | 0; - mid = (mid + Math.imul(ah3, bl8)) | 0; - hi = (hi + Math.imul(ah3, bh8)) | 0; - lo = (lo + Math.imul(al2, bl9)) | 0; - mid = (mid + Math.imul(al2, bh9)) | 0; - mid = (mid + Math.imul(ah2, bl9)) | 0; - hi = (hi + Math.imul(ah2, bh9)) | 0; - var w11 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w11 >>> 26)) | 0; - w11 &= 0x3ffffff; - /* k = 12 */ - lo = Math.imul(al9, bl3); - mid = Math.imul(al9, bh3); - mid = (mid + Math.imul(ah9, bl3)) | 0; - hi = Math.imul(ah9, bh3); - lo = (lo + Math.imul(al8, bl4)) | 0; - mid = (mid + Math.imul(al8, bh4)) | 0; - mid = (mid + Math.imul(ah8, bl4)) | 0; - hi = (hi + Math.imul(ah8, bh4)) | 0; - lo = (lo + Math.imul(al7, bl5)) | 0; - mid = (mid + Math.imul(al7, bh5)) | 0; - mid = (mid + Math.imul(ah7, bl5)) | 0; - hi = (hi + Math.imul(ah7, bh5)) | 0; - lo = (lo + Math.imul(al6, bl6)) | 0; - mid = (mid + Math.imul(al6, bh6)) | 0; - mid = (mid + Math.imul(ah6, bl6)) | 0; - hi = (hi + Math.imul(ah6, bh6)) | 0; - lo = (lo + Math.imul(al5, bl7)) | 0; - mid = (mid + Math.imul(al5, bh7)) | 0; - mid = (mid + Math.imul(ah5, bl7)) | 0; - hi = (hi + Math.imul(ah5, bh7)) | 0; - lo = (lo + Math.imul(al4, bl8)) | 0; - mid = (mid + Math.imul(al4, bh8)) | 0; - mid = (mid + Math.imul(ah4, bl8)) | 0; - hi = (hi + Math.imul(ah4, bh8)) | 0; - lo = (lo + Math.imul(al3, bl9)) | 0; - mid = (mid + Math.imul(al3, bh9)) | 0; - mid = (mid + Math.imul(ah3, bl9)) | 0; - hi = (hi + Math.imul(ah3, bh9)) | 0; - var w12 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w12 >>> 26)) | 0; - w12 &= 0x3ffffff; - /* k = 13 */ - lo = Math.imul(al9, bl4); - mid = Math.imul(al9, bh4); - mid = (mid + Math.imul(ah9, bl4)) | 0; - hi = Math.imul(ah9, bh4); - lo = (lo + Math.imul(al8, bl5)) | 0; - mid = (mid + Math.imul(al8, bh5)) | 0; - mid = (mid + Math.imul(ah8, bl5)) | 0; - hi = (hi + Math.imul(ah8, bh5)) | 0; - lo = (lo + Math.imul(al7, bl6)) | 0; - mid = (mid + Math.imul(al7, bh6)) | 0; - mid = (mid + Math.imul(ah7, bl6)) | 0; - hi = (hi + Math.imul(ah7, bh6)) | 0; - lo = (lo + Math.imul(al6, bl7)) | 0; - mid = (mid + Math.imul(al6, bh7)) | 0; - mid = (mid + Math.imul(ah6, bl7)) | 0; - hi = (hi + Math.imul(ah6, bh7)) | 0; - lo = (lo + Math.imul(al5, bl8)) | 0; - mid = (mid + Math.imul(al5, bh8)) | 0; - mid = (mid + Math.imul(ah5, bl8)) | 0; - hi = (hi + Math.imul(ah5, bh8)) | 0; - lo = (lo + Math.imul(al4, bl9)) | 0; - mid = (mid + Math.imul(al4, bh9)) | 0; - mid = (mid + Math.imul(ah4, bl9)) | 0; - hi = (hi + Math.imul(ah4, bh9)) | 0; - var w13 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w13 >>> 26)) | 0; - w13 &= 0x3ffffff; - /* k = 14 */ - lo = Math.imul(al9, bl5); - mid = Math.imul(al9, bh5); - mid = (mid + Math.imul(ah9, bl5)) | 0; - hi = Math.imul(ah9, bh5); - lo = (lo + Math.imul(al8, bl6)) | 0; - mid = (mid + Math.imul(al8, bh6)) | 0; - mid = (mid + Math.imul(ah8, bl6)) | 0; - hi = (hi + Math.imul(ah8, bh6)) | 0; - lo = (lo + Math.imul(al7, bl7)) | 0; - mid = (mid + Math.imul(al7, bh7)) | 0; - mid = (mid + Math.imul(ah7, bl7)) | 0; - hi = (hi + Math.imul(ah7, bh7)) | 0; - lo = (lo + Math.imul(al6, bl8)) | 0; - mid = (mid + Math.imul(al6, bh8)) | 0; - mid = (mid + Math.imul(ah6, bl8)) | 0; - hi = (hi + Math.imul(ah6, bh8)) | 0; - lo = (lo + Math.imul(al5, bl9)) | 0; - mid = (mid + Math.imul(al5, bh9)) | 0; - mid = (mid + Math.imul(ah5, bl9)) | 0; - hi = (hi + Math.imul(ah5, bh9)) | 0; - var w14 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w14 >>> 26)) | 0; - w14 &= 0x3ffffff; - /* k = 15 */ - lo = Math.imul(al9, bl6); - mid = Math.imul(al9, bh6); - mid = (mid + Math.imul(ah9, bl6)) | 0; - hi = Math.imul(ah9, bh6); - lo = (lo + Math.imul(al8, bl7)) | 0; - mid = (mid + Math.imul(al8, bh7)) | 0; - mid = (mid + Math.imul(ah8, bl7)) | 0; - hi = (hi + Math.imul(ah8, bh7)) | 0; - lo = (lo + Math.imul(al7, bl8)) | 0; - mid = (mid + Math.imul(al7, bh8)) | 0; - mid = (mid + Math.imul(ah7, bl8)) | 0; - hi = (hi + Math.imul(ah7, bh8)) | 0; - lo = (lo + Math.imul(al6, bl9)) | 0; - mid = (mid + Math.imul(al6, bh9)) | 0; - mid = (mid + Math.imul(ah6, bl9)) | 0; - hi = (hi + Math.imul(ah6, bh9)) | 0; - var w15 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w15 >>> 26)) | 0; - w15 &= 0x3ffffff; - /* k = 16 */ - lo = Math.imul(al9, bl7); - mid = Math.imul(al9, bh7); - mid = (mid + Math.imul(ah9, bl7)) | 0; - hi = Math.imul(ah9, bh7); - lo = (lo + Math.imul(al8, bl8)) | 0; - mid = (mid + Math.imul(al8, bh8)) | 0; - mid = (mid + Math.imul(ah8, bl8)) | 0; - hi = (hi + Math.imul(ah8, bh8)) | 0; - lo = (lo + Math.imul(al7, bl9)) | 0; - mid = (mid + Math.imul(al7, bh9)) | 0; - mid = (mid + Math.imul(ah7, bl9)) | 0; - hi = (hi + Math.imul(ah7, bh9)) | 0; - var w16 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w16 >>> 26)) | 0; - w16 &= 0x3ffffff; - /* k = 17 */ - lo = Math.imul(al9, bl8); - mid = Math.imul(al9, bh8); - mid = (mid + Math.imul(ah9, bl8)) | 0; - hi = Math.imul(ah9, bh8); - lo = (lo + Math.imul(al8, bl9)) | 0; - mid = (mid + Math.imul(al8, bh9)) | 0; - mid = (mid + Math.imul(ah8, bl9)) | 0; - hi = (hi + Math.imul(ah8, bh9)) | 0; - var w17 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w17 >>> 26)) | 0; - w17 &= 0x3ffffff; - /* k = 18 */ - lo = Math.imul(al9, bl9); - mid = Math.imul(al9, bh9); - mid = (mid + Math.imul(ah9, bl9)) | 0; - hi = Math.imul(ah9, bh9); - var w18 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; - c = (((hi + (mid >>> 13)) | 0) + (w18 >>> 26)) | 0; - w18 &= 0x3ffffff; - o[0] = w0; - o[1] = w1; - o[2] = w2; - o[3] = w3; - o[4] = w4; - o[5] = w5; - o[6] = w6; - o[7] = w7; - o[8] = w8; - o[9] = w9; - o[10] = w10; - o[11] = w11; - o[12] = w12; - o[13] = w13; - o[14] = w14; - o[15] = w15; - o[16] = w16; - o[17] = w17; - o[18] = w18; - if (c !== 0) { - o[19] = c; - out.length++; - } - return out; - }; - - // Polyfill comb - if (!Math.imul) { - comb10MulTo = smallMulTo; - } - - function bigMulTo (self, num, out) { - out.negative = num.negative ^ self.negative; - out.length = self.length + num.length; - - var carry = 0; - var hncarry = 0; - for (var k = 0; k < out.length - 1; k++) { - // Sum all words with the same `i + j = k` and accumulate `ncarry`, - // note that ncarry could be >= 0x3ffffff - var ncarry = hncarry; - hncarry = 0; - var rword = carry & 0x3ffffff; - var maxJ = Math.min(k, num.length - 1); - for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { - var i = k - j; - var a = self.words[i] | 0; - var b = num.words[j] | 0; - var r = a * b; - - var lo = r & 0x3ffffff; - ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0; - lo = (lo + rword) | 0; - rword = lo & 0x3ffffff; - ncarry = (ncarry + (lo >>> 26)) | 0; - - hncarry += ncarry >>> 26; - ncarry &= 0x3ffffff; - } - out.words[k] = rword; - carry = ncarry; - ncarry = hncarry; - } - if (carry !== 0) { - out.words[k] = carry; - } else { - out.length--; - } - - return out.strip(); - } - - function jumboMulTo (self, num, out) { - var fftm = new FFTM(); - return fftm.mulp(self, num, out); - } - - BN.prototype.mulTo = function mulTo (num, out) { - var res; - var len = this.length + num.length; - if (this.length === 10 && num.length === 10) { - res = comb10MulTo(this, num, out); - } else if (len < 63) { - res = smallMulTo(this, num, out); - } else if (len < 1024) { - res = bigMulTo(this, num, out); - } else { - res = jumboMulTo(this, num, out); - } - - return res; - }; - - // Cooley-Tukey algorithm for FFT - // slightly revisited to rely on looping instead of recursion - - function FFTM (x, y) { - this.x = x; - this.y = y; - } - - FFTM.prototype.makeRBT = function makeRBT (N) { - var t = new Array(N); - var l = BN.prototype._countBits(N) - 1; - for (var i = 0; i < N; i++) { - t[i] = this.revBin(i, l, N); - } - - return t; - }; - - // Returns binary-reversed representation of `x` - FFTM.prototype.revBin = function revBin (x, l, N) { - if (x === 0 || x === N - 1) return x; - - var rb = 0; - for (var i = 0; i < l; i++) { - rb |= (x & 1) << (l - i - 1); - x >>= 1; - } - - return rb; - }; - - // Performs "tweedling" phase, therefore 'emulating' - // behaviour of the recursive algorithm - FFTM.prototype.permute = function permute (rbt, rws, iws, rtws, itws, N) { - for (var i = 0; i < N; i++) { - rtws[i] = rws[rbt[i]]; - itws[i] = iws[rbt[i]]; - } - }; - - FFTM.prototype.transform = function transform (rws, iws, rtws, itws, N, rbt) { - this.permute(rbt, rws, iws, rtws, itws, N); - - for (var s = 1; s < N; s <<= 1) { - var l = s << 1; - - var rtwdf = Math.cos(2 * Math.PI / l); - var itwdf = Math.sin(2 * Math.PI / l); - - for (var p = 0; p < N; p += l) { - var rtwdf_ = rtwdf; - var itwdf_ = itwdf; - - for (var j = 0; j < s; j++) { - var re = rtws[p + j]; - var ie = itws[p + j]; - - var ro = rtws[p + j + s]; - var io = itws[p + j + s]; - - var rx = rtwdf_ * ro - itwdf_ * io; - - io = rtwdf_ * io + itwdf_ * ro; - ro = rx; - - rtws[p + j] = re + ro; - itws[p + j] = ie + io; - - rtws[p + j + s] = re - ro; - itws[p + j + s] = ie - io; - - /* jshint maxdepth : false */ - if (j !== l) { - rx = rtwdf * rtwdf_ - itwdf * itwdf_; - - itwdf_ = rtwdf * itwdf_ + itwdf * rtwdf_; - rtwdf_ = rx; - } - } - } - } - }; - - FFTM.prototype.guessLen13b = function guessLen13b (n, m) { - var N = Math.max(m, n) | 1; - var odd = N & 1; - var i = 0; - for (N = N / 2 | 0; N; N = N >>> 1) { - i++; - } - - return 1 << i + 1 + odd; - }; - - FFTM.prototype.conjugate = function conjugate (rws, iws, N) { - if (N <= 1) return; - - for (var i = 0; i < N / 2; i++) { - var t = rws[i]; - - rws[i] = rws[N - i - 1]; - rws[N - i - 1] = t; - - t = iws[i]; - - iws[i] = -iws[N - i - 1]; - iws[N - i - 1] = -t; - } - }; - - FFTM.prototype.normalize13b = function normalize13b (ws, N) { - var carry = 0; - for (var i = 0; i < N / 2; i++) { - var w = Math.round(ws[2 * i + 1] / N) * 0x2000 + - Math.round(ws[2 * i] / N) + - carry; - - ws[i] = w & 0x3ffffff; - - if (w < 0x4000000) { - carry = 0; - } else { - carry = w / 0x4000000 | 0; - } - } - - return ws; - }; - - FFTM.prototype.convert13b = function convert13b (ws, len, rws, N) { - var carry = 0; - for (var i = 0; i < len; i++) { - carry = carry + (ws[i] | 0); - - rws[2 * i] = carry & 0x1fff; carry = carry >>> 13; - rws[2 * i + 1] = carry & 0x1fff; carry = carry >>> 13; - } - - // Pad with zeroes - for (i = 2 * len; i < N; ++i) { - rws[i] = 0; - } - - assert(carry === 0); - assert((carry & ~0x1fff) === 0); - }; - - FFTM.prototype.stub = function stub (N) { - var ph = new Array(N); - for (var i = 0; i < N; i++) { - ph[i] = 0; - } - - return ph; - }; - - FFTM.prototype.mulp = function mulp (x, y, out) { - var N = 2 * this.guessLen13b(x.length, y.length); - - var rbt = this.makeRBT(N); - - var _ = this.stub(N); - - var rws = new Array(N); - var rwst = new Array(N); - var iwst = new Array(N); - - var nrws = new Array(N); - var nrwst = new Array(N); - var niwst = new Array(N); - - var rmws = out.words; - rmws.length = N; - - this.convert13b(x.words, x.length, rws, N); - this.convert13b(y.words, y.length, nrws, N); - - this.transform(rws, _, rwst, iwst, N, rbt); - this.transform(nrws, _, nrwst, niwst, N, rbt); - - for (var i = 0; i < N; i++) { - var rx = rwst[i] * nrwst[i] - iwst[i] * niwst[i]; - iwst[i] = rwst[i] * niwst[i] + iwst[i] * nrwst[i]; - rwst[i] = rx; - } - - this.conjugate(rwst, iwst, N); - this.transform(rwst, iwst, rmws, _, N, rbt); - this.conjugate(rmws, _, N); - this.normalize13b(rmws, N); - - out.negative = x.negative ^ y.negative; - out.length = x.length + y.length; - return out.strip(); - }; - - // Multiply `this` by `num` - BN.prototype.mul = function mul (num) { - var out = new BN(null); - out.words = new Array(this.length + num.length); - return this.mulTo(num, out); - }; - - // Multiply employing FFT - BN.prototype.mulf = function mulf (num) { - var out = new BN(null); - out.words = new Array(this.length + num.length); - return jumboMulTo(this, num, out); - }; - - // In-place Multiplication - BN.prototype.imul = function imul (num) { - return this.clone().mulTo(num, this); - }; - - BN.prototype.imuln = function imuln (num) { - assert(typeof num === 'number'); - assert(num < 0x4000000); - - // Carry - var carry = 0; - for (var i = 0; i < this.length; i++) { - var w = (this.words[i] | 0) * num; - var lo = (w & 0x3ffffff) + (carry & 0x3ffffff); - carry >>= 26; - carry += (w / 0x4000000) | 0; - // NOTE: lo is 27bit maximum - carry += lo >>> 26; - this.words[i] = lo & 0x3ffffff; - } - - if (carry !== 0) { - this.words[i] = carry; - this.length++; - } - - return this; - }; - - BN.prototype.muln = function muln (num) { - return this.clone().imuln(num); - }; - - // `this` * `this` - BN.prototype.sqr = function sqr () { - return this.mul(this); - }; - - // `this` * `this` in-place - BN.prototype.isqr = function isqr () { - return this.imul(this.clone()); - }; - - // Math.pow(`this`, `num`) - BN.prototype.pow = function pow (num) { - var w = toBitArray(num); - if (w.length === 0) return new BN(1); - - // Skip leading zeroes - var res = this; - for (var i = 0; i < w.length; i++, res = res.sqr()) { - if (w[i] !== 0) break; - } - - if (++i < w.length) { - for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) { - if (w[i] === 0) continue; - - res = res.mul(q); - } - } - - return res; - }; - - // Shift-left in-place - BN.prototype.iushln = function iushln (bits) { - assert(typeof bits === 'number' && bits >= 0); - var r = bits % 26; - var s = (bits - r) / 26; - var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r); - var i; - - if (r !== 0) { - var carry = 0; - - for (i = 0; i < this.length; i++) { - var newCarry = this.words[i] & carryMask; - var c = ((this.words[i] | 0) - newCarry) << r; - this.words[i] = c | carry; - carry = newCarry >>> (26 - r); - } - - if (carry) { - this.words[i] = carry; - this.length++; - } - } - - if (s !== 0) { - for (i = this.length - 1; i >= 0; i--) { - this.words[i + s] = this.words[i]; - } - - for (i = 0; i < s; i++) { - this.words[i] = 0; - } - - this.length += s; - } - - return this.strip(); - }; - - BN.prototype.ishln = function ishln (bits) { - // TODO(indutny): implement me - assert(this.negative === 0); - return this.iushln(bits); - }; - - // Shift-right in-place - // NOTE: `hint` is a lowest bit before trailing zeroes - // NOTE: if `extended` is present - it will be filled with destroyed bits - BN.prototype.iushrn = function iushrn (bits, hint, extended) { - assert(typeof bits === 'number' && bits >= 0); - var h; - if (hint) { - h = (hint - (hint % 26)) / 26; - } else { - h = 0; - } - - var r = bits % 26; - var s = Math.min((bits - r) / 26, this.length); - var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); - var maskedWords = extended; - - h -= s; - h = Math.max(0, h); - - // Extended mode, copy masked part - if (maskedWords) { - for (var i = 0; i < s; i++) { - maskedWords.words[i] = this.words[i]; - } - maskedWords.length = s; - } - - if (s === 0) { - // No-op, we should not move anything at all - } else if (this.length > s) { - this.length -= s; - for (i = 0; i < this.length; i++) { - this.words[i] = this.words[i + s]; - } - } else { - this.words[0] = 0; - this.length = 1; - } - - var carry = 0; - for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) { - var word = this.words[i] | 0; - this.words[i] = (carry << (26 - r)) | (word >>> r); - carry = word & mask; - } - - // Push carried bits as a mask - if (maskedWords && carry !== 0) { - maskedWords.words[maskedWords.length++] = carry; - } - - if (this.length === 0) { - this.words[0] = 0; - this.length = 1; - } - - return this.strip(); - }; - - BN.prototype.ishrn = function ishrn (bits, hint, extended) { - // TODO(indutny): implement me - assert(this.negative === 0); - return this.iushrn(bits, hint, extended); - }; - - // Shift-left - BN.prototype.shln = function shln (bits) { - return this.clone().ishln(bits); - }; - - BN.prototype.ushln = function ushln (bits) { - return this.clone().iushln(bits); - }; - - // Shift-right - BN.prototype.shrn = function shrn (bits) { - return this.clone().ishrn(bits); - }; - - BN.prototype.ushrn = function ushrn (bits) { - return this.clone().iushrn(bits); - }; - - // Test if n bit is set - BN.prototype.testn = function testn (bit) { - assert(typeof bit === 'number' && bit >= 0); - var r = bit % 26; - var s = (bit - r) / 26; - var q = 1 << r; - - // Fast case: bit is much higher than all existing words - if (this.length <= s) return false; - - // Check bit and return - var w = this.words[s]; - - return !!(w & q); - }; - - // Return only lowers bits of number (in-place) - BN.prototype.imaskn = function imaskn (bits) { - assert(typeof bits === 'number' && bits >= 0); - var r = bits % 26; - var s = (bits - r) / 26; - - assert(this.negative === 0, 'imaskn works only with positive numbers'); - - if (this.length <= s) { - return this; - } - - if (r !== 0) { - s++; - } - this.length = Math.min(s, this.length); - - if (r !== 0) { - var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); - this.words[this.length - 1] &= mask; - } - - return this.strip(); - }; - - // Return only lowers bits of number - BN.prototype.maskn = function maskn (bits) { - return this.clone().imaskn(bits); - }; - - // Add plain number `num` to `this` - BN.prototype.iaddn = function iaddn (num) { - assert(typeof num === 'number'); - assert(num < 0x4000000); - if (num < 0) return this.isubn(-num); - - // Possible sign change - if (this.negative !== 0) { - if (this.length === 1 && (this.words[0] | 0) < num) { - this.words[0] = num - (this.words[0] | 0); - this.negative = 0; - return this; - } - - this.negative = 0; - this.isubn(num); - this.negative = 1; - return this; - } - - // Add without checks - return this._iaddn(num); - }; - - BN.prototype._iaddn = function _iaddn (num) { - this.words[0] += num; - - // Carry - for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) { - this.words[i] -= 0x4000000; - if (i === this.length - 1) { - this.words[i + 1] = 1; - } else { - this.words[i + 1]++; - } - } - this.length = Math.max(this.length, i + 1); - - return this; - }; - - // Subtract plain number `num` from `this` - BN.prototype.isubn = function isubn (num) { - assert(typeof num === 'number'); - assert(num < 0x4000000); - if (num < 0) return this.iaddn(-num); - - if (this.negative !== 0) { - this.negative = 0; - this.iaddn(num); - this.negative = 1; - return this; - } - - this.words[0] -= num; - - if (this.length === 1 && this.words[0] < 0) { - this.words[0] = -this.words[0]; - this.negative = 1; - } else { - // Carry - for (var i = 0; i < this.length && this.words[i] < 0; i++) { - this.words[i] += 0x4000000; - this.words[i + 1] -= 1; - } - } - - return this.strip(); - }; - - BN.prototype.addn = function addn (num) { - return this.clone().iaddn(num); - }; - - BN.prototype.subn = function subn (num) { - return this.clone().isubn(num); - }; - - BN.prototype.iabs = function iabs () { - this.negative = 0; - - return this; - }; - - BN.prototype.abs = function abs () { - return this.clone().iabs(); - }; - - BN.prototype._ishlnsubmul = function _ishlnsubmul (num, mul, shift) { - var len = num.length + shift; - var i; - - this._expand(len); - - var w; - var carry = 0; - for (i = 0; i < num.length; i++) { - w = (this.words[i + shift] | 0) + carry; - var right = (num.words[i] | 0) * mul; - w -= right & 0x3ffffff; - carry = (w >> 26) - ((right / 0x4000000) | 0); - this.words[i + shift] = w & 0x3ffffff; - } - for (; i < this.length - shift; i++) { - w = (this.words[i + shift] | 0) + carry; - carry = w >> 26; - this.words[i + shift] = w & 0x3ffffff; - } - - if (carry === 0) return this.strip(); - - // Subtraction overflow - assert(carry === -1); - carry = 0; - for (i = 0; i < this.length; i++) { - w = -(this.words[i] | 0) + carry; - carry = w >> 26; - this.words[i] = w & 0x3ffffff; - } - this.negative = 1; - - return this.strip(); - }; - - BN.prototype._wordDiv = function _wordDiv (num, mode) { - var shift = this.length - num.length; - - var a = this.clone(); - var b = num; - - // Normalize - var bhi = b.words[b.length - 1] | 0; - var bhiBits = this._countBits(bhi); - shift = 26 - bhiBits; - if (shift !== 0) { - b = b.ushln(shift); - a.iushln(shift); - bhi = b.words[b.length - 1] | 0; - } - - // Initialize quotient - var m = a.length - b.length; - var q; - - if (mode !== 'mod') { - q = new BN(null); - q.length = m + 1; - q.words = new Array(q.length); - for (var i = 0; i < q.length; i++) { - q.words[i] = 0; - } - } - - var diff = a.clone()._ishlnsubmul(b, 1, m); - if (diff.negative === 0) { - a = diff; - if (q) { - q.words[m] = 1; - } - } - - for (var j = m - 1; j >= 0; j--) { - var qj = (a.words[b.length + j] | 0) * 0x4000000 + - (a.words[b.length + j - 1] | 0); - - // NOTE: (qj / bhi) is (0x3ffffff * 0x4000000 + 0x3ffffff) / 0x2000000 max - // (0x7ffffff) - qj = Math.min((qj / bhi) | 0, 0x3ffffff); - - a._ishlnsubmul(b, qj, j); - while (a.negative !== 0) { - qj--; - a.negative = 0; - a._ishlnsubmul(b, 1, j); - if (!a.isZero()) { - a.negative ^= 1; - } - } - if (q) { - q.words[j] = qj; - } - } - if (q) { - q.strip(); - } - a.strip(); - - // Denormalize - if (mode !== 'div' && shift !== 0) { - a.iushrn(shift); - } - - return { - div: q || null, - mod: a - }; - }; - - // NOTE: 1) `mode` can be set to `mod` to request mod only, - // to `div` to request div only, or be absent to - // request both div & mod - // 2) `positive` is true if unsigned mod is requested - BN.prototype.divmod = function divmod (num, mode, positive) { - assert(!num.isZero()); - - if (this.isZero()) { - return { - div: new BN(0), - mod: new BN(0) - }; - } - - var div, mod, res; - if (this.negative !== 0 && num.negative === 0) { - res = this.neg().divmod(num, mode); - - if (mode !== 'mod') { - div = res.div.neg(); - } - - if (mode !== 'div') { - mod = res.mod.neg(); - if (positive && mod.negative !== 0) { - mod.iadd(num); - } - } - - return { - div: div, - mod: mod - }; - } - - if (this.negative === 0 && num.negative !== 0) { - res = this.divmod(num.neg(), mode); - - if (mode !== 'mod') { - div = res.div.neg(); - } - - return { - div: div, - mod: res.mod - }; - } - - if ((this.negative & num.negative) !== 0) { - res = this.neg().divmod(num.neg(), mode); - - if (mode !== 'div') { - mod = res.mod.neg(); - if (positive && mod.negative !== 0) { - mod.isub(num); - } - } - - return { - div: res.div, - mod: mod - }; - } - - // Both numbers are positive at this point - - // Strip both numbers to approximate shift value - if (num.length > this.length || this.cmp(num) < 0) { - return { - div: new BN(0), - mod: this - }; - } - - // Very short reduction - if (num.length === 1) { - if (mode === 'div') { - return { - div: this.divn(num.words[0]), - mod: null - }; - } - - if (mode === 'mod') { - return { - div: null, - mod: new BN(this.modn(num.words[0])) - }; - } - - return { - div: this.divn(num.words[0]), - mod: new BN(this.modn(num.words[0])) - }; - } - - return this._wordDiv(num, mode); - }; - - // Find `this` / `num` - BN.prototype.div = function div (num) { - return this.divmod(num, 'div', false).div; - }; - - // Find `this` % `num` - BN.prototype.mod = function mod (num) { - return this.divmod(num, 'mod', false).mod; - }; - - BN.prototype.umod = function umod (num) { - return this.divmod(num, 'mod', true).mod; - }; - - // Find Round(`this` / `num`) - BN.prototype.divRound = function divRound (num) { - var dm = this.divmod(num); - - // Fast case - exact division - if (dm.mod.isZero()) return dm.div; - - var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod; - - var half = num.ushrn(1); - var r2 = num.andln(1); - var cmp = mod.cmp(half); - - // Round down - if (cmp < 0 || r2 === 1 && cmp === 0) return dm.div; - - // Round up - return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1); - }; - - BN.prototype.modn = function modn (num) { - assert(num <= 0x3ffffff); - var p = (1 << 26) % num; - - var acc = 0; - for (var i = this.length - 1; i >= 0; i--) { - acc = (p * acc + (this.words[i] | 0)) % num; - } - - return acc; - }; - - // In-place division by number - BN.prototype.idivn = function idivn (num) { - assert(num <= 0x3ffffff); - - var carry = 0; - for (var i = this.length - 1; i >= 0; i--) { - var w = (this.words[i] | 0) + carry * 0x4000000; - this.words[i] = (w / num) | 0; - carry = w % num; - } - - return this.strip(); - }; - - BN.prototype.divn = function divn (num) { - return this.clone().idivn(num); - }; - - BN.prototype.egcd = function egcd (p) { - assert(p.negative === 0); - assert(!p.isZero()); - - var x = this; - var y = p.clone(); - - if (x.negative !== 0) { - x = x.umod(p); - } else { - x = x.clone(); - } - - // A * x + B * y = x - var A = new BN(1); - var B = new BN(0); - - // C * x + D * y = y - var C = new BN(0); - var D = new BN(1); - - var g = 0; - - while (x.isEven() && y.isEven()) { - x.iushrn(1); - y.iushrn(1); - ++g; - } - - var yp = y.clone(); - var xp = x.clone(); - - while (!x.isZero()) { - for (var i = 0, im = 1; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1); - if (i > 0) { - x.iushrn(i); - while (i-- > 0) { - if (A.isOdd() || B.isOdd()) { - A.iadd(yp); - B.isub(xp); - } - - A.iushrn(1); - B.iushrn(1); - } - } - - for (var j = 0, jm = 1; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1); - if (j > 0) { - y.iushrn(j); - while (j-- > 0) { - if (C.isOdd() || D.isOdd()) { - C.iadd(yp); - D.isub(xp); - } - - C.iushrn(1); - D.iushrn(1); - } - } - - if (x.cmp(y) >= 0) { - x.isub(y); - A.isub(C); - B.isub(D); - } else { - y.isub(x); - C.isub(A); - D.isub(B); - } - } - - return { - a: C, - b: D, - gcd: y.iushln(g) - }; - }; - - // This is reduced incarnation of the binary EEA - // above, designated to invert members of the - // _prime_ fields F(p) at a maximal speed - BN.prototype._invmp = function _invmp (p) { - assert(p.negative === 0); - assert(!p.isZero()); - - var a = this; - var b = p.clone(); - - if (a.negative !== 0) { - a = a.umod(p); - } else { - a = a.clone(); - } - - var x1 = new BN(1); - var x2 = new BN(0); - - var delta = b.clone(); - - while (a.cmpn(1) > 0 && b.cmpn(1) > 0) { - for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1); - if (i > 0) { - a.iushrn(i); - while (i-- > 0) { - if (x1.isOdd()) { - x1.iadd(delta); - } - - x1.iushrn(1); - } - } - - for (var j = 0, jm = 1; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1); - if (j > 0) { - b.iushrn(j); - while (j-- > 0) { - if (x2.isOdd()) { - x2.iadd(delta); - } - - x2.iushrn(1); - } - } - - if (a.cmp(b) >= 0) { - a.isub(b); - x1.isub(x2); - } else { - b.isub(a); - x2.isub(x1); - } - } - - var res; - if (a.cmpn(1) === 0) { - res = x1; - } else { - res = x2; - } - - if (res.cmpn(0) < 0) { - res.iadd(p); - } - - return res; - }; - - BN.prototype.gcd = function gcd (num) { - if (this.isZero()) return num.abs(); - if (num.isZero()) return this.abs(); - - var a = this.clone(); - var b = num.clone(); - a.negative = 0; - b.negative = 0; - - // Remove common factor of two - for (var shift = 0; a.isEven() && b.isEven(); shift++) { - a.iushrn(1); - b.iushrn(1); - } - - do { - while (a.isEven()) { - a.iushrn(1); - } - while (b.isEven()) { - b.iushrn(1); - } - - var r = a.cmp(b); - if (r < 0) { - // Swap `a` and `b` to make `a` always bigger than `b` - var t = a; - a = b; - b = t; - } else if (r === 0 || b.cmpn(1) === 0) { - break; - } - - a.isub(b); - } while (true); - - return b.iushln(shift); - }; - - // Invert number in the field F(num) - BN.prototype.invm = function invm (num) { - return this.egcd(num).a.umod(num); - }; - - BN.prototype.isEven = function isEven () { - return (this.words[0] & 1) === 0; - }; - - BN.prototype.isOdd = function isOdd () { - return (this.words[0] & 1) === 1; - }; - - // And first word and num - BN.prototype.andln = function andln (num) { - return this.words[0] & num; - }; - - // Increment at the bit position in-line - BN.prototype.bincn = function bincn (bit) { - assert(typeof bit === 'number'); - var r = bit % 26; - var s = (bit - r) / 26; - var q = 1 << r; - - // Fast case: bit is much higher than all existing words - if (this.length <= s) { - this._expand(s + 1); - this.words[s] |= q; - return this; - } - - // Add bit and propagate, if needed - var carry = q; - for (var i = s; carry !== 0 && i < this.length; i++) { - var w = this.words[i] | 0; - w += carry; - carry = w >>> 26; - w &= 0x3ffffff; - this.words[i] = w; - } - if (carry !== 0) { - this.words[i] = carry; - this.length++; - } - return this; - }; - - BN.prototype.isZero = function isZero () { - return this.length === 1 && this.words[0] === 0; - }; - - BN.prototype.cmpn = function cmpn (num) { - var negative = num < 0; - - if (this.negative !== 0 && !negative) return -1; - if (this.negative === 0 && negative) return 1; - - this.strip(); - - var res; - if (this.length > 1) { - res = 1; - } else { - if (negative) { - num = -num; - } - - assert(num <= 0x3ffffff, 'Number is too big'); - - var w = this.words[0] | 0; - res = w === num ? 0 : w < num ? -1 : 1; - } - if (this.negative !== 0) return -res | 0; - return res; - }; - - // Compare two numbers and return: - // 1 - if `this` > `num` - // 0 - if `this` == `num` - // -1 - if `this` < `num` - BN.prototype.cmp = function cmp (num) { - if (this.negative !== 0 && num.negative === 0) return -1; - if (this.negative === 0 && num.negative !== 0) return 1; - - var res = this.ucmp(num); - if (this.negative !== 0) return -res | 0; - return res; - }; - - // Unsigned comparison - BN.prototype.ucmp = function ucmp (num) { - // At this point both numbers have the same sign - if (this.length > num.length) return 1; - if (this.length < num.length) return -1; - - var res = 0; - for (var i = this.length - 1; i >= 0; i--) { - var a = this.words[i] | 0; - var b = num.words[i] | 0; - - if (a === b) continue; - if (a < b) { - res = -1; - } else if (a > b) { - res = 1; - } - break; - } - return res; - }; - - BN.prototype.gtn = function gtn (num) { - return this.cmpn(num) === 1; - }; - - BN.prototype.gt = function gt (num) { - return this.cmp(num) === 1; - }; - - BN.prototype.gten = function gten (num) { - return this.cmpn(num) >= 0; - }; - - BN.prototype.gte = function gte (num) { - return this.cmp(num) >= 0; - }; - - BN.prototype.ltn = function ltn (num) { - return this.cmpn(num) === -1; - }; - - BN.prototype.lt = function lt (num) { - return this.cmp(num) === -1; - }; - - BN.prototype.lten = function lten (num) { - return this.cmpn(num) <= 0; - }; - - BN.prototype.lte = function lte (num) { - return this.cmp(num) <= 0; - }; - - BN.prototype.eqn = function eqn (num) { - return this.cmpn(num) === 0; - }; - - BN.prototype.eq = function eq (num) { - return this.cmp(num) === 0; - }; - - // - // A reduce context, could be using montgomery or something better, depending - // on the `m` itself. - // - BN.red = function red (num) { - return new Red(num); - }; - - BN.prototype.toRed = function toRed (ctx) { - assert(!this.red, 'Already a number in reduction context'); - assert(this.negative === 0, 'red works only with positives'); - return ctx.convertTo(this)._forceRed(ctx); - }; - - BN.prototype.fromRed = function fromRed () { - assert(this.red, 'fromRed works only with numbers in reduction context'); - return this.red.convertFrom(this); - }; - - BN.prototype._forceRed = function _forceRed (ctx) { - this.red = ctx; - return this; - }; - - BN.prototype.forceRed = function forceRed (ctx) { - assert(!this.red, 'Already a number in reduction context'); - return this._forceRed(ctx); - }; - - BN.prototype.redAdd = function redAdd (num) { - assert(this.red, 'redAdd works only with red numbers'); - return this.red.add(this, num); - }; - - BN.prototype.redIAdd = function redIAdd (num) { - assert(this.red, 'redIAdd works only with red numbers'); - return this.red.iadd(this, num); - }; - - BN.prototype.redSub = function redSub (num) { - assert(this.red, 'redSub works only with red numbers'); - return this.red.sub(this, num); - }; - - BN.prototype.redISub = function redISub (num) { - assert(this.red, 'redISub works only with red numbers'); - return this.red.isub(this, num); - }; - - BN.prototype.redShl = function redShl (num) { - assert(this.red, 'redShl works only with red numbers'); - return this.red.shl(this, num); - }; - - BN.prototype.redMul = function redMul (num) { - assert(this.red, 'redMul works only with red numbers'); - this.red._verify2(this, num); - return this.red.mul(this, num); - }; - - BN.prototype.redIMul = function redIMul (num) { - assert(this.red, 'redMul works only with red numbers'); - this.red._verify2(this, num); - return this.red.imul(this, num); - }; - - BN.prototype.redSqr = function redSqr () { - assert(this.red, 'redSqr works only with red numbers'); - this.red._verify1(this); - return this.red.sqr(this); - }; - - BN.prototype.redISqr = function redISqr () { - assert(this.red, 'redISqr works only with red numbers'); - this.red._verify1(this); - return this.red.isqr(this); - }; - - // Square root over p - BN.prototype.redSqrt = function redSqrt () { - assert(this.red, 'redSqrt works only with red numbers'); - this.red._verify1(this); - return this.red.sqrt(this); - }; - - BN.prototype.redInvm = function redInvm () { - assert(this.red, 'redInvm works only with red numbers'); - this.red._verify1(this); - return this.red.invm(this); - }; - - // Return negative clone of `this` % `red modulo` - BN.prototype.redNeg = function redNeg () { - assert(this.red, 'redNeg works only with red numbers'); - this.red._verify1(this); - return this.red.neg(this); - }; - - BN.prototype.redPow = function redPow (num) { - assert(this.red && !num.red, 'redPow(normalNum)'); - this.red._verify1(this); - return this.red.pow(this, num); - }; - - // Prime numbers with efficient reduction - var primes = { - k256: null, - p224: null, - p192: null, - p25519: null - }; - - // Pseudo-Mersenne prime - function MPrime (name, p) { - // P = 2 ^ N - K - this.name = name; - this.p = new BN(p, 16); - this.n = this.p.bitLength(); - this.k = new BN(1).iushln(this.n).isub(this.p); - - this.tmp = this._tmp(); - } - - MPrime.prototype._tmp = function _tmp () { - var tmp = new BN(null); - tmp.words = new Array(Math.ceil(this.n / 13)); - return tmp; - }; - - MPrime.prototype.ireduce = function ireduce (num) { - // Assumes that `num` is less than `P^2` - // num = HI * (2 ^ N - K) + HI * K + LO = HI * K + LO (mod P) - var r = num; - var rlen; - - do { - this.split(r, this.tmp); - r = this.imulK(r); - r = r.iadd(this.tmp); - rlen = r.bitLength(); - } while (rlen > this.n); - - var cmp = rlen < this.n ? -1 : r.ucmp(this.p); - if (cmp === 0) { - r.words[0] = 0; - r.length = 1; - } else if (cmp > 0) { - r.isub(this.p); - } else { - r.strip(); - } - - return r; - }; - - MPrime.prototype.split = function split (input, out) { - input.iushrn(this.n, 0, out); - }; - - MPrime.prototype.imulK = function imulK (num) { - return num.imul(this.k); - }; - - function K256 () { - MPrime.call( - this, - 'k256', - 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f'); - } - inherits(K256, MPrime); - - K256.prototype.split = function split (input, output) { - // 256 = 9 * 26 + 22 - var mask = 0x3fffff; - - var outLen = Math.min(input.length, 9); - for (var i = 0; i < outLen; i++) { - output.words[i] = input.words[i]; - } - output.length = outLen; - - if (input.length <= 9) { - input.words[0] = 0; - input.length = 1; - return; - } - - // Shift by 9 limbs - var prev = input.words[9]; - output.words[output.length++] = prev & mask; - - for (i = 10; i < input.length; i++) { - var next = input.words[i] | 0; - input.words[i - 10] = ((next & mask) << 4) | (prev >>> 22); - prev = next; - } - prev >>>= 22; - input.words[i - 10] = prev; - if (prev === 0 && input.length > 10) { - input.length -= 10; - } else { - input.length -= 9; - } - }; - - K256.prototype.imulK = function imulK (num) { - // K = 0x1000003d1 = [ 0x40, 0x3d1 ] - num.words[num.length] = 0; - num.words[num.length + 1] = 0; - num.length += 2; - - // bounded at: 0x40 * 0x3ffffff + 0x3d0 = 0x100000390 - var lo = 0; - for (var i = 0; i < num.length; i++) { - var w = num.words[i] | 0; - lo += w * 0x3d1; - num.words[i] = lo & 0x3ffffff; - lo = w * 0x40 + ((lo / 0x4000000) | 0); - } - - // Fast length reduction - if (num.words[num.length - 1] === 0) { - num.length--; - if (num.words[num.length - 1] === 0) { - num.length--; - } - } - return num; - }; - - function P224 () { - MPrime.call( - this, - 'p224', - 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001'); - } - inherits(P224, MPrime); - - function P192 () { - MPrime.call( - this, - 'p192', - 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff'); - } - inherits(P192, MPrime); - - function P25519 () { - // 2 ^ 255 - 19 - MPrime.call( - this, - '25519', - '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed'); - } - inherits(P25519, MPrime); - - P25519.prototype.imulK = function imulK (num) { - // K = 0x13 - var carry = 0; - for (var i = 0; i < num.length; i++) { - var hi = (num.words[i] | 0) * 0x13 + carry; - var lo = hi & 0x3ffffff; - hi >>>= 26; - - num.words[i] = lo; - carry = hi; - } - if (carry !== 0) { - num.words[num.length++] = carry; - } - return num; - }; - - // Exported mostly for testing purposes, use plain name instead - BN._prime = function prime (name) { - // Cached version of prime - if (primes[name]) return primes[name]; - - var prime; - if (name === 'k256') { - prime = new K256(); - } else if (name === 'p224') { - prime = new P224(); - } else if (name === 'p192') { - prime = new P192(); - } else if (name === 'p25519') { - prime = new P25519(); - } else { - throw new Error('Unknown prime ' + name); - } - primes[name] = prime; - - return prime; - }; - - // - // Base reduction engine - // - function Red (m) { - if (typeof m === 'string') { - var prime = BN._prime(m); - this.m = prime.p; - this.prime = prime; - } else { - assert(m.gtn(1), 'modulus must be greater than 1'); - this.m = m; - this.prime = null; - } - } - - Red.prototype._verify1 = function _verify1 (a) { - assert(a.negative === 0, 'red works only with positives'); - assert(a.red, 'red works only with red numbers'); - }; - - Red.prototype._verify2 = function _verify2 (a, b) { - assert((a.negative | b.negative) === 0, 'red works only with positives'); - assert(a.red && a.red === b.red, - 'red works only with red numbers'); - }; - - Red.prototype.imod = function imod (a) { - if (this.prime) return this.prime.ireduce(a)._forceRed(this); - return a.umod(this.m)._forceRed(this); - }; - - Red.prototype.neg = function neg (a) { - if (a.isZero()) { - return a.clone(); - } - - return this.m.sub(a)._forceRed(this); - }; - - Red.prototype.add = function add (a, b) { - this._verify2(a, b); - - var res = a.add(b); - if (res.cmp(this.m) >= 0) { - res.isub(this.m); - } - return res._forceRed(this); - }; - - Red.prototype.iadd = function iadd (a, b) { - this._verify2(a, b); - - var res = a.iadd(b); - if (res.cmp(this.m) >= 0) { - res.isub(this.m); - } - return res; - }; - - Red.prototype.sub = function sub (a, b) { - this._verify2(a, b); - - var res = a.sub(b); - if (res.cmpn(0) < 0) { - res.iadd(this.m); - } - return res._forceRed(this); - }; - - Red.prototype.isub = function isub (a, b) { - this._verify2(a, b); - - var res = a.isub(b); - if (res.cmpn(0) < 0) { - res.iadd(this.m); - } - return res; - }; - - Red.prototype.shl = function shl (a, num) { - this._verify1(a); - return this.imod(a.ushln(num)); - }; - - Red.prototype.imul = function imul (a, b) { - this._verify2(a, b); - return this.imod(a.imul(b)); - }; - - Red.prototype.mul = function mul (a, b) { - this._verify2(a, b); - return this.imod(a.mul(b)); - }; - - Red.prototype.isqr = function isqr (a) { - return this.imul(a, a.clone()); - }; - - Red.prototype.sqr = function sqr (a) { - return this.mul(a, a); - }; - - Red.prototype.sqrt = function sqrt (a) { - if (a.isZero()) return a.clone(); - - var mod3 = this.m.andln(3); - assert(mod3 % 2 === 1); - - // Fast case - if (mod3 === 3) { - var pow = this.m.add(new BN(1)).iushrn(2); - return this.pow(a, pow); - } - - // Tonelli-Shanks algorithm (Totally unoptimized and slow) - // - // Find Q and S, that Q * 2 ^ S = (P - 1) - var q = this.m.subn(1); - var s = 0; - while (!q.isZero() && q.andln(1) === 0) { - s++; - q.iushrn(1); - } - assert(!q.isZero()); - - var one = new BN(1).toRed(this); - var nOne = one.redNeg(); - - // Find quadratic non-residue - // NOTE: Max is such because of generalized Riemann hypothesis. - var lpow = this.m.subn(1).iushrn(1); - var z = this.m.bitLength(); - z = new BN(2 * z * z).toRed(this); - - while (this.pow(z, lpow).cmp(nOne) !== 0) { - z.redIAdd(nOne); - } - - var c = this.pow(z, q); - var r = this.pow(a, q.addn(1).iushrn(1)); - var t = this.pow(a, q); - var m = s; - while (t.cmp(one) !== 0) { - var tmp = t; - for (var i = 0; tmp.cmp(one) !== 0; i++) { - tmp = tmp.redSqr(); - } - assert(i < m); - var b = this.pow(c, new BN(1).iushln(m - i - 1)); - - r = r.redMul(b); - c = b.redSqr(); - t = t.redMul(c); - m = i; - } - - return r; - }; - - Red.prototype.invm = function invm (a) { - var inv = a._invmp(this.m); - if (inv.negative !== 0) { - inv.negative = 0; - return this.imod(inv).redNeg(); - } else { - return this.imod(inv); - } - }; - - Red.prototype.pow = function pow (a, num) { - if (num.isZero()) return new BN(1).toRed(this); - if (num.cmpn(1) === 0) return a.clone(); - - var windowSize = 4; - var wnd = new Array(1 << windowSize); - wnd[0] = new BN(1).toRed(this); - wnd[1] = a; - for (var i = 2; i < wnd.length; i++) { - wnd[i] = this.mul(wnd[i - 1], a); - } - - var res = wnd[0]; - var current = 0; - var currentLen = 0; - var start = num.bitLength() % 26; - if (start === 0) { - start = 26; - } - - for (i = num.length - 1; i >= 0; i--) { - var word = num.words[i]; - for (var j = start - 1; j >= 0; j--) { - var bit = (word >> j) & 1; - if (res !== wnd[0]) { - res = this.sqr(res); - } - - if (bit === 0 && current === 0) { - currentLen = 0; - continue; - } - - current <<= 1; - current |= bit; - currentLen++; - if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue; - - res = this.mul(res, wnd[current]); - currentLen = 0; - current = 0; - } - start = 26; - } - - return res; - }; - - Red.prototype.convertTo = function convertTo (num) { - var r = num.umod(this.m); - - return r === num ? r.clone() : r; - }; - - Red.prototype.convertFrom = function convertFrom (num) { - var res = num.clone(); - res.red = null; - return res; - }; - - // - // Montgomery method engine - // - - BN.mont = function mont (num) { - return new Mont(num); - }; - - function Mont (m) { - Red.call(this, m); - - this.shift = this.m.bitLength(); - if (this.shift % 26 !== 0) { - this.shift += 26 - (this.shift % 26); - } - - this.r = new BN(1).iushln(this.shift); - this.r2 = this.imod(this.r.sqr()); - this.rinv = this.r._invmp(this.m); - - this.minv = this.rinv.mul(this.r).isubn(1).div(this.m); - this.minv = this.minv.umod(this.r); - this.minv = this.r.sub(this.minv); - } - inherits(Mont, Red); - - Mont.prototype.convertTo = function convertTo (num) { - return this.imod(num.ushln(this.shift)); - }; - - Mont.prototype.convertFrom = function convertFrom (num) { - var r = this.imod(num.mul(this.rinv)); - r.red = null; - return r; - }; - - Mont.prototype.imul = function imul (a, b) { - if (a.isZero() || b.isZero()) { - a.words[0] = 0; - a.length = 1; - return a; - } - - var t = a.imul(b); - var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); - var u = t.isub(c).iushrn(this.shift); - var res = u; - - if (u.cmp(this.m) >= 0) { - res = u.isub(this.m); - } else if (u.cmpn(0) < 0) { - res = u.iadd(this.m); - } - - return res._forceRed(this); - }; - - Mont.prototype.mul = function mul (a, b) { - if (a.isZero() || b.isZero()) return new BN(0)._forceRed(this); - - var t = a.mul(b); - var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); - var u = t.isub(c).iushrn(this.shift); - var res = u; - if (u.cmp(this.m) >= 0) { - res = u.isub(this.m); - } else if (u.cmpn(0) < 0) { - res = u.iadd(this.m); - } - - return res._forceRed(this); - }; - - Mont.prototype.invm = function invm (a) { - // (AR)^-1 * R^2 = (A^-1 * R^-1) * R^2 = A^-1 * R - var res = this.imod(a._invmp(this.m).mul(this.r2)); - return res._forceRed(this); - }; -})(typeof module === 'undefined' || module, this); diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/brorand.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/brorand.js deleted file mode 100644 index 0d6a339..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/brorand.js +++ /dev/null @@ -1,52 +0,0 @@ -var r; - -module.exports = function rand(len) { - if (!r) - r = new Rand(null); - - return r.generate(len); -}; - -function Rand(rand) { - this.rand = rand; -} -module.exports.Rand = Rand; - -Rand.prototype.generate = function generate(len) { - return this._rand(len); -}; - -// Emulate crypto API using randy -Rand.prototype._rand = function _rand(n) { - console.log(this); - if (this.rand.getBytes) - return this.rand.getBytes(n); - - var res = new Uint8Array(n); - for (var i = 0; i < res.length; i++) - res[i] = this.rand.getByte(); - return res; -}; - -if (typeof self === 'object') { - Rand.prototype._rand = function _rand(n) { - var list = []; - for (var i = 0; i < n; i++) { - list.push(Math.ceil(Math.random() * 255)) - } - var arr = new Uint8Array(list); - return arr; - }; -} else { - // Node.js or Web worker with no crypto support - try { - var crypto = require('crypto'); - if (typeof crypto.randomBytes !== 'function') - throw new Error('Not supported'); - - Rand.prototype._rand = function _rand(n) { - return crypto.randomBytes(n); - }; - } catch (e) { - } -} diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/buffer.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/buffer.js deleted file mode 100644 index c1719db..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/buffer.js +++ /dev/null @@ -1,1777 +0,0 @@ -/*! - * The buffer module from node.js, for the browser. - * - * @author Feross Aboukhadijeh - * @license MIT - */ -/* eslint-disable no-proto */ - -'use strict' - -var base64 = require('base64-js') -var ieee754 = require('ieee754') - -exports.Buffer = Buffer -exports.SlowBuffer = SlowBuffer -exports.INSPECT_MAX_BYTES = 50 - -var K_MAX_LENGTH = 0x7fffffff -exports.kMaxLength = K_MAX_LENGTH - -/** - * If `Buffer.TYPED_ARRAY_SUPPORT`: - * === true Use Uint8Array implementation (fastest) - * === false Print warning and recommend using `buffer` v4.x which has an Object - * implementation (most compatible, even IE6) - * - * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, - * Opera 11.6+, iOS 4.2+. - * - * We report that the browser does not support typed arrays if the are not subclassable - * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array` - * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support - * for __proto__ and has a buggy typed array implementation. - */ -Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport() - -if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' && - typeof console.error === 'function') { - console.error( - 'This browser lacks typed array (Uint8Array) support which is required by ' + - '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.' - ) -} - -function typedArraySupport () { - // Can typed array instances can be augmented? - try { - var arr = new Uint8Array(1) - arr.__proto__ = { __proto__: Uint8Array.prototype, foo: function () { return 42 } } - return arr.foo() === 42 - } catch (e) { - return false - } -} - -Object.defineProperty(Buffer.prototype, 'parent', { - enumerable: true, - get: function () { - if (!Buffer.isBuffer(this)) return undefined - return this.buffer - } -}) - -Object.defineProperty(Buffer.prototype, 'offset', { - enumerable: true, - get: function () { - if (!Buffer.isBuffer(this)) return undefined - return this.byteOffset - } -}) - -function createBuffer (length) { - if (length > K_MAX_LENGTH) { - throw new RangeError('The value "' + length + '" is invalid for option "size"') - } - // Return an augmented `Uint8Array` instance - var buf = new Uint8Array(length) - buf.__proto__ = Buffer.prototype - return buf -} - -/** - * The Buffer constructor returns instances of `Uint8Array` that have their - * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of - * `Uint8Array`, so the returned instances will have all the node `Buffer` methods - * and the `Uint8Array` methods. Square bracket notation works as expected -- it - * returns a single octet. - * - * The `Uint8Array` prototype remains unmodified. - */ - -function Buffer (arg, encodingOrOffset, length) { - // Common case. - if (typeof arg === 'number') { - if (typeof encodingOrOffset === 'string') { - throw new TypeError( - 'The "string" argument must be of type string. Received type number' - ) - } - return allocUnsafe(arg) - } - return from(arg, encodingOrOffset, length) -} - -// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 -if (typeof Symbol !== 'undefined' && Symbol.species != null && - Buffer[Symbol.species] === Buffer) { - Object.defineProperty(Buffer, Symbol.species, { - value: null, - configurable: true, - enumerable: false, - writable: false - }) -} - -Buffer.poolSize = 8192 // not used by this implementation - -function from (value, encodingOrOffset, length) { - if (typeof value === 'string') { - return fromString(value, encodingOrOffset) - } - - if (ArrayBuffer.isView(value)) { - return fromArrayLike(value) - } - - if (value == null) { - throw TypeError( - 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' + - 'or Array-like Object. Received type ' + (typeof value) - ) - } - - if (isInstance(value, ArrayBuffer) || - (value && isInstance(value.buffer, ArrayBuffer))) { - return fromArrayBuffer(value, encodingOrOffset, length) - } - - if (typeof value === 'number') { - throw new TypeError( - 'The "value" argument must not be of type number. Received type number' - ) - } - - var valueOf = value.valueOf && value.valueOf() - if (valueOf != null && valueOf !== value) { - return Buffer.from(valueOf, encodingOrOffset, length) - } - - var b = fromObject(value) - if (b) return b - - if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null && - typeof value[Symbol.toPrimitive] === 'function') { - return Buffer.from( - value[Symbol.toPrimitive]('string'), encodingOrOffset, length - ) - } - - throw new TypeError( - 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' + - 'or Array-like Object. Received type ' + (typeof value) - ) -} - -/** - * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError - * if value is a number. - * Buffer.from(str[, encoding]) - * Buffer.from(array) - * Buffer.from(buffer) - * Buffer.from(arrayBuffer[, byteOffset[, length]]) - **/ -Buffer.from = function (value, encodingOrOffset, length) { - return from(value, encodingOrOffset, length) -} - -// Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug: -// https://github.com/feross/buffer/pull/148 -Buffer.prototype.__proto__ = Uint8Array.prototype -Buffer.__proto__ = Uint8Array - -function assertSize (size) { - if (typeof size !== 'number') { - throw new TypeError('"size" argument must be of type number') - } else if (size < 0) { - throw new RangeError('The value "' + size + '" is invalid for option "size"') - } -} - -function alloc (size, fill, encoding) { - assertSize(size) - if (size <= 0) { - return createBuffer(size) - } - if (fill !== undefined) { - // Only pay attention to encoding if it's a string. This - // prevents accidentally sending in a number that would - // be interpretted as a start offset. - return typeof encoding === 'string' - ? createBuffer(size).fill(fill, encoding) - : createBuffer(size).fill(fill) - } - return createBuffer(size) -} - -/** - * Creates a new filled Buffer instance. - * alloc(size[, fill[, encoding]]) - **/ -Buffer.alloc = function (size, fill, encoding) { - return alloc(size, fill, encoding) -} - -function allocUnsafe (size) { - assertSize(size) - return createBuffer(size < 0 ? 0 : checked(size) | 0) -} - -/** - * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance. - * */ -Buffer.allocUnsafe = function (size) { - return allocUnsafe(size) -} -/** - * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance. - */ -Buffer.allocUnsafeSlow = function (size) { - return allocUnsafe(size) -} - -function fromString (string, encoding) { - if (typeof encoding !== 'string' || encoding === '') { - encoding = 'utf8' - } - - if (!Buffer.isEncoding(encoding)) { - throw new TypeError('Unknown encoding: ' + encoding) - } - - var length = byteLength(string, encoding) | 0 - var buf = createBuffer(length) - - var actual = buf.write(string, encoding) - - if (actual !== length) { - // Writing a hex string, for example, that contains invalid characters will - // cause everything after the first invalid character to be ignored. (e.g. - // 'abxxcd' will be treated as 'ab') - buf = buf.slice(0, actual) - } - - return buf -} - -function fromArrayLike (array) { - var length = array.length < 0 ? 0 : checked(array.length) | 0 - var buf = createBuffer(length) - for (var i = 0; i < length; i += 1) { - buf[i] = array[i] & 255 - } - return buf -} - -function fromArrayBuffer (array, byteOffset, length) { - if (byteOffset < 0 || array.byteLength < byteOffset) { - throw new RangeError('"offset" is outside of buffer bounds') - } - - if (array.byteLength < byteOffset + (length || 0)) { - throw new RangeError('"length" is outside of buffer bounds') - } - - var buf - if (byteOffset === undefined && length === undefined) { - buf = new Uint8Array(array) - } else if (length === undefined) { - buf = new Uint8Array(array, byteOffset) - } else { - buf = new Uint8Array(array, byteOffset, length) - } - - // Return an augmented `Uint8Array` instance - buf.__proto__ = Buffer.prototype - return buf -} - -function fromObject (obj) { - if (Buffer.isBuffer(obj)) { - var len = checked(obj.length) | 0 - var buf = createBuffer(len) - - if (buf.length === 0) { - return buf - } - - obj.copy(buf, 0, 0, len) - return buf - } - - if (obj.length !== undefined) { - if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) { - return createBuffer(0) - } - return fromArrayLike(obj) - } - - if (obj.type === 'Buffer' && Array.isArray(obj.data)) { - return fromArrayLike(obj.data) - } -} - -function checked (length) { - // Note: cannot use `length < K_MAX_LENGTH` here because that fails when - // length is NaN (which is otherwise coerced to zero.) - if (length >= K_MAX_LENGTH) { - throw new RangeError('Attempt to allocate Buffer larger than maximum ' + - 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes') - } - return length | 0 -} - -function SlowBuffer (length) { - if (+length != length) { // eslint-disable-line eqeqeq - length = 0 - } - return Buffer.alloc(+length) -} - -Buffer.isBuffer = function isBuffer (b) { - return b != null && b._isBuffer === true && - b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false -} - -Buffer.compare = function compare (a, b) { - if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength) - if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength) - if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { - throw new TypeError( - 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array' - ) - } - - if (a === b) return 0 - - var x = a.length - var y = b.length - - for (var i = 0, len = Math.min(x, y); i < len; ++i) { - if (a[i] !== b[i]) { - x = a[i] - y = b[i] - break - } - } - - if (x < y) return -1 - if (y < x) return 1 - return 0 -} - -Buffer.isEncoding = function isEncoding (encoding) { - switch (String(encoding).toLowerCase()) { - case 'hex': - case 'utf8': - case 'utf-8': - case 'ascii': - case 'latin1': - case 'binary': - case 'base64': - case 'ucs2': - case 'ucs-2': - case 'utf16le': - case 'utf-16le': - return true - default: - return false - } -} - -Buffer.concat = function concat (list, length) { - if (!Array.isArray(list)) { - throw new TypeError('"list" argument must be an Array of Buffers') - } - - if (list.length === 0) { - return Buffer.alloc(0) - } - - var i - if (length === undefined) { - length = 0 - for (i = 0; i < list.length; ++i) { - length += list[i].length - } - } - - var buffer = Buffer.allocUnsafe(length) - var pos = 0 - for (i = 0; i < list.length; ++i) { - var buf = list[i] - if (isInstance(buf, Uint8Array)) { - buf = Buffer.from(buf) - } - if (!Buffer.isBuffer(buf)) { - throw new TypeError('"list" argument must be an Array of Buffers') - } - buf.copy(buffer, pos) - pos += buf.length - } - return buffer -} - -function byteLength (string, encoding) { - if (Buffer.isBuffer(string)) { - return string.length - } - if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) { - return string.byteLength - } - if (typeof string !== 'string') { - throw new TypeError( - 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' + - 'Received type ' + typeof string - ) - } - - var len = string.length - var mustMatch = (arguments.length > 2 && arguments[2] === true) - if (!mustMatch && len === 0) return 0 - - // Use a for loop to avoid recursion - var loweredCase = false - for (;;) { - switch (encoding) { - case 'ascii': - case 'latin1': - case 'binary': - return len - case 'utf8': - case 'utf-8': - return utf8ToBytes(string).length - case 'ucs2': - case 'ucs-2': - case 'utf16le': - case 'utf-16le': - return len * 2 - case 'hex': - return len >>> 1 - case 'base64': - return base64ToBytes(string).length - default: - if (loweredCase) { - return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8 - } - encoding = ('' + encoding).toLowerCase() - loweredCase = true - } - } -} -Buffer.byteLength = byteLength - -function slowToString (encoding, start, end) { - var loweredCase = false - - // No need to verify that "this.length <= MAX_UINT32" since it's a read-only - // property of a typed array. - - // This behaves neither like String nor Uint8Array in that we set start/end - // to their upper/lower bounds if the value passed is out of range. - // undefined is handled specially as per ECMA-262 6th Edition, - // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization. - if (start === undefined || start < 0) { - start = 0 - } - // Return early if start > this.length. Done here to prevent potential uint32 - // coercion fail below. - if (start > this.length) { - return '' - } - - if (end === undefined || end > this.length) { - end = this.length - } - - if (end <= 0) { - return '' - } - - // Force coersion to uint32. This will also coerce falsey/NaN values to 0. - end >>>= 0 - start >>>= 0 - - if (end <= start) { - return '' - } - - if (!encoding) encoding = 'utf8' - - while (true) { - switch (encoding) { - case 'hex': - return hexSlice(this, start, end) - - case 'utf8': - case 'utf-8': - return utf8Slice(this, start, end) - - case 'ascii': - return asciiSlice(this, start, end) - - case 'latin1': - case 'binary': - return latin1Slice(this, start, end) - - case 'base64': - return base64Slice(this, start, end) - - case 'ucs2': - case 'ucs-2': - case 'utf16le': - case 'utf-16le': - return utf16leSlice(this, start, end) - - default: - if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) - encoding = (encoding + '').toLowerCase() - loweredCase = true - } - } -} - -// This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package) -// to detect a Buffer instance. It's not possible to use `instanceof Buffer` -// reliably in a browserify context because there could be multiple different -// copies of the 'buffer' package in use. This method works even for Buffer -// instances that were created from another copy of the `buffer` package. -// See: https://github.com/feross/buffer/issues/154 -Buffer.prototype._isBuffer = true - -function swap (b, n, m) { - var i = b[n] - b[n] = b[m] - b[m] = i -} - -Buffer.prototype.swap16 = function swap16 () { - var len = this.length - if (len % 2 !== 0) { - throw new RangeError('Buffer size must be a multiple of 16-bits') - } - for (var i = 0; i < len; i += 2) { - swap(this, i, i + 1) - } - return this -} - -Buffer.prototype.swap32 = function swap32 () { - var len = this.length - if (len % 4 !== 0) { - throw new RangeError('Buffer size must be a multiple of 32-bits') - } - for (var i = 0; i < len; i += 4) { - swap(this, i, i + 3) - swap(this, i + 1, i + 2) - } - return this -} - -Buffer.prototype.swap64 = function swap64 () { - var len = this.length - if (len % 8 !== 0) { - throw new RangeError('Buffer size must be a multiple of 64-bits') - } - for (var i = 0; i < len; i += 8) { - swap(this, i, i + 7) - swap(this, i + 1, i + 6) - swap(this, i + 2, i + 5) - swap(this, i + 3, i + 4) - } - return this -} - -Buffer.prototype.toString = function toString () { - var length = this.length - if (length === 0) return '' - if (arguments.length === 0) return utf8Slice(this, 0, length) - return slowToString.apply(this, arguments) -} - -Buffer.prototype.toLocaleString = Buffer.prototype.toString - -Buffer.prototype.equals = function equals (b) { - if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') - if (this === b) return true - return Buffer.compare(this, b) === 0 -} - -Buffer.prototype.inspect = function inspect () { - var str = '' - var max = exports.INSPECT_MAX_BYTES - str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim() - if (this.length > max) str += ' ... ' - return '' -} - -Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) { - if (isInstance(target, Uint8Array)) { - target = Buffer.from(target, target.offset, target.byteLength) - } - if (!Buffer.isBuffer(target)) { - throw new TypeError( - 'The "target" argument must be one of type Buffer or Uint8Array. ' + - 'Received type ' + (typeof target) - ) - } - - if (start === undefined) { - start = 0 - } - if (end === undefined) { - end = target ? target.length : 0 - } - if (thisStart === undefined) { - thisStart = 0 - } - if (thisEnd === undefined) { - thisEnd = this.length - } - - if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) { - throw new RangeError('out of range index') - } - - if (thisStart >= thisEnd && start >= end) { - return 0 - } - if (thisStart >= thisEnd) { - return -1 - } - if (start >= end) { - return 1 - } - - start >>>= 0 - end >>>= 0 - thisStart >>>= 0 - thisEnd >>>= 0 - - if (this === target) return 0 - - var x = thisEnd - thisStart - var y = end - start - var len = Math.min(x, y) - - var thisCopy = this.slice(thisStart, thisEnd) - var targetCopy = target.slice(start, end) - - for (var i = 0; i < len; ++i) { - if (thisCopy[i] !== targetCopy[i]) { - x = thisCopy[i] - y = targetCopy[i] - break - } - } - - if (x < y) return -1 - if (y < x) return 1 - return 0 -} - -// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`, -// OR the last index of `val` in `buffer` at offset <= `byteOffset`. -// -// Arguments: -// - buffer - a Buffer to search -// - val - a string, Buffer, or number -// - byteOffset - an index into `buffer`; will be clamped to an int32 -// - encoding - an optional encoding, relevant is val is a string -// - dir - true for indexOf, false for lastIndexOf -function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) { - // Empty buffer means no match - if (buffer.length === 0) return -1 - - // Normalize byteOffset - if (typeof byteOffset === 'string') { - encoding = byteOffset - byteOffset = 0 - } else if (byteOffset > 0x7fffffff) { - byteOffset = 0x7fffffff - } else if (byteOffset < -0x80000000) { - byteOffset = -0x80000000 - } - byteOffset = +byteOffset // Coerce to Number. - if (numberIsNaN(byteOffset)) { - // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer - byteOffset = dir ? 0 : (buffer.length - 1) - } - - // Normalize byteOffset: negative offsets start from the end of the buffer - if (byteOffset < 0) byteOffset = buffer.length + byteOffset - if (byteOffset >= buffer.length) { - if (dir) return -1 - else byteOffset = buffer.length - 1 - } else if (byteOffset < 0) { - if (dir) byteOffset = 0 - else return -1 - } - - // Normalize val - if (typeof val === 'string') { - val = Buffer.from(val, encoding) - } - - // Finally, search either indexOf (if dir is true) or lastIndexOf - if (Buffer.isBuffer(val)) { - // Special case: looking for empty string/buffer always fails - if (val.length === 0) { - return -1 - } - return arrayIndexOf(buffer, val, byteOffset, encoding, dir) - } else if (typeof val === 'number') { - val = val & 0xFF // Search for a byte value [0-255] - if (typeof Uint8Array.prototype.indexOf === 'function') { - if (dir) { - return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset) - } else { - return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset) - } - } - return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir) - } - - throw new TypeError('val must be string, number or Buffer') -} - -function arrayIndexOf (arr, val, byteOffset, encoding, dir) { - var indexSize = 1 - var arrLength = arr.length - var valLength = val.length - - if (encoding !== undefined) { - encoding = String(encoding).toLowerCase() - if (encoding === 'ucs2' || encoding === 'ucs-2' || - encoding === 'utf16le' || encoding === 'utf-16le') { - if (arr.length < 2 || val.length < 2) { - return -1 - } - indexSize = 2 - arrLength /= 2 - valLength /= 2 - byteOffset /= 2 - } - } - - function read (buf, i) { - if (indexSize === 1) { - return buf[i] - } else { - return buf.readUInt16BE(i * indexSize) - } - } - - var i - if (dir) { - var foundIndex = -1 - for (i = byteOffset; i < arrLength; i++) { - if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) { - if (foundIndex === -1) foundIndex = i - if (i - foundIndex + 1 === valLength) return foundIndex * indexSize - } else { - if (foundIndex !== -1) i -= i - foundIndex - foundIndex = -1 - } - } - } else { - if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength - for (i = byteOffset; i >= 0; i--) { - var found = true - for (var j = 0; j < valLength; j++) { - if (read(arr, i + j) !== read(val, j)) { - found = false - break - } - } - if (found) return i - } - } - - return -1 -} - -Buffer.prototype.includes = function includes (val, byteOffset, encoding) { - return this.indexOf(val, byteOffset, encoding) !== -1 -} - -Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) { - return bidirectionalIndexOf(this, val, byteOffset, encoding, true) -} - -Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) { - return bidirectionalIndexOf(this, val, byteOffset, encoding, false) -} - -function hexWrite (buf, string, offset, length) { - offset = Number(offset) || 0 - var remaining = buf.length - offset - if (!length) { - length = remaining - } else { - length = Number(length) - if (length > remaining) { - length = remaining - } - } - - var strLen = string.length - - if (length > strLen / 2) { - length = strLen / 2 - } - for (var i = 0; i < length; ++i) { - var parsed = parseInt(string.substr(i * 2, 2), 16) - if (numberIsNaN(parsed)) return i - buf[offset + i] = parsed - } - return i -} - -function utf8Write (buf, string, offset, length) { - return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) -} - -function asciiWrite (buf, string, offset, length) { - return blitBuffer(asciiToBytes(string), buf, offset, length) -} - -function latin1Write (buf, string, offset, length) { - return asciiWrite(buf, string, offset, length) -} - -function base64Write (buf, string, offset, length) { - return blitBuffer(base64ToBytes(string), buf, offset, length) -} - -function ucs2Write (buf, string, offset, length) { - return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) -} - -Buffer.prototype.write = function write (string, offset, length, encoding) { - // Buffer#write(string) - if (offset === undefined) { - encoding = 'utf8' - length = this.length - offset = 0 - // Buffer#write(string, encoding) - } else if (length === undefined && typeof offset === 'string') { - encoding = offset - length = this.length - offset = 0 - // Buffer#write(string, offset[, length][, encoding]) - } else if (isFinite(offset)) { - offset = offset >>> 0 - if (isFinite(length)) { - length = length >>> 0 - if (encoding === undefined) encoding = 'utf8' - } else { - encoding = length - length = undefined - } - } else { - throw new Error( - 'Buffer.write(string, encoding, offset[, length]) is no longer supported' - ) - } - - var remaining = this.length - offset - if (length === undefined || length > remaining) length = remaining - - if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { - throw new RangeError('Attempt to write outside buffer bounds') - } - - if (!encoding) encoding = 'utf8' - - var loweredCase = false - for (;;) { - switch (encoding) { - case 'hex': - return hexWrite(this, string, offset, length) - - case 'utf8': - case 'utf-8': - return utf8Write(this, string, offset, length) - - case 'ascii': - return asciiWrite(this, string, offset, length) - - case 'latin1': - case 'binary': - return latin1Write(this, string, offset, length) - - case 'base64': - // Warning: maxLength not taken into account in base64Write - return base64Write(this, string, offset, length) - - case 'ucs2': - case 'ucs-2': - case 'utf16le': - case 'utf-16le': - return ucs2Write(this, string, offset, length) - - default: - if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) - encoding = ('' + encoding).toLowerCase() - loweredCase = true - } - } -} - -Buffer.prototype.toJSON = function toJSON () { - return { - type: 'Buffer', - data: Array.prototype.slice.call(this._arr || this, 0) - } -} - -function base64Slice (buf, start, end) { - if (start === 0 && end === buf.length) { - return base64.fromByteArray(buf) - } else { - return base64.fromByteArray(buf.slice(start, end)) - } -} - -function utf8Slice (buf, start, end) { - end = Math.min(buf.length, end) - var res = [] - - var i = start - while (i < end) { - var firstByte = buf[i] - var codePoint = null - var bytesPerSequence = (firstByte > 0xEF) ? 4 - : (firstByte > 0xDF) ? 3 - : (firstByte > 0xBF) ? 2 - : 1 - - if (i + bytesPerSequence <= end) { - var secondByte, thirdByte, fourthByte, tempCodePoint - - switch (bytesPerSequence) { - case 1: - if (firstByte < 0x80) { - codePoint = firstByte - } - break - case 2: - secondByte = buf[i + 1] - if ((secondByte & 0xC0) === 0x80) { - tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F) - if (tempCodePoint > 0x7F) { - codePoint = tempCodePoint - } - } - break - case 3: - secondByte = buf[i + 1] - thirdByte = buf[i + 2] - if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { - tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F) - if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { - codePoint = tempCodePoint - } - } - break - case 4: - secondByte = buf[i + 1] - thirdByte = buf[i + 2] - fourthByte = buf[i + 3] - if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { - tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F) - if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { - codePoint = tempCodePoint - } - } - } - } - - if (codePoint === null) { - // we did not generate a valid codePoint so insert a - // replacement char (U+FFFD) and advance only 1 byte - codePoint = 0xFFFD - bytesPerSequence = 1 - } else if (codePoint > 0xFFFF) { - // encode to utf16 (surrogate pair dance) - codePoint -= 0x10000 - res.push(codePoint >>> 10 & 0x3FF | 0xD800) - codePoint = 0xDC00 | codePoint & 0x3FF - } - - res.push(codePoint) - i += bytesPerSequence - } - - return decodeCodePointsArray(res) -} - -// Based on http://stackoverflow.com/a/22747272/680742, the browser with -// the lowest limit is Chrome, with 0x10000 args. -// We go 1 magnitude less, for safety -var MAX_ARGUMENTS_LENGTH = 0x1000 - -function decodeCodePointsArray (codePoints) { - var len = codePoints.length - if (len <= MAX_ARGUMENTS_LENGTH) { - return String.fromCharCode.apply(String, codePoints) // avoid extra slice() - } - - // Decode in chunks to avoid "call stack size exceeded". - var res = '' - var i = 0 - while (i < len) { - res += String.fromCharCode.apply( - String, - codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH) - ) - } - return res -} - -function asciiSlice (buf, start, end) { - var ret = '' - end = Math.min(buf.length, end) - - for (var i = start; i < end; ++i) { - ret += String.fromCharCode(buf[i] & 0x7F) - } - return ret -} - -function latin1Slice (buf, start, end) { - var ret = '' - end = Math.min(buf.length, end) - - for (var i = start; i < end; ++i) { - ret += String.fromCharCode(buf[i]) - } - return ret -} - -function hexSlice (buf, start, end) { - var len = buf.length - - if (!start || start < 0) start = 0 - if (!end || end < 0 || end > len) end = len - - var out = '' - for (var i = start; i < end; ++i) { - out += toHex(buf[i]) - } - return out -} - -function utf16leSlice (buf, start, end) { - var bytes = buf.slice(start, end) - var res = '' - for (var i = 0; i < bytes.length; i += 2) { - res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256)) - } - return res -} - -Buffer.prototype.slice = function slice (start, end) { - var len = this.length - start = ~~start - end = end === undefined ? len : ~~end - - if (start < 0) { - start += len - if (start < 0) start = 0 - } else if (start > len) { - start = len - } - - if (end < 0) { - end += len - if (end < 0) end = 0 - } else if (end > len) { - end = len - } - - if (end < start) end = start - - var newBuf = this.subarray(start, end) - // Return an augmented `Uint8Array` instance - newBuf.__proto__ = Buffer.prototype - return newBuf -} - -/* - * Need to make sure that buffer isn't trying to write out of bounds. - */ -function checkOffset (offset, ext, length) { - if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') - if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') -} - -Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) { - offset = offset >>> 0 - byteLength = byteLength >>> 0 - if (!noAssert) checkOffset(offset, byteLength, this.length) - - var val = this[offset] - var mul = 1 - var i = 0 - while (++i < byteLength && (mul *= 0x100)) { - val += this[offset + i] * mul - } - - return val -} - -Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) { - offset = offset >>> 0 - byteLength = byteLength >>> 0 - if (!noAssert) { - checkOffset(offset, byteLength, this.length) - } - - var val = this[offset + --byteLength] - var mul = 1 - while (byteLength > 0 && (mul *= 0x100)) { - val += this[offset + --byteLength] * mul - } - - return val -} - -Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 1, this.length) - return this[offset] -} - -Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 2, this.length) - return this[offset] | (this[offset + 1] << 8) -} - -Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 2, this.length) - return (this[offset] << 8) | this[offset + 1] -} - -Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 4, this.length) - - return ((this[offset]) | - (this[offset + 1] << 8) | - (this[offset + 2] << 16)) + - (this[offset + 3] * 0x1000000) -} - -Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 4, this.length) - - return (this[offset] * 0x1000000) + - ((this[offset + 1] << 16) | - (this[offset + 2] << 8) | - this[offset + 3]) -} - -Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) { - offset = offset >>> 0 - byteLength = byteLength >>> 0 - if (!noAssert) checkOffset(offset, byteLength, this.length) - - var val = this[offset] - var mul = 1 - var i = 0 - while (++i < byteLength && (mul *= 0x100)) { - val += this[offset + i] * mul - } - mul *= 0x80 - - if (val >= mul) val -= Math.pow(2, 8 * byteLength) - - return val -} - -Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) { - offset = offset >>> 0 - byteLength = byteLength >>> 0 - if (!noAssert) checkOffset(offset, byteLength, this.length) - - var i = byteLength - var mul = 1 - var val = this[offset + --i] - while (i > 0 && (mul *= 0x100)) { - val += this[offset + --i] * mul - } - mul *= 0x80 - - if (val >= mul) val -= Math.pow(2, 8 * byteLength) - - return val -} - -Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 1, this.length) - if (!(this[offset] & 0x80)) return (this[offset]) - return ((0xff - this[offset] + 1) * -1) -} - -Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 2, this.length) - var val = this[offset] | (this[offset + 1] << 8) - return (val & 0x8000) ? val | 0xFFFF0000 : val -} - -Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 2, this.length) - var val = this[offset + 1] | (this[offset] << 8) - return (val & 0x8000) ? val | 0xFFFF0000 : val -} - -Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 4, this.length) - - return (this[offset]) | - (this[offset + 1] << 8) | - (this[offset + 2] << 16) | - (this[offset + 3] << 24) -} - -Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 4, this.length) - - return (this[offset] << 24) | - (this[offset + 1] << 16) | - (this[offset + 2] << 8) | - (this[offset + 3]) -} - -Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 4, this.length) - return ieee754.read(this, offset, true, 23, 4) -} - -Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 4, this.length) - return ieee754.read(this, offset, false, 23, 4) -} - -Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 8, this.length) - return ieee754.read(this, offset, true, 52, 8) -} - -Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) { - offset = offset >>> 0 - if (!noAssert) checkOffset(offset, 8, this.length) - return ieee754.read(this, offset, false, 52, 8) -} - -function checkInt (buf, value, offset, ext, max, min) { - if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance') - if (value > max || value < min) throw new RangeError('"value" argument is out of bounds') - if (offset + ext > buf.length) throw new RangeError('Index out of range') -} - -Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) { - value = +value - offset = offset >>> 0 - byteLength = byteLength >>> 0 - if (!noAssert) { - var maxBytes = Math.pow(2, 8 * byteLength) - 1 - checkInt(this, value, offset, byteLength, maxBytes, 0) - } - - var mul = 1 - var i = 0 - this[offset] = value & 0xFF - while (++i < byteLength && (mul *= 0x100)) { - this[offset + i] = (value / mul) & 0xFF - } - - return offset + byteLength -} - -Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) { - value = +value - offset = offset >>> 0 - byteLength = byteLength >>> 0 - if (!noAssert) { - var maxBytes = Math.pow(2, 8 * byteLength) - 1 - checkInt(this, value, offset, byteLength, maxBytes, 0) - } - - var i = byteLength - 1 - var mul = 1 - this[offset + i] = value & 0xFF - while (--i >= 0 && (mul *= 0x100)) { - this[offset + i] = (value / mul) & 0xFF - } - - return offset + byteLength -} - -Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0) - this[offset] = (value & 0xff) - return offset + 1 -} - -Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) - this[offset] = (value & 0xff) - this[offset + 1] = (value >>> 8) - return offset + 2 -} - -Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) - this[offset] = (value >>> 8) - this[offset + 1] = (value & 0xff) - return offset + 2 -} - -Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) - this[offset + 3] = (value >>> 24) - this[offset + 2] = (value >>> 16) - this[offset + 1] = (value >>> 8) - this[offset] = (value & 0xff) - return offset + 4 -} - -Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) - this[offset] = (value >>> 24) - this[offset + 1] = (value >>> 16) - this[offset + 2] = (value >>> 8) - this[offset + 3] = (value & 0xff) - return offset + 4 -} - -Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) { - var limit = Math.pow(2, (8 * byteLength) - 1) - - checkInt(this, value, offset, byteLength, limit - 1, -limit) - } - - var i = 0 - var mul = 1 - var sub = 0 - this[offset] = value & 0xFF - while (++i < byteLength && (mul *= 0x100)) { - if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) { - sub = 1 - } - this[offset + i] = ((value / mul) >> 0) - sub & 0xFF - } - - return offset + byteLength -} - -Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) { - var limit = Math.pow(2, (8 * byteLength) - 1) - - checkInt(this, value, offset, byteLength, limit - 1, -limit) - } - - var i = byteLength - 1 - var mul = 1 - var sub = 0 - this[offset + i] = value & 0xFF - while (--i >= 0 && (mul *= 0x100)) { - if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) { - sub = 1 - } - this[offset + i] = ((value / mul) >> 0) - sub & 0xFF - } - - return offset + byteLength -} - -Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80) - if (value < 0) value = 0xff + value + 1 - this[offset] = (value & 0xff) - return offset + 1 -} - -Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) - this[offset] = (value & 0xff) - this[offset + 1] = (value >>> 8) - return offset + 2 -} - -Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) - this[offset] = (value >>> 8) - this[offset + 1] = (value & 0xff) - return offset + 2 -} - -Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) - this[offset] = (value & 0xff) - this[offset + 1] = (value >>> 8) - this[offset + 2] = (value >>> 16) - this[offset + 3] = (value >>> 24) - return offset + 4 -} - -Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) - if (value < 0) value = 0xffffffff + value + 1 - this[offset] = (value >>> 24) - this[offset + 1] = (value >>> 16) - this[offset + 2] = (value >>> 8) - this[offset + 3] = (value & 0xff) - return offset + 4 -} - -function checkIEEE754 (buf, value, offset, ext, max, min) { - if (offset + ext > buf.length) throw new RangeError('Index out of range') - if (offset < 0) throw new RangeError('Index out of range') -} - -function writeFloat (buf, value, offset, littleEndian, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) { - checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) - } - ieee754.write(buf, value, offset, littleEndian, 23, 4) - return offset + 4 -} - -Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) { - return writeFloat(this, value, offset, true, noAssert) -} - -Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) { - return writeFloat(this, value, offset, false, noAssert) -} - -function writeDouble (buf, value, offset, littleEndian, noAssert) { - value = +value - offset = offset >>> 0 - if (!noAssert) { - checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) - } - ieee754.write(buf, value, offset, littleEndian, 52, 8) - return offset + 8 -} - -Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) { - return writeDouble(this, value, offset, true, noAssert) -} - -Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) { - return writeDouble(this, value, offset, false, noAssert) -} - -// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) -Buffer.prototype.copy = function copy (target, targetStart, start, end) { - if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer') - if (!start) start = 0 - if (!end && end !== 0) end = this.length - if (targetStart >= target.length) targetStart = target.length - if (!targetStart) targetStart = 0 - if (end > 0 && end < start) end = start - - // Copy 0 bytes; we're done - if (end === start) return 0 - if (target.length === 0 || this.length === 0) return 0 - - // Fatal error conditions - if (targetStart < 0) { - throw new RangeError('targetStart out of bounds') - } - if (start < 0 || start >= this.length) throw new RangeError('Index out of range') - if (end < 0) throw new RangeError('sourceEnd out of bounds') - - // Are we oob? - if (end > this.length) end = this.length - if (target.length - targetStart < end - start) { - end = target.length - targetStart + start - } - - var len = end - start - - if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') { - // Use built-in when available, missing from IE11 - this.copyWithin(targetStart, start, end) - } else if (this === target && start < targetStart && targetStart < end) { - // descending copy from end - for (var i = len - 1; i >= 0; --i) { - target[i + targetStart] = this[i + start] - } - } else { - Uint8Array.prototype.set.call( - target, - this.subarray(start, end), - targetStart - ) - } - - return len -} - -// Usage: -// buffer.fill(number[, offset[, end]]) -// buffer.fill(buffer[, offset[, end]]) -// buffer.fill(string[, offset[, end]][, encoding]) -Buffer.prototype.fill = function fill (val, start, end, encoding) { - // Handle string cases: - if (typeof val === 'string') { - if (typeof start === 'string') { - encoding = start - start = 0 - end = this.length - } else if (typeof end === 'string') { - encoding = end - end = this.length - } - if (encoding !== undefined && typeof encoding !== 'string') { - throw new TypeError('encoding must be a string') - } - if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { - throw new TypeError('Unknown encoding: ' + encoding) - } - if (val.length === 1) { - var code = val.charCodeAt(0) - if ((encoding === 'utf8' && code < 128) || - encoding === 'latin1') { - // Fast path: If `val` fits into a single byte, use that numeric value. - val = code - } - } - } else if (typeof val === 'number') { - val = val & 255 - } - - // Invalid ranges are not set to a default, so can range check early. - if (start < 0 || this.length < start || this.length < end) { - throw new RangeError('Out of range index') - } - - if (end <= start) { - return this - } - - start = start >>> 0 - end = end === undefined ? this.length : end >>> 0 - - if (!val) val = 0 - - var i - if (typeof val === 'number') { - for (i = start; i < end; ++i) { - this[i] = val - } - } else { - var bytes = Buffer.isBuffer(val) - ? val - : Buffer.from(val, encoding) - var len = bytes.length - if (len === 0) { - throw new TypeError('The value "' + val + - '" is invalid for argument "value"') - } - for (i = 0; i < end - start; ++i) { - this[i + start] = bytes[i % len] - } - } - - return this -} - -// HELPER FUNCTIONS -// ================ - -var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g - -function base64clean (str) { - // Node takes equal signs as end of the Base64 encoding - str = str.split('=')[0] - // Node strips out invalid characters like \n and \t from the string, base64-js does not - str = str.trim().replace(INVALID_BASE64_RE, '') - // Node converts strings with length < 2 to '' - if (str.length < 2) return '' - // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not - while (str.length % 4 !== 0) { - str = str + '=' - } - return str -} - -function toHex (n) { - if (n < 16) return '0' + n.toString(16) - return n.toString(16) -} - -function utf8ToBytes (string, units) { - units = units || Infinity - var codePoint - var length = string.length - var leadSurrogate = null - var bytes = [] - - for (var i = 0; i < length; ++i) { - codePoint = string.charCodeAt(i) - - // is surrogate component - if (codePoint > 0xD7FF && codePoint < 0xE000) { - // last char was a lead - if (!leadSurrogate) { - // no lead yet - if (codePoint > 0xDBFF) { - // unexpected trail - if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) - continue - } else if (i + 1 === length) { - // unpaired lead - if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) - continue - } - - // valid lead - leadSurrogate = codePoint - - continue - } - - // 2 leads in a row - if (codePoint < 0xDC00) { - if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) - leadSurrogate = codePoint - continue - } - - // valid surrogate pair - codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000 - } else if (leadSurrogate) { - // valid bmp char, but last char was a lead - if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) - } - - leadSurrogate = null - - // encode utf8 - if (codePoint < 0x80) { - if ((units -= 1) < 0) break - bytes.push(codePoint) - } else if (codePoint < 0x800) { - if ((units -= 2) < 0) break - bytes.push( - codePoint >> 0x6 | 0xC0, - codePoint & 0x3F | 0x80 - ) - } else if (codePoint < 0x10000) { - if ((units -= 3) < 0) break - bytes.push( - codePoint >> 0xC | 0xE0, - codePoint >> 0x6 & 0x3F | 0x80, - codePoint & 0x3F | 0x80 - ) - } else if (codePoint < 0x110000) { - if ((units -= 4) < 0) break - bytes.push( - codePoint >> 0x12 | 0xF0, - codePoint >> 0xC & 0x3F | 0x80, - codePoint >> 0x6 & 0x3F | 0x80, - codePoint & 0x3F | 0x80 - ) - } else { - throw new Error('Invalid code point') - } - } - - return bytes -} - -function asciiToBytes (str) { - var byteArray = [] - for (var i = 0; i < str.length; ++i) { - // Node's code seems to be doing this and not & 0x7F.. - byteArray.push(str.charCodeAt(i) & 0xFF) - } - return byteArray -} - -function utf16leToBytes (str, units) { - var c, hi, lo - var byteArray = [] - for (var i = 0; i < str.length; ++i) { - if ((units -= 2) < 0) break - - c = str.charCodeAt(i) - hi = c >> 8 - lo = c % 256 - byteArray.push(lo) - byteArray.push(hi) - } - - return byteArray -} - -function base64ToBytes (str) { - return base64.toByteArray(base64clean(str)) -} - -function blitBuffer (src, dst, offset, length) { - for (var i = 0; i < length; ++i) { - if ((i + offset >= dst.length) || (i >= src.length)) break - dst[i + offset] = src[i] - } - return i -} - -// ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass -// the `instanceof` check but they should be treated as of that type. -// See: https://github.com/feross/buffer/issues/166 -function isInstance (obj, type) { - return obj instanceof type || - (obj != null && obj.constructor != null && obj.constructor.name != null && - obj.constructor.name === type.name) -} -function numberIsNaN (obj) { - // For IE11 support - return obj !== obj // eslint-disable-line no-self-compare -} diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/dh.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/dh.js deleted file mode 100644 index 642c216..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/dh.js +++ /dev/null @@ -1,165 +0,0 @@ -var BN = require('bn'); -var MillerRabin = require('miller-rabin'); -var millerRabin = new MillerRabin(); -var TWENTYFOUR = new BN(24); -var ELEVEN = new BN(11); -var TEN = new BN(10); -var THREE = new BN(3); -var SEVEN = new BN(7); -var Buffer = require('safe-buffer').Buffer -var primes = require('./generatePrime'); -var randomBytes = require('randombytes'); -module.exports = DH; - -function setPublicKey(pub, enc) { - enc = enc || 'utf8'; - if (!Buffer.isBuffer(pub)) { - pub = new Buffer(pub, enc); - } - this._pub = new BN(pub); - return this; -} - -function setPrivateKey(priv, enc) { - enc = enc || 'utf8'; - if (!Buffer.isBuffer(priv)) { - priv = new Buffer(priv, enc); - } - this._priv = new BN(priv); - return this; -} - -var primeCache = {}; -function checkPrime(prime, generator) { - var gen = generator.toString('hex'); - var hex = [gen, prime.toString(16)].join('_'); - if (hex in primeCache) { - return primeCache[hex]; - } - var error = 0; - - if (prime.isEven() || - !primes.simpleSieve || - !primes.fermatTest(prime) || - !millerRabin.test(prime)) { - //not a prime so +1 - error += 1; - - if (gen === '02' || gen === '05') { - // we'd be able to check the generator - // it would fail so +8 - error += 8; - } else { - //we wouldn't be able to test the generator - // so +4 - error += 4; - } - primeCache[hex] = error; - return error; - } - if (!millerRabin.test(prime.shrn(1))) { - //not a safe prime - error += 2; - } - var rem; - switch (gen) { - case '02': - if (prime.mod(TWENTYFOUR).cmp(ELEVEN)) { - // unsuidable generator - error += 8; - } - break; - case '05': - rem = prime.mod(TEN); - if (rem.cmp(THREE) && rem.cmp(SEVEN)) { - // prime mod 10 needs to equal 3 or 7 - error += 8; - } - break; - default: - error += 4; - } - primeCache[hex] = error; - return error; -} - -function DH(prime, generator, malleable) { - this.setGenerator(generator); - this.__prime = new BN(prime); - this._prime = BN.mont(this.__prime); - this._primeLen = 128; - this._pub = undefined; - this._priv = undefined; - this._primeCode = undefined; - if (malleable) { - this.setPublicKey = setPublicKey; - this.setPrivateKey = setPrivateKey; - } else { - this._primeCode = 8; - } -} -Object.defineProperty(DH.prototype, 'verifyError', { - enumerable: true, - get: function () { - if (typeof this._primeCode !== 'number') { - this._primeCode = checkPrime(this.__prime, this.__gen); - } - return this._primeCode; - } -}); -DH.prototype.generateKeys = function () { - if (!this._priv) { - this._priv = new BN(randomBytes(this._primeLen)); - } - this._pub = this._gen.toRed(this._prime).redPow(this._priv).fromRed(); - return this.getPublicKey(); -}; - -DH.prototype.computeSecret = function (other) { - other = new BN(other); - other = other.toRed(this._prime); - var secret = other.redPow(this._priv).fromRed(); - var out = new Buffer(secret.toArray()); - var prime = this.getPrime(); - if (out.length < prime.length) { - var front = new Buffer(prime.length - out.length); - front.fill(0); - out = Buffer.concat([front, out]); - } - return out; -}; - -DH.prototype.getPublicKey = function getPublicKey(enc) { - return formatReturnValue(this._pub, enc); -}; - -DH.prototype.getPrivateKey = function getPrivateKey(enc) { - return formatReturnValue(this._priv, enc); -}; - -DH.prototype.getPrime = function (enc) { - return formatReturnValue(this.__prime, enc); -}; - -DH.prototype.getGenerator = function (enc) { - return formatReturnValue(this._gen, enc); -}; - -DH.prototype.setGenerator = function (gen, enc) { - enc = enc || 'utf8'; - if (!Buffer.isBuffer(gen)) { - gen = new Buffer(gen, enc); - } - this.__gen = gen; - this._gen = new BN(gen); - return this; -}; - -function formatReturnValue(bn, enc) { - var buf = new Buffer(bn.toArray()); - if (!enc) { - return buf; - } else { - return buf.toString(enc); - } -} diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/generatePrime.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/generatePrime.js deleted file mode 100644 index 32e053c..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/generatePrime.js +++ /dev/null @@ -1,105 +0,0 @@ -var randomBytes = require('randombytes'); -module.exports = findPrime; -findPrime.simpleSieve = simpleSieve; -findPrime.fermatTest = fermatTest; -var BN = require('bn.js'); -var TWENTYFOUR = new BN(24); -var MillerRabin = require('miller-rabin'); -var millerRabin = new MillerRabin(); -var ONE = new BN(1); -var TWO = new BN(2); -var FIVE = new BN(5); -var SIXTEEN = new BN(16); -var EIGHT = new BN(8); -var TEN = new BN(10); -var THREE = new BN(3); -var SEVEN = new BN(7); -var ELEVEN = new BN(11); -var FOUR = new BN(4); -var TWELVE = new BN(12); -var primes = null; - -function _getPrimes() { - if (primes !== null) - return primes; - - var limit = 0x100000; - var res = []; - res[0] = 2; - for (var i = 1, k = 3; k < limit; k += 2) { - var sqrt = Math.ceil(Math.sqrt(k)); - for (var j = 0; j < i && res[j] <= sqrt; j++) - if (k % res[j] === 0) - break; - - if (i !== j && res[j] <= sqrt) - continue; - - res[i++] = k; - } - primes = res; - return res; -} - -function simpleSieve(p) { - var primes = _getPrimes(); - - for (var i = 0; i < primes.length; i++) - if (p.modn(primes[i]) === 0) { - if (p.cmpn(primes[i]) === 0) { - return true; - } else { - return false; - } - } - - return true; -} - -function fermatTest(p) { - var red = BN.mont(p); - return TWO.toRed(red).redPow(p.subn(1)).fromRed().cmpn(1) === 0; -} - -function findPrime(bits, gen) { - if (bits < 16) { - // this is what openssl does - if (gen === 2 || gen === 5) { - return new BN([0x8c, 0x7b]); - } else { - return new BN([0x8c, 0x27]); - } - } - gen = new BN(gen); - - var num, n2; - - while (true) { - num = new BN(randomBytes(Math.ceil(bits / 8))); - while (num.bitLength() > bits) { - num.ishrn(1); - } - if (num.isEven()) { - num.iadd(ONE); - } - if (!num.testn(1)) { - num.iadd(TWO); - } - if (!gen.cmp(TWO)) { - while (num.mod(TWENTYFOUR).cmp(ELEVEN)) { - num.iadd(FOUR); - } - } else if (!gen.cmp(FIVE)) { - while (num.mod(TEN).cmp(THREE)) { - num.iadd(FOUR); - } - } - n2 = num.shrn(1); - if (simpleSieve(n2) && simpleSieve(num) && - fermatTest(n2) && fermatTest(num) && - millerRabin.test(n2) && millerRabin.test(num)) { - return num; - } - } - -} diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/ieee754.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/ieee754.js deleted file mode 100644 index e87e6ff..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/ieee754.js +++ /dev/null @@ -1,84 +0,0 @@ -exports.read = function (buffer, offset, isLE, mLen, nBytes) { - var e, m - var eLen = (nBytes * 8) - mLen - 1 - var eMax = (1 << eLen) - 1 - var eBias = eMax >> 1 - var nBits = -7 - var i = isLE ? (nBytes - 1) : 0 - var d = isLE ? -1 : 1 - var s = buffer[offset + i] - - i += d - - e = s & ((1 << (-nBits)) - 1) - s >>= (-nBits) - nBits += eLen - for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {} - - m = e & ((1 << (-nBits)) - 1) - e >>= (-nBits) - nBits += mLen - for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {} - - if (e === 0) { - e = 1 - eBias - } else if (e === eMax) { - return m ? NaN : ((s ? -1 : 1) * Infinity) - } else { - m = m + Math.pow(2, mLen) - e = e - eBias - } - return (s ? -1 : 1) * m * Math.pow(2, e - mLen) -} - -exports.write = function (buffer, value, offset, isLE, mLen, nBytes) { - var e, m, c - var eLen = (nBytes * 8) - mLen - 1 - var eMax = (1 << eLen) - 1 - var eBias = eMax >> 1 - var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0) - var i = isLE ? 0 : (nBytes - 1) - var d = isLE ? 1 : -1 - var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0 - - value = Math.abs(value) - - if (isNaN(value) || value === Infinity) { - m = isNaN(value) ? 1 : 0 - e = eMax - } else { - e = Math.floor(Math.log(value) / Math.LN2) - if (value * (c = Math.pow(2, -e)) < 1) { - e-- - c *= 2 - } - if (e + eBias >= 1) { - value += rt / c - } else { - value += rt * Math.pow(2, 1 - eBias) - } - if (value * c >= 2) { - e++ - c /= 2 - } - - if (e + eBias >= eMax) { - m = 0 - e = eMax - } else if (e + eBias >= 1) { - m = ((value * c) - 1) * Math.pow(2, mLen) - e = e + eBias - } else { - m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen) - e = 0 - } - } - - for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {} - - e = (e << mLen) | m - eLen += mLen - for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {} - - buffer[offset + i - d] |= s * 128 -} diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/miller-rabin.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/miller-rabin.js deleted file mode 100644 index 60d2a8e..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/miller-rabin.js +++ /dev/null @@ -1,115 +0,0 @@ -var bn = require('bn.js'); -var brorand = require('brorand'); - -function MillerRabin(rand) { - this.rand = rand || new brorand.Rand(); -} -module.exports = MillerRabin; - -MillerRabin.create = function create(rand) { - return new MillerRabin(rand); -}; - -MillerRabin.prototype._randbelow = function _randbelow(n) { - var len = n.bitLength(); - var min_bytes = Math.ceil(len / 8); - - // Generage random bytes until a number less than n is found. - // This ensures that 0..n-1 have an equal probability of being selected. - do - var a = new bn(this.rand.generate(min_bytes)); - while (a.cmp(n) >= 0); - - return a; -}; - -MillerRabin.prototype._randrange = function _randrange(start, stop) { - // Generate a random number greater than or equal to start and less than stop. - var size = stop.sub(start); - return start.add(this._randbelow(size)); -}; - -MillerRabin.prototype.test = function test(n, k, cb) { - var len = n.bitLength(); - var red = bn.mont(n); - var rone = new bn(1).toRed(red); - - if (!k) - k = Math.max(1, (len / 48) | 0); - - // Find d and s, (n - 1) = (2 ^ s) * d; - var n1 = n.subn(1); - for (var s = 0; !n1.testn(s); s++) {} - var d = n.shrn(s); - - var rn1 = n1.toRed(red); - - var prime = true; - for (; k > 0; k--) { - var a = this._randrange(new bn(2), n1); - if (cb) - cb(a); - - var x = a.toRed(red).redPow(d); - if (x.cmp(rone) === 0 || x.cmp(rn1) === 0) - continue; - - for (var i = 1; i < s; i++) { - x = x.redSqr(); - - if (x.cmp(rone) === 0) - return false; - if (x.cmp(rn1) === 0) - break; - } - - if (i === s) - return false; - } - - return prime; -}; - -MillerRabin.prototype.getDivisor = function getDivisor(n, k) { - var len = n.bitLength(); - var red = bn.mont(n); - var rone = new bn(1).toRed(red); - - if (!k) - k = Math.max(1, (len / 48) | 0); - - // Find d and s, (n - 1) = (2 ^ s) * d; - var n1 = n.subn(1); - for (var s = 0; !n1.testn(s); s++) {} - var d = n.shrn(s); - - var rn1 = n1.toRed(red); - - for (; k > 0; k--) { - var a = this._randrange(new bn(2), n1); - - var g = n.gcd(a); - if (g.cmpn(1) !== 0) - return g; - - var x = a.toRed(red).redPow(d); - if (x.cmp(rone) === 0 || x.cmp(rn1) === 0) - continue; - - for (var i = 1; i < s; i++) { - x = x.redSqr(); - - if (x.cmp(rone) === 0) - return x.fromRed().subn(1).gcd(n); - if (x.cmp(rn1) === 0) - break; - } - - if (i === s) { - x = x.redSqr(); - return x.fromRed().subn(1).gcd(n); - } - } - - return false; -}; diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/randombytes.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/randombytes.js deleted file mode 100644 index fef72d7..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/randombytes.js +++ /dev/null @@ -1,31 +0,0 @@ -'use strict' - -function oldBrowser() { - throw new Error('Secure random number generation is not supported by this browser.\nUse Chrome, Firefox or Internet Explorer 11') -} - -var Buffer = require('safe-buffer').Buffer - -module.exports = randomBytes - -function randomBytes(size, cb) { - // phantomjs needs to throw - if (size > 65536) throw new Error('requested too many random bytes') - // in case browserify isn't using the Uint8Array version - var arr = []; - for (var i = 0; i < size; i++) { - arr.push(Math.ceil(Math.random() * 255)) - } - var rawBytes = new Uint8Array(arr) - - // XXX: phantomjs doesn't like a buffer being passed here - var bytes = Buffer.from(rawBytes.buffer) - - if (typeof cb === 'function') { - return process.nextTick(function () { - cb(null, bytes) - }) - } - - return bytes -} diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/safe-buffer.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/safe-buffer.js deleted file mode 100644 index 22438da..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/lib/safe-buffer.js +++ /dev/null @@ -1,62 +0,0 @@ -/* eslint-disable node/no-deprecated-api */ -var buffer = require('buffer') -var Buffer = buffer.Buffer - -// alternative to using Object.keys for old browsers -function copyProps (src, dst) { - for (var key in src) { - dst[key] = src[key] - } -} -if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) { - module.exports = buffer -} else { - // Copy properties from require('buffer') - copyProps(buffer, exports) - exports.Buffer = SafeBuffer -} - -function SafeBuffer (arg, encodingOrOffset, length) { - return Buffer(arg, encodingOrOffset, length) -} - -// Copy static methods from Buffer -copyProps(Buffer, SafeBuffer) - -SafeBuffer.from = function (arg, encodingOrOffset, length) { - if (typeof arg === 'number') { - throw new TypeError('Argument must not be a number') - } - return Buffer(arg, encodingOrOffset, length) -} - -SafeBuffer.alloc = function (size, fill, encoding) { - if (typeof size !== 'number') { - throw new TypeError('Argument must be a number') - } - var buf = Buffer(size) - if (fill !== undefined) { - if (typeof encoding === 'string') { - buf.fill(fill, encoding) - } else { - buf.fill(fill) - } - } else { - buf.fill(0) - } - return buf -} - -SafeBuffer.allocUnsafe = function (size) { - if (typeof size !== 'number') { - throw new TypeError('Argument must be a number') - } - return Buffer(size) -} - -SafeBuffer.allocUnsafeSlow = function (size) { - if (typeof size !== 'number') { - throw new TypeError('Argument must be a number') - } - return buffer.SlowBuffer(size) -} diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/md5.min.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/md5.min.js deleted file mode 100644 index 7418ff8..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/crypto/md5.min.js +++ /dev/null @@ -1,10 +0,0 @@ -/** - * [js-md5]{@link https://github.com/emn178/js-md5} - * - * @namespace md5 - * @version 0.7.3 - * @author Chen, Yi-Cyuan [emn178@gmail.com] - * @copyright Chen, Yi-Cyuan 2014-2017 - * @license MIT - */ -!function(){"use strict";function t(t){if(t)d[0]=d[16]=d[1]=d[2]=d[3]=d[4]=d[5]=d[6]=d[7]=d[8]=d[9]=d[10]=d[11]=d[12]=d[13]=d[14]=d[15]=0,this.blocks=d,this.buffer8=l;else if(a){var r=new ArrayBuffer(68);this.buffer8=new Uint8Array(r),this.blocks=new Uint32Array(r)}else this.blocks=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];this.h0=this.h1=this.h2=this.h3=this.start=this.bytes=this.hBytes=0,this.finalized=this.hashed=!1,this.first=!0}var r="input is invalid type",e="object"==typeof window,i=e?window:{};i.JS_MD5_NO_WINDOW&&(e=!1);var s=!e&&"object"==typeof self,h=!i.JS_MD5_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node;h?i=global:s&&(i=self);var f=!i.JS_MD5_NO_COMMON_JS&&"object"==typeof module&&module.exports,o="function"==typeof define&&define.amd,a=!i.JS_MD5_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,n="0123456789abcdef".split(""),u=[128,32768,8388608,-2147483648],y=[0,8,16,24],c=["hex","array","digest","buffer","arrayBuffer","base64"],p="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(""),d=[],l;if(a){var A=new ArrayBuffer(68);l=new Uint8Array(A),d=new Uint32Array(A)}!i.JS_MD5_NO_NODE_JS&&Array.isArray||(Array.isArray=function(t){return"[object Array]"===Object.prototype.toString.call(t)}),!a||!i.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW&&ArrayBuffer.isView||(ArrayBuffer.isView=function(t){return"object"==typeof t&&t.buffer&&t.buffer.constructor===ArrayBuffer});var b=function(r){return function(e){return new t(!0).update(e)[r]()}},v=function(){var r=b("hex");h&&(r=w(r)),r.create=function(){return new t},r.update=function(t){return r.create().update(t)};for(var e=0;e>2]|=t[f]<>6,u[h++]=128|63&s):s<55296||s>=57344?(u[h++]=224|s>>12,u[h++]=128|s>>6&63,u[h++]=128|63&s):(s=65536+((1023&s)<<10|1023&t.charCodeAt(++f)),u[h++]=240|s>>18,u[h++]=128|s>>12&63,u[h++]=128|s>>6&63,u[h++]=128|63&s);else for(h=this.start;f>2]|=s<>2]|=(192|s>>6)<>2]|=(128|63&s)<=57344?(n[h>>2]|=(224|s>>12)<>2]|=(128|s>>6&63)<>2]|=(128|63&s)<>2]|=(240|s>>18)<>2]|=(128|s>>12&63)<>2]|=(128|s>>6&63)<>2]|=(128|63&s)<=64?(this.start=h-64,this.hash(),this.hashed=!0):this.start=h}return this.bytes>4294967295&&(this.hBytes+=this.bytes/4294967296<<0,this.bytes=this.bytes%4294967296),this}},t.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,r=this.lastByteIndex;t[r>>2]|=u[3&r],r>=56&&(this.hashed||this.hash(),t[0]=t[16],t[16]=t[1]=t[2]=t[3]=t[4]=t[5]=t[6]=t[7]=t[8]=t[9]=t[10]=t[11]=t[12]=t[13]=t[14]=t[15]=0),t[14]=this.bytes<<3,t[15]=this.hBytes<<3|this.bytes>>>29,this.hash()}},t.prototype.hash=function(){var t,r,e,i,s,h,f=this.blocks;this.first?r=((r=((t=((t=f[0]-680876937)<<7|t>>>25)-271733879<<0)^(e=((e=(-271733879^(i=((i=(-1732584194^2004318071&t)+f[1]-117830708)<<12|i>>>20)+t<<0)&(-271733879^t))+f[2]-1126478375)<<17|e>>>15)+i<<0)&(i^t))+f[3]-1316259209)<<22|r>>>10)+e<<0:(t=this.h0,r=this.h1,e=this.h2,r=((r+=((t=((t+=((i=this.h3)^r&(e^i))+f[0]-680876936)<<7|t>>>25)+r<<0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+f[1]-389564586)<<12|i>>>20)+t<<0)&(t^r))+f[2]+606105819)<<17|e>>>15)+i<<0)&(i^t))+f[3]-1044525330)<<22|r>>>10)+e<<0),r=((r+=((t=((t+=(i^r&(e^i))+f[4]-176418897)<<7|t>>>25)+r<<0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+f[5]+1200080426)<<12|i>>>20)+t<<0)&(t^r))+f[6]-1473231341)<<17|e>>>15)+i<<0)&(i^t))+f[7]-45705983)<<22|r>>>10)+e<<0,r=((r+=((t=((t+=(i^r&(e^i))+f[8]+1770035416)<<7|t>>>25)+r<<0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+f[9]-1958414417)<<12|i>>>20)+t<<0)&(t^r))+f[10]-42063)<<17|e>>>15)+i<<0)&(i^t))+f[11]-1990404162)<<22|r>>>10)+e<<0,r=((r+=((t=((t+=(i^r&(e^i))+f[12]+1804603682)<<7|t>>>25)+r<<0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+f[13]-40341101)<<12|i>>>20)+t<<0)&(t^r))+f[14]-1502002290)<<17|e>>>15)+i<<0)&(i^t))+f[15]+1236535329)<<22|r>>>10)+e<<0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+f[1]-165796510)<<5|t>>>27)+r<<0)^r))+f[6]-1069501632)<<9|i>>>23)+t<<0)^t&((e=((e+=(t^r&(i^t))+f[11]+643717713)<<14|e>>>18)+i<<0)^i))+f[0]-373897302)<<20|r>>>12)+e<<0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+f[5]-701558691)<<5|t>>>27)+r<<0)^r))+f[10]+38016083)<<9|i>>>23)+t<<0)^t&((e=((e+=(t^r&(i^t))+f[15]-660478335)<<14|e>>>18)+i<<0)^i))+f[4]-405537848)<<20|r>>>12)+e<<0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+f[9]+568446438)<<5|t>>>27)+r<<0)^r))+f[14]-1019803690)<<9|i>>>23)+t<<0)^t&((e=((e+=(t^r&(i^t))+f[3]-187363961)<<14|e>>>18)+i<<0)^i))+f[8]+1163531501)<<20|r>>>12)+e<<0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+f[13]-1444681467)<<5|t>>>27)+r<<0)^r))+f[2]-51403784)<<9|i>>>23)+t<<0)^t&((e=((e+=(t^r&(i^t))+f[7]+1735328473)<<14|e>>>18)+i<<0)^i))+f[12]-1926607734)<<20|r>>>12)+e<<0,r=((r+=((h=(i=((i+=((s=r^e)^(t=((t+=(s^i)+f[5]-378558)<<4|t>>>28)+r<<0))+f[8]-2022574463)<<11|i>>>21)+t<<0)^t)^(e=((e+=(h^r)+f[11]+1839030562)<<16|e>>>16)+i<<0))+f[14]-35309556)<<23|r>>>9)+e<<0,r=((r+=((h=(i=((i+=((s=r^e)^(t=((t+=(s^i)+f[1]-1530992060)<<4|t>>>28)+r<<0))+f[4]+1272893353)<<11|i>>>21)+t<<0)^t)^(e=((e+=(h^r)+f[7]-155497632)<<16|e>>>16)+i<<0))+f[10]-1094730640)<<23|r>>>9)+e<<0,r=((r+=((h=(i=((i+=((s=r^e)^(t=((t+=(s^i)+f[13]+681279174)<<4|t>>>28)+r<<0))+f[0]-358537222)<<11|i>>>21)+t<<0)^t)^(e=((e+=(h^r)+f[3]-722521979)<<16|e>>>16)+i<<0))+f[6]+76029189)<<23|r>>>9)+e<<0,r=((r+=((h=(i=((i+=((s=r^e)^(t=((t+=(s^i)+f[9]-640364487)<<4|t>>>28)+r<<0))+f[12]-421815835)<<11|i>>>21)+t<<0)^t)^(e=((e+=(h^r)+f[15]+530742520)<<16|e>>>16)+i<<0))+f[2]-995338651)<<23|r>>>9)+e<<0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+f[0]-198630844)<<6|t>>>26)+r<<0)|~e))+f[7]+1126891415)<<10|i>>>22)+t<<0)^((e=((e+=(t^(i|~r))+f[14]-1416354905)<<15|e>>>17)+i<<0)|~t))+f[5]-57434055)<<21|r>>>11)+e<<0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+f[12]+1700485571)<<6|t>>>26)+r<<0)|~e))+f[3]-1894986606)<<10|i>>>22)+t<<0)^((e=((e+=(t^(i|~r))+f[10]-1051523)<<15|e>>>17)+i<<0)|~t))+f[1]-2054922799)<<21|r>>>11)+e<<0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+f[8]+1873313359)<<6|t>>>26)+r<<0)|~e))+f[15]-30611744)<<10|i>>>22)+t<<0)^((e=((e+=(t^(i|~r))+f[6]-1560198380)<<15|e>>>17)+i<<0)|~t))+f[13]+1309151649)<<21|r>>>11)+e<<0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+f[4]-145523070)<<6|t>>>26)+r<<0)|~e))+f[11]-1120210379)<<10|i>>>22)+t<<0)^((e=((e+=(t^(i|~r))+f[2]+718787259)<<15|e>>>17)+i<<0)|~t))+f[9]-343485551)<<21|r>>>11)+e<<0,this.first?(this.h0=t+1732584193<<0,this.h1=r-271733879<<0,this.h2=e-1732584194<<0,this.h3=i+271733878<<0,this.first=!1):(this.h0=this.h0+t<<0,this.h1=this.h1+r<<0,this.h2=this.h2+e<<0,this.h3=this.h3+i<<0)},t.prototype.hex=function(){this.finalize();var t=this.h0,r=this.h1,e=this.h2,i=this.h3;return n[t>>4&15]+n[15&t]+n[t>>12&15]+n[t>>8&15]+n[t>>20&15]+n[t>>16&15]+n[t>>28&15]+n[t>>24&15]+n[r>>4&15]+n[15&r]+n[r>>12&15]+n[r>>8&15]+n[r>>20&15]+n[r>>16&15]+n[r>>28&15]+n[r>>24&15]+n[e>>4&15]+n[15&e]+n[e>>12&15]+n[e>>8&15]+n[e>>20&15]+n[e>>16&15]+n[e>>28&15]+n[e>>24&15]+n[i>>4&15]+n[15&i]+n[i>>12&15]+n[i>>8&15]+n[i>>20&15]+n[i>>16&15]+n[i>>28&15]+n[i>>24&15]},t.prototype.toString=t.prototype.hex,t.prototype.digest=function(){this.finalize();var t=this.h0,r=this.h1,e=this.h2,i=this.h3;return[255&t,t>>8&255,t>>16&255,t>>24&255,255&r,r>>8&255,r>>16&255,r>>24&255,255&e,e>>8&255,e>>16&255,e>>24&255,255&i,i>>8&255,i>>16&255,i>>24&255]},t.prototype.array=t.prototype.digest,t.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(16),r=new Uint32Array(t);return r[0]=this.h0,r[1]=this.h1,r[2]=this.h2,r[3]=this.h3,t},t.prototype.buffer=t.prototype.arrayBuffer,t.prototype.base64=function(){for(var t,r,e,i="",s=this.array(),h=0;h<15;)t=s[h++],r=s[h++],e=s[h++],i+=p[t>>>2]+p[63&(t<<4|r>>>4)]+p[63&(r<<2|e>>>6)]+p[63&e];return t=s[h],i+=p[t>>>2]+p[t<<4&63]+"=="};var _=v();f?module.exports=_:(i.md5=_,o&&define(function(){return _}))}(); \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/bluetooth-way.png b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/bluetooth-way.png deleted file mode 100644 index 05fa94c..0000000 Binary files a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/bluetooth-way.png and /dev/null differ diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/bluetooth.png b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/bluetooth.png deleted file mode 100644 index 16a9711..0000000 Binary files a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/bluetooth.png and /dev/null differ diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/failure.png b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/failure.png deleted file mode 100644 index 2a3a87e..0000000 Binary files a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/failure.png and /dev/null differ diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/network.png b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/network.png deleted file mode 100644 index 7366de5..0000000 Binary files a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/network.png and /dev/null differ diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/round.gif b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/round.gif deleted file mode 100644 index 51a6a16..0000000 Binary files a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/round.gif and /dev/null differ diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/wifi-lock.png b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/wifi-lock.png deleted file mode 100644 index d41a16e..0000000 Binary files a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/wifi-lock.png and /dev/null differ diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/wifi-way.png b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/wifi-way.png deleted file mode 100644 index 02050b4..0000000 Binary files a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/wifi-way.png and /dev/null differ diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/wifi.png b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/wifi.png deleted file mode 100644 index beee61a..0000000 Binary files a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/images/wifi.png and /dev/null differ diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/package-lock.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/package-lock.json deleted file mode 100644 index 4c28627..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/package-lock.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "name": "ESP-Config-WeChat", - "version": "1.0.0", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "name": "ESP-Config-WeChat", - "version": "1.0.0", - "license": "ISC", - "dependencies": { - "crypto-js": "^4.0.0" - } - }, - "node_modules/crypto-js": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.0.0.tgz", - "integrity": "sha512-bzHZN8Pn+gS7DQA6n+iUmBfl0hO5DJq++QP3U6uTucDtk/0iGpXd/Gg7CGR0p8tJhofJyaKoWBuJI4eAO00BBg==" - } - }, - "dependencies": { - "crypto-js": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.0.0.tgz", - "integrity": "sha512-bzHZN8Pn+gS7DQA6n+iUmBfl0hO5DJq++QP3U6uTucDtk/0iGpXd/Gg7CGR0p8tJhofJyaKoWBuJI4eAO00BBg==" - } - } -} diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/package.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/package.json deleted file mode 100644 index 92722a1..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/package.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "name": "ESP-Config-WeChat", - "version": "1.0.0", - "description": "", - "main": "app.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/EspressifApps/ESP-Config-WeChat.git" - }, - "keywords": [], - "author": "", - "license": "ISC", - "bugs": { - "url": "https://github.com/EspressifApps/ESP-Config-WeChat/issues" - }, - "homepage": "https://github.com/EspressifApps/ESP-Config-WeChat#readme", - "dependencies": { - "crypto-js": "^4.0.0" - } -} diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueConnect/blueConnect.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueConnect/blueConnect.js deleted file mode 100644 index 014bc55..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueConnect/blueConnect.js +++ /dev/null @@ -1,388 +0,0 @@ -//获取应用实例 -const app = getApp() -const util = require('../../utils/util.js'); - -const timeOut = 20;//超时时间 -var timeId = ""; -Page({ - data: { - failure: false, - value: 0, - desc: "Device connecting...", - isChecksum: false, - isEncrypt: false, - flagEnd: false, - defaultData: 1, - ssidType: 2, - passwordType: 3, - meshIdType: 3, - deviceId: "", - ssid: "", - uuid: "", - serviceId: "", - password: "", - meshId: "", - processList: [], - result: [], - }, - blueConnect: function (event) { - var self = this; - self.setProcess(0, util.descSucList[0]); - self.setProcess(10, util.descSucList[1]); - self.setProcess(20, util.descSucList[2]); - self.setProcess(35, util.descSucList[3]); - self.openNotify(self.data.deviceId, self.data.serviceId, self.data.uuid); - }, - // 告知设备数据开始写入 - writeDeviceStart: function (deviceId, serviceId, characteristicId, data) { - var self = this, obj = {}, frameControl = 0; - self.setProcess(40, util.descSucList[4]); - app.data.sequenceControl = parseInt(app.data.sequenceControl) + 1; - if (!util._isEmpty(data)) { - obj = util.isSubcontractor(data, self.data.isChecksum, app.data.sequenceControl, self.data.isEncrypt); - frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag); - } else { - obj = util.isSubcontractor([self.data.defaultData], self.data.isChecksum, app.data.sequenceControl, true); - frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag); - } - // var defaultData = util.encrypt(app.data.sequenceControl, obj.lenData, true); - var value = util.writeData(util.PACKAGE_CONTROL_VALUE, util.SUBTYPE_WIFI_MODEl, frameControl, app.data.sequenceControl, obj.len, obj.lenData); - var typedArray = new Uint8Array(value) - wx.writeBLECharacteristicValue({ - deviceId: deviceId, - serviceId: serviceId, - characteristicId: characteristicId, - value: typedArray.buffer, - success: function (res) { - if (obj.flag) { - self.writeDeviceStart(deviceId, serviceId, characteristicId, obj.laveData); - } else { - self.setProcess(60, util.descSucList[5]); - self.writeRouterSsid(deviceId, serviceId, characteristicId, null); - } - }, - fail: function (res) { - self.setFailProcess(true, util.descFailList[3]); - } - }) - }, - //写入路由ssid - writeRouterSsid: function (deviceId, serviceId, characteristicId, data) { - var self = this, obj = {}, frameControl = 0; - app.data.sequenceControl = parseInt(app.data.sequenceControl) + 1; - if (!util._isEmpty(data)) { - obj = util.isSubcontractor(data, self.data.isChecksum, app.data.sequenceControl, self.data.isEncrypt); - frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag); - } else { - var ssidData = self.getCharCodeat(self.data.ssid); - obj = util.isSubcontractor(ssidData, self.data.isChecksum, app.data.sequenceControl, self.data.isEncrypt); - frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag); - } - // var defaultData = util.encrypt(app.data.sequenceControl, obj.lenData, true); - var value = util.writeData(util.PACKAGE_VALUE, util.SUBTYPE_SET_SSID, frameControl, app.data.sequenceControl, obj.len, obj.lenData); - var typedArray = new Uint8Array(value) - wx.writeBLECharacteristicValue({ - deviceId: deviceId, - serviceId: serviceId, - characteristicId: characteristicId, - value: typedArray.buffer, - success: function (res) { - if (obj.flag) { - self.writeRouterSsid(deviceId, serviceId, characteristicId, obj.laveData); - } else { - self.writeDevicePwd(deviceId, serviceId, characteristicId, null); - } - }, - fail: function (res) { - self.setFailProcess(true, util.descFailList[4]); - } - }) - }, - //写入路由密码 - writeDevicePwd: function (deviceId, serviceId, characteristicId, data) { - var self = this, obj = {}, frameControl = 0; - app.data.sequenceControl = parseInt(app.data.sequenceControl) + 1; - if (!util._isEmpty(data)) { - obj = util.isSubcontractor(data, self.data.isChecksum, app.data.sequenceControl, self.data.isEncrypt); - frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag); - } else { - var pwdData = self.getCharCodeat(self.data.password); - obj = util.isSubcontractor(pwdData, self.data.isChecksum, app.data.sequenceControl, self.data.isEncrypt); - frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag); - } - // var defaultData = util.encrypt(app.data.sequenceControl, obj.lenData, true); - var value = util.writeData(util.PACKAGE_VALUE, util.SUBTYPE_SET_PWD, frameControl, app.data.sequenceControl, obj.len, obj.lenData); - var typedArray = new Uint8Array(value) - wx.writeBLECharacteristicValue({ - deviceId: deviceId, - serviceId: serviceId, - characteristicId: characteristicId, - value: typedArray.buffer, - success: function (res) { - if (obj.flag) { - self.writeDevicePwd(deviceId, serviceId, characteristicId, obj.laveData); - } else { - self.writeDeviceEnd(deviceId, serviceId, characteristicId); - } - }, - fail: function (res) { - self.setFailProcess(true, util.descFailList[4]); - } - }) - }, - //告知设备写入结束 - writeDeviceEnd: function (deviceId, serviceId, characteristicId) { - var self = this; - app.data.sequenceControl = parseInt(app.data.sequenceControl) + 1; - var frameControl = util.getFrameCTRLValue(self.data.isEncrypt, false, util.DIRECTION_OUTPUT, false, false); - var value = util.writeData(self.data.PACKAGE_CONTROL_VALUE, util.SUBTYPE_END, frameControl, app.data.sequenceControl, 0, null); - var typedArray = new Uint8Array(value) - wx.writeBLECharacteristicValue({ - deviceId: deviceId, - serviceId: serviceId, - characteristicId: characteristicId, - value: typedArray.buffer, - success: function (res) { - self.onTimeout(0); - }, - fail: function (res) { - self.setFailProcess(true, util.descFailList[4]); - } - }) - }, - //连接超时 - onTimeout: function(num) { - const self = this; - timeId = setInterval(function() { - if (num < timeOut) { - num++; - } else { - clearInterval(timeId); - self.setFailProcess(true, util.descFailList[4]); - } - }, 1000) - }, - //监听通知 - onNotify: function () { - var self = this; - wx.onBLECharacteristicValueChange(function (res) { - self.getResultType(util.ab2hex(res.value)); - }) - }, - //启用通知 - openNotify: function (deviceId, serviceId, characteristicId) { - var self = this; - wx.notifyBLECharacteristicValueChange({ - state: true, // 启用 notify 功能 - deviceId: deviceId, - serviceId: serviceId, - characteristicId: app.data.characteristic_read_uuid, - success: function (res) { - self.writeDeviceStart(deviceId, serviceId, characteristicId); - self.onNotify(); - }, - fail: function (res) { - } - }) - }, - getSsids: function (str) { - var list = [], - strs = str.split(":"); - for (var i = 0; i < strs.length; i++) { - list.push(parseInt(strs[i], 16)); - } - return list; - }, - getCharCodeat: function (str) { - var list = []; - for (var i = 0; i < str.length; i++) { - list.push(str.charCodeAt(i)); - } - return list; - }, - setProcess: function(value, desc) { - var self = this, list = []; - list = self.data.processList; - list.push(desc); - self.setData({ - value: value, - processList: list - }); - if (value == 100) { - self.closeConnect(); - self.setData({ - desc: util.descSucList[6] - }); - clearInterval(timeId); - app.data.sequenceControl = 0; - setTimeout(function () { - wx.reLaunch({ - url: '/pages/index/index' - }) - }, 3000) - } - }, - setFailProcess: function (flag, desc) { - var self = this, list = []; - list = self.data.processList; - list.push(desc); - self.setFailBg(); - self.setData({ - failure: flag, - processList: list - }); - }, - getResultType: function(list) { - var self = this; - var result = self.data.result; - console.log(list) - if (list.length < 4) { - self.setFailProcess(true, util.descFailList[4]); - return false; - } - var val = parseInt(list[0], 16), - type = val & 3, - subType = val >> 2; - console.log(type, subType, self.data.flagEnd) - if (type != parseInt(util.PACKAGE_VALUE)) { - self.setFailProcess(true, util.descFailList[4]); - return false; - } - var sequenceNum = parseInt(list[2], 16); - if (sequenceNum - app.data.sequenceNumber != 1) { - self.setFailProcess(true, util.descFailList[4]); - return false; - } - app.data.sequenceNumber = sequenceNum; - if (app.data.sequenceNumber == 255) { - app.data.sequenceNumber = -1 - } - var dataLength = parseInt(list[3], 16); - if (dataLength == 0) { - self.setFailProcess(true, util.descFailList[4]); - return false; - } - var fragNum = util.hexToBinArray(list[1]); - list = util.isEncrypt(self, fragNum, list); - result = result.concat(list); - self.setData({ - result: result, - }) - if (self.data.flagEnd) { - self.setData({ - flagEnd: false, - }) - if (type == 1) { - if (subType == 15) { - for (var i = 0; i <= result.length; i++) { - var num = parseInt(result[i], 16) + ""; - if (i == 0) { - self.setProcess(85, "Connected: " + util.successList[num]); - } else if (i == 1) { - if (num == 0) { - self.setProcess(100, util.descSucList[6]); - } - } - } - } else if (subType == 18) { - for (var i = 0; i <= result.length; i++) { - var num = parseInt(result[i], 16) + ""; - if (i == 0) { - self.setProcess(85, util.successList[num]); - } else if (i == 1) { - self.setFailProcess(true, util.failList[num]); - } - } - } else { - self.setFailProcess(true, util.descFailList[4]) - } - } else { - self.setFailProcess(true, util.descFailList[4]) - } - } - - - }, - closeConnect: function () { - var self = this; - wx.closeBLEConnection({ - deviceId: self.data.deviceId, - success: function (res) { - } - }) - wx.closeBluetoothAdapter({ - success: function() { - } - }); - }, - //设置配网失败背景色 - setFailBg: function() { - wx.setNavigationBarColor({ - frontColor: "#ffffff", - backgroundColor: '#737d89', - }) - }, - //设置配网成功背景色 - setSucBg: function() { - wx.setNavigationBarColor({ - frontColor: "#ffffff", - backgroundColor: '#4d9efb', - }) - }, - onLoad: function (options) { - var self = this; - self.setSucBg(); - wx.setNavigationBarTitle({ - title: '配网' - }); - self.setData({ - deviceId: options.deviceId, - ssid: unescape(options.ssid), - password: unescape(options.password), - meshId: options.bssid, - uuid: options.uuid, - serviceId: options.serviceId, - }) - self.blueConnect(); - }, - /** - * 生命周期函数--监听页面初次渲染完成 - */ - onReady: function () { - - }, - /** - * 生命周期函数--监听页面显示 - */ - onShow: function () { - - }, - - /** - * 生命周期函数--监听页面隐藏 - */ - onHide: function () { - }, - - /** - * 生命周期函数--监听页面卸载 - */ - onUnload: function () { - - }, - - /** - * 页面相关事件处理函数--监听用户下拉动作 - */ - onPullDownRefresh: function () { - - }, - - /** - * 页面上拉触底事件的处理函数 - */ - onReachBottom: function () { - - }, - -}) diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueConnect/blueConnect.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueConnect/blueConnect.json deleted file mode 100644 index 9e26dfe..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueConnect/blueConnect.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueConnect/blueConnect.wxml b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueConnect/blueConnect.wxml deleted file mode 100644 index e2378a8..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueConnect/blueConnect.wxml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - {{value}}% - {{desc}} - - - - - - - - - - ! - {{desc}} - - - - - - {{item}} - {{vConsoleV}} - - diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueConnect/blueConnect.wxss b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueConnect/blueConnect.wxss deleted file mode 100644 index f9ac275..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueConnect/blueConnect.wxss +++ /dev/null @@ -1,68 +0,0 @@ -/* pages/blueConnect/blueConnect.wxss */ -.container { - padding: 0; -} -.schedule { - background: #4d9efb; - height: 46vh; - width: 100%; - padding: 50rpx; -} -.schedule-result { - position: relative; - display: flex; - justify-content: center; - align-items: center; - border-radius: 100%; - background: transparent; - height: 55vw; - width: 55vw; - margin: 0 auto; - color: #fff; -} -.schedule-img { - height: 55vw; - width: 55vw; -} -.fail{ - background: #737d89; -} -.desc-wrapper { - position: absolute; -} -.text-value { - text-align: center; - font-size: 20px; -} -.text-desc { - font-size: 14px; -} -.text-samll { - font-size: 16px; -} -.text-sigh { - height: 80rpx; - width: 80rpx; - background: #fff; - border-radius: 100%; - display: flex; - justify-content: center; - align-items: center; - font-size: 20px; - font-weight: bold; - color: #737d89; - margin: 0 auto 10px; -} -.process-wrapper { - display: flex; - flex-direction: column; - width: 100%; - text-align: left; - margin-top: 20px; - padding-left: 30px; - font-size: 14px; - color: #999; -} -.text-process { - margin: 3px 0; -} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueDevices/blueDevices.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueDevices/blueDevices.js deleted file mode 100644 index 5d989f6..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueDevices/blueDevices.js +++ /dev/null @@ -1,105 +0,0 @@ - -//获取应用实例 -const app = getApp(); -const util = require('../../utils/util.js'); -Page({ - data: { - deviceList: [], - deviceId: "", - }, - bindViewConnect: function (event) { - var self = this, - deviceId = event.currentTarget.dataset.value; - self.setData({ - deviceId: deviceId - }); - wx.navigateTo({ - url: '/pages/blueWifi/blueWifi?deviceId=' + deviceId, - }) - }, - - getBluDevice: function () { - var self = this; - wx.getBluetoothDevices({ - success: function (res) { - var list = util.filterDevice(res.devices, "name"); - if (list.length > 0) { - wx.hideLoading(); - } - self.setData({ - deviceList: list - }) - } - }) - wx.onBluetoothDeviceFound(function (res) { - console.log(res.devices[0].name); - var list = util.filterDevice(res.devices, "name"); - if (list.length > 0) { - wx.hideLoading(); - } - self.setData({ - deviceList: self.data.deviceList.concat(list) - }) - - }) - }, - - onLoad: function () { - var self = this; - wx.setNavigationBarTitle({ - title: 'BluFi扫描' - }); - wx.showLoading({ - title: '设备扫描中...', - }) - self.getBluDevice(); - }, - /** - * 生命周期函数--监听页面初次渲染完成 - */ - onReady: function () { - - }, - - /** - * 生命周期函数--监听页面显示 - */ - onShow: function () { - var self = this, - deviceId = self.data.deviceId; - if (!util._isEmpty(deviceId)) { - wx.closeBLEConnection({ - deviceId: deviceId, - }) - } - }, - - /** - * 生命周期函数--监听页面隐藏 - */ - onHide: function () { - - }, - - /** - * 生命周期函数--监听页面卸载 - */ - onUnload: function () { - - }, - - /** - * 页面相关事件处理函数--监听用户下拉动作 - */ - onPullDownRefresh: function () { - - }, - - /** - * 页面上拉触底事件的处理函数 - */ - onReachBottom: function () { - - }, - -}) diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueDevices/blueDevices.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueDevices/blueDevices.json deleted file mode 100644 index 9e26dfe..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueDevices/blueDevices.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueDevices/blueDevices.wxml b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueDevices/blueDevices.wxml deleted file mode 100644 index 4aa7d13..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueDevices/blueDevices.wxml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - {{item.name}} - Rssi: {{item.RSSI}} - deviceId: {{item.deviceId}} - - - diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueDevices/blueDevices.wxss b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueDevices/blueDevices.wxss deleted file mode 100644 index d944cfc..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueDevices/blueDevices.wxss +++ /dev/null @@ -1,11 +0,0 @@ -/* pages/blueDevices/blueDevices.wxss */ -.prompt-title { - height: 50rpx; - border-top: 1px solid #999; - border-bottom: 1px solid #999; - width: 100%; -} -.item { - height: auto; - padding: 8rpx 0; -} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueWifi/blueWifi.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueWifi/blueWifi.js deleted file mode 100644 index 396b7c1..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueWifi/blueWifi.js +++ /dev/null @@ -1,476 +0,0 @@ -// pages/blueWifi/blueWifi.js -//获取应用实例 -const app = getApp(); -const util = require('../../utils/util.js'); -const crypto = require('../../crypto/crypto-dh.js'); -const md5 = require('../../crypto/md5.min.js'); -var client = ""; -Page({ - - /** - * 页面的初始数据 - */ - data: { - deviceId: "", - wifiList: [], - hiddenModal: true, - blueConnectNum: 0, - frag: 16, - flag: false, - flagEnd: false, - fragList: [], - frameControl: 0, - ssid: "", - bssid: "", - serviceId: "", - uuid: "", - password: "", - }, - bindViewWifi: function(event) { - var self = this, - ssid = event.currentTarget.dataset.ssid, - bssid = event.currentTarget.dataset.bssid; - self.setData({ - hiddenModal: false, - ssid: ssid, - bssid: bssid, - password: "" - }) - }, - bindViewConfirm: function() { - var self = this; - wx.navigateTo({ - url: '/pages/blueConnect/blueConnect?deviceId=' + self.data.deviceId + "&ssid=" + escape(self.data.ssid) + "&bssid=" + self.data.bssid + "&password=" + escape(self.data.password) + "&serviceId=" + self.data.serviceId + "&uuid=" + self.data.uuid, - }) - this.setData({ - hiddenModal: true, - }) - }, - bindViewCancel: function(){ - this.setData({ - hiddenModal: true, - ssid: "", - bssid: "", - password: "" - }) - }, - bindViewInput: function (e) { - this.setData({ password: e.detail.value }) - }, - blueConnect: function (event) { - var self = this; - app.data.sequenceControl = -1; - app.data.sequenceNumber = -1; - self.setData({ - fragList: [], - wifiList: [], - flagEnd: false, - serviceId: "", - uuid: "" - }); - wx.createBLEConnection({ - deviceId: self.data.deviceId, - timeout: 10000, - success: function (res) { - console.info("app.data.platform", app.data.platform) - if (app.data.platform == 'android') { - app.data.mtu = app.data.constMtu; - wx.setBLEMTU({ - deviceId: self.data.deviceId, - mtu: app.data.constMtu, - success (res) { - console.info('setBLEMTU-suc', res) - }, - fail (res) { - console.info('setBLEMTU-fail', res) - } - }) - } - self.getDeviceServices(self.data.deviceId); - }, - fail: function (res) { - var num = self.data.blueConnectNum; - if (num < 3) { - self.blueConnect(); - num++; - self.setData({ - blueConnectNum: num - }) - } else { - self.showFailToast(); - } - } - }) - }, - getDeviceServices: function (deviceId) { - var self = this; - wx.getBLEDeviceServices({ - deviceId: deviceId, - success: function (res) { - var services = res.services; - if (services.length > 0) { - for (var i = 0; i < services.length; i++) { - var uuid = services[i].uuid; - if (uuid == app.data.service_uuid) { - self.getDeviceCharacteristics(deviceId, uuid); - return false; - } - } - } - }, - fail: function (res) { - self.showFailToast(); - } - }) - }, - getDeviceCharacteristics: function (deviceId, serviceId) { - var self = this; - wx.getBLEDeviceCharacteristics({ - deviceId: deviceId, - serviceId: serviceId, - success: function (res) { - var list = res.characteristics; - if (list.length > 0) { - for (var i = 0; i < list.length; i++) { - var uuid = list[i].uuid; - if (uuid == app.data.characteristic_write_uuid) { - self.openNotify(deviceId, serviceId, uuid); - self.setData({ - serviceId: serviceId, - uuid: uuid, - }) - return false; - } - } - } - }, - fail: function (res) { - self.showFailToast(); - } - }) - }, - //通知设备交互方式(是否加密) - notifyDevice: function (deviceId, serviceId, characteristicId) { - var self = this; - client = util.blueDH(util.DH_P, util.DH_G, crypto); - var kBytes = util.uint8ArrayToArray(client.getPublicKey()); - var pBytes = util.hexByInt(util.DH_P); - var gBytes = util.hexByInt(util.DH_G); - var pgkLength = pBytes.length + gBytes.length + kBytes.length + 6; - var pgkLen1 = (pgkLength >> 8) & 0xff; - var pgkLen2 = pgkLength & 0xff; - var data = []; - data.push(util.NEG_SET_SEC_TOTAL_LEN); - data.push(pgkLen1); - data.push(pgkLen2); - var frameControl = util.getFrameCTRLValue(false, false, util.DIRECTION_OUTPUT, false, false); - var value = util.writeData(util.PACKAGE_VALUE, util.SUBTYPE_NEG, frameControl, app.data.sequenceControl, data.length, data); - var typedArray = new Uint8Array(value); - console.log("notifyDevice:", value) - wx.writeBLECharacteristicValue({ - deviceId: deviceId, - serviceId: serviceId, - characteristicId: characteristicId, - value: typedArray.buffer, - success: function (res) { - console.log("notifyDevice-suc:", res) - self.getSecret(deviceId, serviceId, characteristicId, client, kBytes, pBytes, gBytes, null); - }, - fail: function (res) { - console.log("notifyDevice-fail:", res) - self.showFailToast(); - } - }) - }, - getSecret: function (deviceId, serviceId, characteristicId, client, kBytes, pBytes, gBytes, data) { - var self = this, obj = {}, frameControl = 0; - app.data.sequenceControl = parseInt(app.data.sequenceControl) + 1; - const encrypt = false - const checksum = false - if (!util._isEmpty(data)) { - obj = util.isSubcontractor(data, checksum, app.data.sequenceControl); - frameControl = util.getFrameCTRLValue(encrypt, checksum, util.DIRECTION_OUTPUT, false, obj.flag); - } else { - data = []; - data.push(util.NEG_SET_SEC_ALL_DATA); - var pLength = pBytes.length; - var pLen1 = (pLength >> 8) & 0xff; - var pLen2 = pLength & 0xff; - data.push(pLen1); - data.push(pLen2); - data = data.concat(pBytes); - var gLength = gBytes.length; - var gLen1 = (gLength >> 8) & 0xff; - var gLen2 = gLength & 0xff; - data.push(gLen1); - data.push(gLen2); - data = data.concat(gBytes); - var kLength = kBytes.length; - var kLen1 = (kLength >> 8) & 0xff; - var kLen2 = kLength & 0xff; - data.push(kLen1); - data.push(kLen2); - data = data.concat(kBytes); - obj = util.isSubcontractor(data, checksum, app.data.sequenceControl); - frameControl = util.getFrameCTRLValue(encrypt, checksum, util.DIRECTION_OUTPUT, false, obj.flag); - } - var value = util.writeData(util.PACKAGE_VALUE, util.SUBTYPE_NEG, frameControl, app.data.sequenceControl, obj.len, obj.lenData); - var typedArray = new Uint8Array(value); - wx.writeBLECharacteristicValue({ - deviceId: deviceId, - serviceId: serviceId, - characteristicId: characteristicId, - value: typedArray.buffer, - success: function (res) { - console.log('getSecret-suc', res) - if (obj.flag) { - self.getSecret(deviceId, serviceId, characteristicId, client, kBytes, pBytes, gBytes, obj.laveData); - } else { - // setTimeout(function(){ - // self.getWifiList(deviceId, serviceId, characteristicId); - // }, 3000) - self.setSecret(deviceId, serviceId, characteristicId); - } - }, - fail: function (res) { - console.log('getSecret-error', res) - self.showFailToast(); - } - }) - }, - setSecret (deviceId, serviceId, characteristicId) { - const self = this; - let value = 0; - // value |= 1; // 数据包校验 - // value |= 0b10; //数据包加密 - let data = [value] - app.data.sequenceControl = parseInt(app.data.sequenceControl) + 1; - let frameControl = util.getFrameCTRLValue(false, false, util.DIRECTION_OUTPUT, false, false); - value = util.writeData(util.PACKAGE_CONTROL_VALUE, util.SUBTYPE_SET_SEC_MODE, frameControl, app.data.sequenceControl, data.length, data); - var typedArray = new Uint8Array(value); - wx.writeBLECharacteristicValue({ - deviceId: deviceId, - serviceId: serviceId, - characteristicId: characteristicId, - value: typedArray.buffer, - success: function (res) { - setTimeout(function(){ - self.getWifiList(deviceId, serviceId, characteristicId); - }, 3000) - }, - fail: function (res) { - self.showFailToast(); - } - }) - }, - getWifiList: function (deviceId, serviceId, characteristicId) { - var self = this; - var frameControl = util.getFrameCTRLValue(false, false, util.DIRECTION_OUTPUT, false, false); - app.data.sequenceControl = parseInt(app.data.sequenceControl) + 1; - var value = util.writeData(util.PACKAGE_CONTROL_VALUE, util.SUBTYPE_WIFI_NEG, frameControl, app.data.sequenceControl, 0, null); - var typedArray = new Uint8Array(value); - wx.writeBLECharacteristicValue({ - deviceId: deviceId, - serviceId: serviceId, - characteristicId: characteristicId, - value: typedArray.buffer, - success: function (res) { - }, - fail: function (res) { - self.showFailToast(); - } - }) - }, - // 监听特征值变化 - onNotify: function () { - var self = this; - wx.onBLECharacteristicValueChange(function (res) { - self.analysisWifi(util.ab2hex(res.value)); - }) - }, - // 启用特征值变化 - openNotify: function (deviceId, serviceId, characteristicId) { - var self = this; - wx.notifyBLECharacteristicValueChange({ - state: true, // 启用 notify 功能 - deviceId: deviceId, - serviceId: serviceId, - characteristicId: app.data.characteristic_read_uuid, - success: function (res) { - //通知设备交互方式(是否加密) - // self.notifyDevice(deviceId, serviceId, characteristicId); - self.onNotify(); - self.getWifiList(deviceId, serviceId, characteristicId); - }, - fail: function (res) { - self.showFailToast(); - } - }) - }, - analysisWifi: function (list) { - const self = this; - var fragList = self.data.fragList; - if (list.length < 4) { - return false; - } - var val = list[0], - type = val & 3, - subType = val >> 2; - if (type != parseInt(util.PACKAGE_VALUE)) { - wx.hideLoading(); - return false; - } - var sequenceNum = parseInt(list[2], 16); - if (sequenceNum - app.data.sequenceNumber != 1) { - wx.hideLoading(); - return false; - } - app.data.sequenceNumber = sequenceNum; - if (app.data.sequenceNumber == 255) { - app.data.sequenceNumber = -1 - } - var dataLength = parseInt(list[3], 16); - if (dataLength == 0) { - wx.hideLoading(); - return false; - } - console.log('fragNum', list) - var fragNum = util.hexToBinArray(list[1]); - list = util.isEncrypt(self, fragNum, list); - fragList = fragList.concat(list); - self.setData({ - fragList: fragList, - }) - if (self.data.flagEnd) { - if (subType == util.SUBTYPE_WIFI_LIST_NEG) { - self.getList(fragList, fragList.length, 0); - wx.hideLoading(); - } else if (subType == util.SUBTYPE_NEGOTIATION_NEG) { - var arr = util.hexByInt(fragList.join("")); - var clientSecret = client.computeSecret(new Uint8Array(arr)); - var md5Key = md5.array(clientSecret); - app.data.md5Key = md5Key; - self.setData({ - fragList: [], - }) - } else { - wx.hideLoading(); - } - self.setData({ - flagEnd: false, - }) - } - }, - getList: function (arr, totalLength, curLength) { - var self = this; - if (arr.length > 0) { - var len = parseInt(arr[0], 16); - curLength += (1 + len); - if (len > 0 && curLength < totalLength) { - var rssi = 0, name = ""; - let list = [] - for (var i = 1; i <= len; i++) { - if (i == 1) { - rssi = parseInt(arr[i], 16); - } else { - list.push(parseInt(arr[i], 16)) - } - } - name = decodeURIComponent(encodeURIComponent(String.fromCharCode(...list))) - var wifiList = self.data.wifiList; - wifiList.push({ "rssi": rssi, "SSID": name }); - self.setData({ - wifiList: wifiList.sort(util.sortBy("rssi", false)) - }) - arr = arr.splice(len + 1); - this.getList(arr, totalLength, curLength); - } - } - }, - - showFailToast: function() { - wx.hideLoading(); - wx.showToast({ - title: 'WiFi信息获取失败', - icon: 'none', - duration: 2000 - }) - }, - /** - * 生命周期函数--监听页面加载 - */ - onLoad: function (options) { - var self = this; - wx.setNavigationBarTitle({ - title: '选择WiFi' - }); - self.setData({ - deviceId: options.deviceId, - }) - wx.showLoading({ - title: 'WiFi获取中...', - }) - wx.stopBluetoothDevicesDiscovery({ - success: function (res) { - } - }) - self.blueConnect(); - }, - - /** - * 生命周期函数--监听页面初次渲染完成 - */ - onReady: function () { - - }, - - /** - * 生命周期函数--监听页面显示 - */ - onShow: function () { - - }, - - /** - * 生命周期函数--监听页面隐藏 - */ - onHide: function () { - - }, - - /** - * 生命周期函数--监听页面卸载 - */ - onUnload: function () { - - }, - - /** - * 页面相关事件处理函数--监听用户下拉动作 - */ - onPullDownRefresh: function () { - var self = this; - self.setData({ - wifiList: [], - fragList: [] - }); - self.getWifiList(self.data.deviceId, self.data.serviceId, self.data.uuid); - setTimeout(function () { - wx.stopPullDownRefresh(); - }, 6000); - }, - - /** - * 页面上拉触底事件的处理函数 - */ - onReachBottom: function () { - - }, - - /** - * 用户点击右上角分享 - */ - onShareAppMessage: function () { - - } -}) \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueWifi/blueWifi.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueWifi/blueWifi.json deleted file mode 100644 index 6b33b19..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueWifi/blueWifi.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "backgroundTextStyle": "dark", - "enablePullDownRefresh": true -} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueWifi/blueWifi.wxml b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueWifi/blueWifi.wxml deleted file mode 100644 index f4015ec..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueWifi/blueWifi.wxml +++ /dev/null @@ -1,19 +0,0 @@ - - - - {{item.SSID}} - - - - - - - - \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueWifi/blueWifi.wxss b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueWifi/blueWifi.wxss deleted file mode 100644 index 3e2ada1..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/blueWifi/blueWifi.wxss +++ /dev/null @@ -1,12 +0,0 @@ -/* pages/blueWifi/blueWifi.wxss */ -.item-icon { - flex: 0 0 80rpx; - height: 100%; - display: flex; - justify-items: center; - align-items: center; -} -.wifi-img { - width: 60rpx; - height: 60rpx; -} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/index/index.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/index/index.js deleted file mode 100644 index 43213c8..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/index/index.js +++ /dev/null @@ -1,57 +0,0 @@ -//index.js -//获取应用实例 -const app = getApp(); -const util = require('../../utils/util.js'); -Page({ - data: { - - }, - //事件处理函数 - bindViewBlue: function() { - wx.closeBluetoothAdapter({ - success: function () { - } - }); - wx.openBluetoothAdapter({ - success: function (res) { - wx.startBluetoothDevicesDiscovery({ - success: function (res) { - wx.navigateTo({ - url: '/pages/blueDevices/blueDevices', - }) - } - }) - }, - fail: function (res) { - wx.showToast({ - title: '请打开蓝牙', - icon: 'none', - duration: 2000 - }) - } - }) - - }, - bindViewWifi: function () { - wx.navigateTo({ - url: '/pages/wifiDevices/wifiDevices', - }) - }, - onLoad: function () { - wx.setNavigationBarTitle({ - title: '配网方式' - }) - wx.getSystemInfo({ - success (res) { - try { - app.data.platform = res.platform.toLocaleLowerCase() - } catch(e) { - console.log(e) - } - } - }) - }, - getUserInfo: function(e) { - } - -}) diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/index/index.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/index/index.json deleted file mode 100644 index 9e26dfe..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/index/index.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/index/index.wxml b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/index/index.wxml deleted file mode 100644 index dcaeaf0..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/index/index.wxml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - 配网方式 - - - - BluFi 配网 - - - - - diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/index/index.wxss b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/index/index.wxss deleted file mode 100644 index 8dfcb41..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/index/index.wxss +++ /dev/null @@ -1,60 +0,0 @@ -.container { - display: block; - padding: 0; - height: 100vh; - background: #eee; -} -.header-wrapper { - display: flex; - justify-content: center; - align-items: center; - height: 45vh; - width: 100%; - background: #4d9efb; - border-bottom-right-radius: 40px; - border-bottom-left-radius: 40px; -} -.header-image { - width: 29vh; - height: 29vh; - position: relative; - top: -5vh; -} -.content-wrapper { - position: absolute; - top: 38vh; - left: 15px; - height: 50vh; - width: calc(100% - 30px); - padding: 20px; - box-sizing: border-box; - background: #fff; - border-radius: 10px; - box-shadow: 0px 0px 3px #fff; - color: #818181; -} -.title { - margin: 10px 0; - text-align: center; - font-size: 16px; -} -.btn-info { - display: flex; - margin-top: 10vh; -} -.btn-image { - width: 50px; - height: 50px; -} -.select-way { - flex: 1; - display: flex; - justify-content: center; - align-items: center; - flex-direction: column; -} -.name { - margin: 10px 0; - font-size: 14px; -} - diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConfig/wifiConfig.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConfig/wifiConfig.js deleted file mode 100644 index 35f14f5..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConfig/wifiConfig.js +++ /dev/null @@ -1,72 +0,0 @@ -// pages/wifiConfig/wifiConfig.js -Page({ - - /** - * 页面的初始数据 - */ - data: { - - }, - next: function () { - wx.navigateTo({ - url: "/pages/wifiList/wifiList" - }) - - }, - /** - * 生命周期函数--监听页面加载 - */ - onLoad: function (options) { - wx.setNavigationBarTitle({ - title: 'WiFi配网' - }); - }, - /** - * 生命周期函数--监听页面初次渲染完成 - */ - onReady: function () { - - }, - - /** - * 生命周期函数--监听页面显示 - */ - onShow: function () { - - }, - - /** - * 生命周期函数--监听页面隐藏 - */ - onHide: function () { - - }, - - /** - * 生命周期函数--监听页面卸载 - */ - onUnload: function () { - - }, - - /** - * 页面相关事件处理函数--监听用户下拉动作 - */ - onPullDownRefresh: function () { - - }, - - /** - * 页面上拉触底事件的处理函数 - */ - onReachBottom: function () { - - }, - - /** - * 用户点击右上角分享 - */ - onShareAppMessage: function () { - - } -}) \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConfig/wifiConfig.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConfig/wifiConfig.json deleted file mode 100644 index 9e26dfe..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConfig/wifiConfig.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConfig/wifiConfig.wxml b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConfig/wifiConfig.wxml deleted file mode 100644 index 04426e2..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConfig/wifiConfig.wxml +++ /dev/null @@ -1,20 +0,0 @@ - - - - 第二步 - 将手机连接设备,请前往手机设置,进入无线局域网 - - - - - - - - - - - - - - - diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConfig/wifiConfig.wxss b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConfig/wifiConfig.wxss deleted file mode 100644 index 3b3bcd7..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConfig/wifiConfig.wxss +++ /dev/null @@ -1,9 +0,0 @@ -/* pages/wifiConfig/wifiConfig.wxss */ -.container{ - padding-top: 50rpx; -} -.wifi-img { - width:300px; - height:234px; - margin-top:20px; -} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConnect/wifiConnect.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConnect/wifiConnect.js deleted file mode 100644 index b701d56..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConnect/wifiConnect.js +++ /dev/null @@ -1,166 +0,0 @@ -// pages/wifiConnect/wifiConnect.js - -Page({ - - /** - * 页面的初始数据 - */ - data: { - failStatus: false, - sucStatus: false, - progress: 0, - }, - getStatus: function() { - var self = this; - - wx.request({ - url: 'https://www.alavening.com/wifi/connstatus.cgi', - data: { - }, - header: { - 'content-type': 'application/json' // 默认值 - }, - success: function (res) { - console.log(res.data); - var data = res.data; - console.log(data.status); - if (data.status == "success") { - self.setData({ - progress: 100, - failStatus: false, - sucStatus: true, - }) - self.setResult("success"); - } else if (data.status == "fail") { - self.setData({ - failStatus: true, - sucStatus: false, - }) - self.setResult("fail"); - } else { - if (self.data.progress <= 90) { - self.setData({ - progress: (self.data.progress + 5), - failStatus: false, - sucStatus: false, - }) - } - self.getStatus(); - } - }, - fail: function (res) { - self.setData({ - failStatus: true, - sucStatus: false, - }) - - } - }) - - }, - setResult: function(result) { - var self = this; - wx.request({ - url: 'https://www.alavening.com/wifi/configsuccess.cgi', - method: "post", - data: { - configstatus: result - }, - header: { - 'content-type': 'application/x-www-form-urlencoded' - }, - success: function (res) { - }, - fail: function () { - } - }) - }, - setConnect: function(ssid, password) { - var self = this; - wx.request({ - url: 'https://alavening.com/wifi/connect.cgi', - method: "post", - data: { - essid: ssid, - password: password - }, - header: { - 'content-type': 'application/x-www-form-urlencoded' - }, - success: function (res) { - - }, - fail: function () { - } - }) - self.getStatus(); - }, - successwifi: function() { - wx.reLaunch({ - url: '/pages/index/index' - }) - }, - failwifi: function() { - wx.navigateBack({ - delta: 2 - }) - }, - /** - * 生命周期函数--监听页面加载 - */ - onLoad: function (options) { - var self = this; - wx.setNavigationBarTitle({ - title: 'WiFi配网' - }); - self.setConnect(options.ssid, options.password); - }, - - /** - * 生命周期函数--监听页面初次渲染完成 - */ - onReady: function () { - - }, - - /** - * 生命周期函数--监听页面显示 - */ - onShow: function () { - - }, - - /** - * 生命周期函数--监听页面隐藏 - */ - onHide: function () { - }, - - /** - * 生命周期函数--监听页面卸载 - */ - onUnload: function () { - - }, - - /** - * 页面相关事件处理函数--监听用户下拉动作 - */ - onPullDownRefresh: function () { - - }, - - /** - * 页面上拉触底事件的处理函数 - */ - onReachBottom: function () { - - }, - - /** - * 用户点击右上角分享 - */ - onShareAppMessage: function () { - - } -}) \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConnect/wifiConnect.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConnect/wifiConnect.json deleted file mode 100644 index 9e26dfe..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConnect/wifiConnect.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConnect/wifiConnect.wxml b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConnect/wifiConnect.wxml deleted file mode 100644 index 5ead5fb..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConnect/wifiConnect.wxml +++ /dev/null @@ -1,13 +0,0 @@ - - - - 配网中... - - - - - - - - - diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConnect/wifiConnect.wxss b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConnect/wifiConnect.wxss deleted file mode 100644 index c4cfecd..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiConnect/wifiConnect.wxss +++ /dev/null @@ -1,13 +0,0 @@ -/* pages/wifiConnect/wifiConnect.wxss */ -.container { - padding-top: 50rpx; -} -.progress-wrapper { - width: 100%; - height: 40px; - display: flex; - align-items: center; -} -progress { - flex: 1; -} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiDevices/wifiDevices.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiDevices/wifiDevices.js deleted file mode 100644 index e8f1d15..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiDevices/wifiDevices.js +++ /dev/null @@ -1,67 +0,0 @@ -// pages/wifiDevices/wifiDevices.js -Page({ - - /** - * 页面的初始数据 - */ - data: { - - }, - next: function () { - wx.navigateTo({ - url: "/pages/wifiConfig/wifiConfig" - }) - - }, - - /** - * 生命周期函数--监听页面加载 - */ - onLoad: function (options) { - wx.setNavigationBarTitle({ - title: 'WiFi配网' - }); - }, - - /** - * 生命周期函数--监听页面初次渲染完成 - */ - onReady: function () { - - }, - - /** - * 生命周期函数--监听页面显示 - */ - onShow: function () { - - }, - - /** - * 生命周期函数--监听页面隐藏 - */ - onHide: function () { - - }, - - /** - * 生命周期函数--监听页面卸载 - */ - onUnload: function () { - - }, - - /** - * 页面相关事件处理函数--监听用户下拉动作 - */ - onPullDownRefresh: function () { - - }, - - /** - * 页面上拉触底事件的处理函数 - */ - onReachBottom: function () { - - }, -}) \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiDevices/wifiDevices.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiDevices/wifiDevices.json deleted file mode 100644 index 9e26dfe..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiDevices/wifiDevices.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiDevices/wifiDevices.wxml b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiDevices/wifiDevices.wxml deleted file mode 100644 index 3b4ae60..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiDevices/wifiDevices.wxml +++ /dev/null @@ -1,23 +0,0 @@ - - - - 第一步 - 双击设备按键 - - - - - - - - - - - - - - - - diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiDevices/wifiDevices.wxss b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiDevices/wifiDevices.wxss deleted file mode 100644 index 74048aa..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiDevices/wifiDevices.wxss +++ /dev/null @@ -1,20 +0,0 @@ -/* pages/wifi/wifi.wxss */ -.container { - padding-top: 50rpx; -} -.checkbox{ - color: #333 -} -.back{ - width: 40%; - margin: 0 auto -} -.colored{ - color: red; -} -.hide{ - display: none; -} -.show{ - display: block; -} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiList/wifiList.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiList/wifiList.js deleted file mode 100644 index ada3999..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiList/wifiList.js +++ /dev/null @@ -1,142 +0,0 @@ -// pages/wifiList/wifiList.js -Page({ - - /** - * 页面的初始数据 - */ - data: { - password: '', - hiddenmodal: true,//可以通过hidden是否掩藏弹出框的属性,来指定那个弹出框 - wifiList: [], - ssid: '', - }, - //点击按钮弹出指定的hiddenmodalput弹出框 - bindViewWifi: function (e) { - var list = this.data.wifiList; - var index = e.currentTarget.dataset.index; - //console.log(list[index]) - this.setData({ - ssid: list[index].essid, - hiddenmodal: !this.data.hiddenmodal, - - }) - - }, - bindViewInput: function (e) { - this.setData({ password: e.detail.value }) - }, - getWifiList: function() { - var self = this; - wx.request({ - url: 'https://www.alavening.com/wifi/wifiscan.cgi', - data: { - }, - header: { - 'content-type': 'application/json' // 默认值 - }, - success: function (res) { - console.log(res); - var data = res.data.result; - console.log(data); - if (data.inProgress == "0") { - wx.hideLoading(); - self.setData({ - wifiList: data.APs - }) - } else { - self.getWifiList(); - } - }, - fail: function(res) { - console.log(res); - wx.hideLoading(); - } - }) - }, - //取消按钮 - cancel: function () { - this.setData({ - hiddenmodal: true - }); - }, - //确认 - confirm: function () { - var self = this; - // this.setData({ - // hiddenmodal: true - // }) - if (!self.data.password.length < 8) { - // 这里修改成跳转的页面 - wx.redirectTo({ - url: '/pages/wifiConnect/wifiConnect?password=' + self.data.password + "&ssid=" + self.data.ssid - }) - - } - }, - /** - * 生命周期函数--监听页面加载 - */ - onLoad: function (options) { - var self = this; - wx.setNavigationBarTitle({ - title: 'WiFi配网' - }); - setTimeout(function() { - wx.showLoading({ - title: 'WiFi加载中...', - }); - self.getWifiList(); - console.log("WiFi加载中..."); - }, 500); - - }, - - /** - * 生命周期函数--监听页面初次渲染完成 - */ - onReady: function () { - - }, - - /** - * 生命周期函数--监听页面显示 - */ - onShow: function () { - - }, - - /** - * 生命周期函数--监听页面隐藏 - */ - onHide: function () { - - }, - - /** - * 生命周期函数--监听页面卸载 - */ - onUnload: function () { - - }, - - /** - * 页面相关事件处理函数--监听用户下拉动作 - */ - onPullDownRefresh: function () { - - }, - - /** - * 页面上拉触底事件的处理函数 - */ - onReachBottom: function () { - - }, - - /** - * 用户点击右上角分享 - */ - onShareAppMessage: function () { - - } -}) \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiList/wifiList.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiList/wifiList.json deleted file mode 100644 index 9e26dfe..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiList/wifiList.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiList/wifiList.wxml b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiList/wifiList.wxml deleted file mode 100644 index 1ac912c..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiList/wifiList.wxml +++ /dev/null @@ -1,35 +0,0 @@ - - - - 第三步 - 请选择您的WiFi - - - - - - {{item.essid}} - - - - - - - - - - - - - - - - - diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiList/wifiList.wxss b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiList/wifiList.wxss deleted file mode 100644 index e993838..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/pages/wifiList/wifiList.wxss +++ /dev/null @@ -1,35 +0,0 @@ -.container{ - position: relative; - padding-top: 50rpx; -} -.weui-cell__bd{ - padding: 0 20px; -} -.wifi-list{ - margin-top: 30px; -} -.main-content{ - flex: 1; - height: 100%; - overflow-y: hidden; -} -.item-wifi{ - border-bottom:2rpx solid #eee; - height:100rpx; - line-height:100rpx; - margin-top:0; - padding: 0 20px; - -} - -.item-icon { - flex: 0 0 80rpx; - height: 100%; - display: flex; - justify-items: center; - align-items: center; -} -.wifi-img { - width: 60rpx; - height: 60rpx; -} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/project.config.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/project.config.json deleted file mode 100644 index 9d8582b..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/project.config.json +++ /dev/null @@ -1,54 +0,0 @@ -{ - "description": "项目配置文件", - "packOptions": { - "ignore": [], - "include": [] - }, - "setting": { - "urlCheck": true, - "es6": true, - "enhance": false, - "postcss": true, - "preloadBackgroundData": false, - "minified": true, - "newFeature": true, - "coverView": true, - "nodeModules": true, - "autoAudits": false, - "showShadowRootInWxmlPanel": true, - "scopeDataCheck": false, - "uglifyFileName": false, - "checkInvalidKey": true, - "checkSiteMap": true, - "uploadWithSourceMap": true, - "compileHotReLoad": false, - "useMultiFrameRuntime": false, - "useApiHook": false, - "useApiHostProcess": false, - "babelSetting": { - "ignore": [], - "disablePlugins": [], - "outputPath": "" - }, - "useIsolateContext": false, - "userConfirmedBundleSwitch": false, - "packNpmManually": false, - "packNpmRelationList": [], - "minifyWXSS": true, - "disableUseStrict": false, - "minifyWXML": true, - "showES6CompileOption": false, - "useCompilerPlugins": false - }, - "compileType": "miniprogram", - "libVersion": "2.3.0", - "appid": "wx3ff81ef68814b084", - "projectname": "ESPConfig", - "simulatorType": "wechat", - "simulatorPluginLibVersion": {}, - "condition": {}, - "editorSetting": { - "tabIndent": "insertSpaces", - "tabSize": 2 - } -} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/project.private.config.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/project.private.config.json deleted file mode 100644 index 9a01ecf..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/project.private.config.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "description": "项目私有配置文件。此文件中的内容将覆盖 project.config.json 中的相同字段。项目的改动优先同步到此文件中。详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html", - "projectname": "ESPConfig", - "setting": { - "compileHotReLoad": true - } -} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/sitemap.json b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/sitemap.json deleted file mode 100644 index ca02add..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/sitemap.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "desc": "关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html", - "rules": [{ - "action": "allow", - "page": "*" - }] -} \ No newline at end of file diff --git a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/utils/util.js b/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/utils/util.js deleted file mode 100644 index 7bc1e68..0000000 --- a/code/09.extra/wifi_config/blufi/ESP-Config-WeChat/utils/util.js +++ /dev/null @@ -1,440 +0,0 @@ -import CryptoJS from'crypto-js'; - -const app = getApp(); -const FRAME_CTRL_POSITION_ENCRYPTED = 0; -const FRAME_CTRL_POSITION_CHECKSUM = 1; -const FRAME_CTRL_POSITION_DATA_DIRECTION = 2; -const FRAME_CTRL_POSITION_REQUIRE_ACK = 3; -const FRAME_CTRL_POSITION_FRAG = 4; -const DIRECTION_OUTPUT = 0; -const DIRECTION_INPUT = 1; -const AES_BASE_IV = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; -const NEG_SET_SEC_TOTAL_LEN = 0x00; -const NEG_SET_SEC_ALL_DATA = 0x01; -const PACKAGE_VALUE = 0x01; -const SUBTYPE_NEG = 0x00; -const SUBTYPE_WIFI_MODEl = 0x02; -const SUBTYPE_END = 0x03; -const PACKAGE_CONTROL_VALUE = 0x00; -const SUBTYPE_WIFI_NEG = 0x09; -const SUBTYPE_VERSION_NEG = 0x07; -const SUBTYPE_SET_SSID = 0x2; -const SUBTYPE_SET_PWD = 0x3; -const SUBTYPE_WIFI_LIST_NEG = 11; -const SUBTYPE_NEGOTIATION_NEG = 0; -const SUBTYPE_CUSTOM_DATA = 0x13; -const SUBTYPE_SET_SEC_MODE = 0x01; -var DH_P = "cf5cf5c38419a724957ff5dd323b9c45c3cdd261eb740f69aa94b8bb1a5c96409153bd76b24222d03274e4725a5406092e9e82e9135c643cae98132b0d95f7d65347c68afc1e677da90e51bbab5f5cf429c291b4ba39c6b2dc5e8c7231e46aa7728e87664532cdf547be20c9a3fa8342be6e34371a27c06f7dc0edddd2f86373"; -var DH_G = "02"; - -const descSucList = ["Bluetooth connecting...", "Bluetooth connection successful", "Device information is successfully obtained", "Attribute information is successfully obtained", "Send configuration information...", "Configuration information sent successfully", "Connection successfully"]; -const descFailList = ["Bluetooth connection failed", "Device information acquisition failed", "Attribute information acquisition failed", "Configuration information sent failed", "Distribution network failed"]; -const successList = { "0": "NULL", "1": "STA", "2": "SoftAP", "3": "SoftAP & STA" }; -const failList = { "0": "sequence error", "1": "checksum error", "2": "decrypt error", "3": "encrypt error", "4": "init security error", "5": "dh malloc error", "6": "dh param error", "7": "read param error", "8": "make public error" }; -var CRC_TB = [ - 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, - 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, - 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, - 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, - 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, - 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, - 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, - 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, - 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, - 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, - 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, - 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, - 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, - 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, - 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, - 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 -]; - -//时间格式 -const formatTime = date => { - const year = date.getFullYear() - const month = date.getMonth() + 1 - const day = date.getDate() - const hour = date.getHours() - const minute = date.getMinutes() - const second = date.getSeconds() - - return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') -} -const formatNumber = n => { - n = n.toString() - return n[1] ? n : '0' + n -} -//转16进制 -const ab2hex = buffer => { - var hexArr = Array.prototype.map.call( - new Uint8Array(buffer), - function (bit) { - return ('00' + bit.toString(16)).slice(-2) - } - ) - return hexArr; -} -//16进制转字符串 -const hexCharCodeToStr = hexCharCodeStr => { - var trimedStr = hexCharCodeStr.trim(); - var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr; - var len = rawStr.length; - if (len % 2 !== 0) { - alert("Illegal Format ASCII Code!"); - return ""; - } - var curCharCode; - var resultStr = []; - for (var i = 0; i < len; i = i + 2) { - curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value - resultStr.push(String.fromCharCode(curCharCode)); - } - return resultStr.join(""); -} -//过滤名称 -const filterDevice = (devices, name) => { - var list = []; - for (var i = 0; i < devices.length; i++) { - var device = devices[i]; - var re = new RegExp("^(BLUFI)"); - if (re.test(device[name])) { - list.push(device); - } - } - return list; -} -//获去type -const getType = (pkgType, subType) => { - return (subType << 2) | pkgType; -} -//unit8Arry转数组 -const uint8ArrayToArray = uint8Array => { - var array = []; - - for (var i = 0; i < uint8Array.byteLength; i++) { - array[i] = uint8Array[i]; - } - - return array; -} -//16进制转二进制数组 -const hexToBinArray = str => { - var dec = parseInt(str, 16), - bin = dec.toString(2); - while (bin.length < 8) { - bin = "0" + bin - } - return bin.split(""); -} -//16进制转数组 -const hexByArray = str => { - var arr = []; - if (str.length % 2 != 0) { - str = "0" + str; - } - for (var i = 0; i < str.length; i += 2) { - arr.push(str.substring(i, i + 2)) - } - return arr; -} -//16进制转整形数组 -const hexByInt = str => { - var arr = []; - if (str.length % 2 != 0) { - str = "0" + str; - } - for (var i = 0; i < str.length; i += 2) { - arr.push(parseInt(str.substring(i, i + 2), 16)) - } - return arr; -} -//排序 -const sortBy = (attr, rev) => { - //第二个参数没有传递 默认升序排列 - if (rev == undefined) { - rev = 1; - } else { - rev = (rev) ? 1 : -1; - } - return function (a, b) { - a = a[attr]; - b = b[attr]; - if (a < b) { - return rev * -1; - } else if (a > b) { - return rev * 1; - } - return 0; - } -} -//判断非空 -const _isEmpty = str => { - if (str === "" || str === null || str === undefined || str === "null" || str === "undefined") { - return true; - } else { - return false; - } -} -//设置配网失败背景色 -const setFailBg = () => { - wx.setNavigationBarColor({ - frontColor: "#ffffff", - backgroundColor: '#737d89', - }) -} -//设置配网成功背景色 -const setSucBg = () => { - wx.setNavigationBarColor({ - frontColor: "#ffffff", - backgroundColor: '#4d9efb', - }) -} -//组装数据格式 -const writeData = (type, subType, frameCtl, seq, len, data) => { - var value = [], - type = getType(type, subType); - value.push(type); - value.push(frameCtl); - value.push(seq); - value.push(len); - if (!_isEmpty(data)) { - value = value.concat(data); - } - return value; -} -//是否分包 -const isSubcontractor = (data, checksum, sequence, encrypt) => { - var len = 0, lenData = [], laveData = [], flag = false; - var total = data.length; - console.info('app.data.mtu', app.data.mtu) - console.info('total', total) - const splitLen = app.data.mtu - 3 - if (total > splitLen) { - if (checksum) { - lenData = data.slice(0, splitLen - 4); - laveData = data.slice(splitLen - 4); - } else { - lenData = data.slice(0, splitLen - 2); - laveData = data.slice(splitLen - 2); - } - var len1 = (total >> 8) & 0xff; - var len2 = total & 0xff; - lenData.splice(0, 0, len1); - lenData.splice(0, 0, len2); - len = lenData.length; - flag = true; - } else { - lenData = data; - len = lenData.length; - } - if (checksum) { - lenData = assemblyChecksum(lenData, len, sequence); - } - return {"len": len, "lenData": lenData, "laveData": laveData, "flag": flag} -} -const assemblyChecksum = (list, len, sequence, encrypt) => { - var checkData = []; - checkData.push(sequence); - checkData.push(len); - checkData = checkData.concat(list); - var crc = caluCRC(0, checkData); - var checksumByte1 = crc & 0xff; - var checksumByte2 = (crc >> 8) & 0xff; - list.push(checksumByte1); - list.push(checksumByte2); - return list; -} -//加密发送的数据 -const encrypt = (sequence, data, checksum) => { - var iv = generateAESIV(sequence), sumArr = [], list = []; - if (checksum) { - var len = data.length - 2; - list = data.slice(0, len); - sumArr = data.slice(len); - } else { - list = data; - } - console.log('加密') - var encryptData = blueAesEncrypt(iv, ab2hex(list).join('')); - return encryptData.concat(sumArr); -} -//判断返回的数据是否加密 -const isEncrypt = (self, frameCtrl, list) => { - var checksum = [], checkData = []; - if (frameCtrl[7] == "1") {//返回数据加密 - var iv = generateAESIV(parseInt(list[2], 16)); - console.log('解密') - console.log(iv) - list = list.slice(4) // 解密数据不含 Header - let byteArray = list.map(item => parseInt(item, 16)) - let decryptPkg = blueAesDecrypt(iv, ab2hex(byteArray).join('')) - console.log("DecryptPkg = ", decryptPkg) - list = ab2hex(decryptPkg); - if (frameCtrl[6] == "1") {//数据有校验 - console.log('校验') - var len = list.length - 2; - // checkData = list.slice(2, len); - // checksum = list.slice(len); - // console.log(checksum); - // var crc = caluCRC(0, checkData); - // var checksumByte1 = crc & 0xff; - // var checksumByte2 = (crc >> 8) & 0xff; - list = list.slice(0, len); - } - - if (frameCtrl[3] == "0") {//未分包 - self.setData({ - flagEnd: true - }) - } else {//分包 - list = list.slice(2); - } - - } else {//返回数据未加密 - if (frameCtrl[6] == "1") { - var len = list.length - 2; - // checkData = list.slice(2, len); - // checksum = list.slice(len); - // var crc = caluCRC(0, checkData); - // var checksumByte1 = crc & 0xff; - // var checksumByte2 = (crc >> 8) & 0xff; - list = list.slice(0, len); - } - if (frameCtrl[3] == "0") {//未分包 - list = list.slice(4); - self.setData({ - flagEnd: true - }) - } else {//分包 - list = list.slice(6); - } - } - return list; -} -//DH加密 -const blueDH = (p, g, crypto) => { - var client = crypto.createDiffieHellman(p, "hex", g, "hex"); - var clientKey = client.generateKeys(); - //var clientSecret = client.computeSecret(server.getPublicKey()); - return client; -} -//md5加密 -const blueMd5 = (md5, key) => { - var arr = md5.array(key); - return arr; -} -// aes加密 -const blueAesEncrypt = (iv, bytes) => { - bytes = CryptoJS.enc.Hex.parse(bytes); - let mdKey = ab2hex(app.data.md5Key).join(''); - iv = ab2hex(iv).join(''); - const encryptedStr = CryptoJS.AES.encrypt(bytes, CryptoJS.enc.Hex.parse(mdKey), { - iv: CryptoJS.enc.Hex.parse(iv), - mode: CryptoJS.mode.CFB, - padding: CryptoJS.pad.NoPadding -}); -return hexByInt(encryptedStr.ciphertext.toString()) -} -//aes解密 -const blueAesDecrypt = (iv, bytes) => { - let mdKey = ab2hex(app.data.md5Key).join(''); - iv = ab2hex(iv).join(''); - bytes = CryptoJS.enc.Hex.parse(bytes); - bytes = CryptoJS.enc.Base64.stringify(bytes); - const decryptStr = CryptoJS.AES.decrypt(bytes, CryptoJS.enc.Hex.parse(mdKey), { - iv: CryptoJS.enc.Hex.parse(iv), - mode: CryptoJS.mode.CFB, - padding: CryptoJS.pad.NoPadding - }); - return hexByInt(decryptStr.toString()) -} -//获取Frame Control -const getFrameCTRLValue = (encrypted, checksum, direction, requireAck, frag) => { - var frame = 0; - if (encrypted) { - frame = frame | (1 << FRAME_CTRL_POSITION_ENCRYPTED); - } - if (checksum) { - frame = frame | (1 << FRAME_CTRL_POSITION_CHECKSUM); - } - if (direction == DIRECTION_INPUT) { - frame = frame | (1 << FRAME_CTRL_POSITION_DATA_DIRECTION); - } - if (requireAck) { - frame = frame | (1 << FRAME_CTRL_POSITION_REQUIRE_ACK); - } - if (frag) { - frame = frame | (1 << FRAME_CTRL_POSITION_FRAG); - } - return frame; -} -//获取aes iv -const generateAESIV = sequence => { - var result = []; - for (var i = 0; i < 16; i++) { - if (i == 0) { - result[0] = sequence; - } else { - result[i] = AES_BASE_IV[i]; - } - } - return result; -} -//计算CRC值 -const caluCRC = (crc, pByte) => { - crc = (~crc) & 0xffff; - for (var i in pByte) { - crc = CRC_TB[((crc & 0xffff) >> 8) ^ (pByte[i] & 0xff)] ^ ((crc & 0xffff) << 8); - } - return (~crc) & 0xffff; -} -module.exports = { - formatTime: formatTime, - ab2hex: ab2hex, - hexCharCodeToStr: hexCharCodeToStr, - filterDevice: filterDevice, - getType: getType, - hexToBinArray: hexToBinArray, - hexByArray: hexByArray, - hexByInt: hexByInt, - sortBy: sortBy, - _isEmpty: _isEmpty, - setFailBg: setFailBg, - setSucBg: setSucBg, - writeData: writeData, - isSubcontractor: isSubcontractor, - getFrameCTRLValue: getFrameCTRLValue, - blueDH: blueDH, - blueMd5: blueMd5, - blueAesEncrypt: blueAesEncrypt, - blueAesDecrypt: blueAesDecrypt, - uint8ArrayToArray: uint8ArrayToArray, - generateAESIV: generateAESIV, - isEncrypt: isEncrypt, - caluCRC: caluCRC, - encrypt: encrypt, - DH_P: DH_P, - DH_G: DH_G, - DIRECTION_OUTPUT: DIRECTION_OUTPUT, - DIRECTION_INPUT: DIRECTION_INPUT, - NEG_SET_SEC_TOTAL_LEN: NEG_SET_SEC_TOTAL_LEN, - NEG_SET_SEC_ALL_DATA: NEG_SET_SEC_ALL_DATA, - PACKAGE_VALUE: PACKAGE_VALUE, - SUBTYPE_NEG: SUBTYPE_NEG, - PACKAGE_CONTROL_VALUE: PACKAGE_CONTROL_VALUE, - SUBTYPE_WIFI_NEG: SUBTYPE_WIFI_NEG, - SUBTYPE_WIFI_LIST_NEG: SUBTYPE_WIFI_LIST_NEG, - SUBTYPE_VERSION_NEG: SUBTYPE_VERSION_NEG, - SUBTYPE_NEGOTIATION_NEG: SUBTYPE_NEGOTIATION_NEG, - SUBTYPE_WIFI_MODEl: SUBTYPE_WIFI_MODEl, - SUBTYPE_SET_SSID: SUBTYPE_SET_SSID, - SUBTYPE_SET_PWD: SUBTYPE_SET_PWD, - SUBTYPE_END: SUBTYPE_END, - SUBTYPE_CUSTOM_DATA: SUBTYPE_CUSTOM_DATA, - SUBTYPE_SET_SEC_MODE: SUBTYPE_SET_SEC_MODE, - descSucList: descSucList, - descFailList: descFailList, - successList: successList, - failList: failList, -} diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/CMakeLists.txt b/code/09.extra/wifi_config/blufi/blufi-esp32/CMakeLists.txt deleted file mode 100644 index aa43af0..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -# The following lines of boilerplate have to be in your project's CMakeLists -# in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.16) - -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(blufi_demo) diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/README.md b/code/09.extra/wifi_config/blufi/blufi-esp32/README.md deleted file mode 100644 index 37d16e2..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/README.md +++ /dev/null @@ -1,84 +0,0 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | - -# ESP-IDF Blufi Example - -This demo shows how to use the *Blufi* to configurate the Wi-Fi connection to AP. - -The BluFi for ESP32 is a Wi-Fi network configuration function via Bluetooth channel. It provides a secure protocol to pass Wi-Fi configuration and credentials to the ESP32. - -## How to Use Example - -Before project configuration and build, be sure to set the correct chip target using: - -```bash -idf.py set-target -``` - -To test this demo, you need to prepare a mobile phone with blufi application installed. You can download the blufi application from [Android version](https://github.com/EspressifApp/EspBlufi) and [iOS version](https://itunes.apple.com/cn/app/espblufi/id1450614082?mt=8). - -Blufi is completely open source, here is the download link: - -* [Blufi source code](https://github.com/espressif/esp-idf/tree/master/examples/bluetooth/blufi) -* [BluFi protocol](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/ble/blufi.html) -* [iOS source code](https://github.com/EspressifApp/EspBlufiForiOS) -* [Android source code](https://github.com/EspressifApp/EspBlufi) -* [Bluetooth Network User Guide CN](https://www.espressif.com/sites/default/files/documentation/esp32_bluetooth_networking_user_guide_cn.pdf) - -### Hardware Required - -* A development board with ESP32/ESP32-C3/ESP32-S3/ESP32-C2 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.) -* A USB cable for Power supply and programming - -See [Development Boards](https://www.espressif.com/en/products/devkits) for more information about it. - -### Build and Flash - -Run `idf.py -p PORT flash monitor` to build, flash and monitor the project. - -(To exit the serial monitor, type ``Ctrl-]``.) - -See the [Getting Started Guide](https://idf.espressif.com/) for full steps to configure and use ESP-IDF to build projects. - -## Example Output - -``` -I (0) cpu_start: Starting scheduler on APP CPU. -I (708) wifi:wifi driver task: 3ffbb118, prio:23, stack:6656, core=0 -I (708) system_api: Base MAC address is not set -I (708) system_api: read default base MAC address from EFUSE -I (718) wifi:wifi firmware version: 44799ff -I (718) wifi:wifi certification version: v7.0 -I (718) wifi:config NVS flash: enabled -I (718) wifi:config nano formatting: disabled -I (728) wifi:Init data frame dynamic rx buffer num: 32 -I (728) wifi:Init management frame dynamic rx buffer num: 32 -I (738) wifi:Init management short buffer num: 32 -I (738) wifi:Init dynamic tx buffer num: 32 -I (748) wifi:Init static rx buffer size: 1600 -I (748) wifi:Init static rx buffer num: 10 -I (748) wifi:Init dynamic rx buffer num: 32 -I (758) wifi_init: rx ba win: 6 -I (758) wifi_init: tcpip mbox: 32 -I (768) wifi_init: udp mbox: 6 -I (768) wifi_init: tcp mbox: 6 -I (768) wifi_init: tcp tx win: 5744 -I (778) wifi_init: tcp rx win: 5744 -I (778) wifi_init: tcp mss: 1440 -I (788) wifi_init: WiFi IRAM OP enabled -I (788) wifi_init: WiFi RX IRAM OP enabled -I (798) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07 -I (908) wifi:mode : sta (fc:f5:c4:3c:62:90) -I (908) wifi:enable tsf -I (908) BTDM_INIT: BT controller compile version [1342a48] -I (1198) BLUFI_EXAMPLE: BD ADDR: fc:f5:c4:3c:62:92 - -I (1198) BLUFI_EXAMPLE: BLUFI VERSION 0102 - -I (1198) BLUFI_EXAMPLE: BLUFI init finish - -``` - -## Troubleshooting - -For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon. diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/main/CMakeLists.txt b/code/09.extra/wifi_config/blufi/blufi-esp32/main/CMakeLists.txt deleted file mode 100644 index 68e37f4..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/main/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -idf_component_register(SRCS "blufi_example_main.c" - "blufi_security.c" - "blufi_init.c" - INCLUDE_DIRS ".") diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/main/Kconfig.projbuild b/code/09.extra/wifi_config/blufi/blufi-esp32/main/Kconfig.projbuild deleted file mode 100644 index 8346f7a..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/main/Kconfig.projbuild +++ /dev/null @@ -1,10 +0,0 @@ -menu "Example Configuration" - - config EXAMPLE_WIFI_CONNECTION_MAXIMUM_RETRY - int "WiFi connection maximum retry" - range 0 255 - default 2 - help - WiFi connection maximum retry, from 0 to 255. - -endmenu diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/main/blufi_example.h b/code/09.extra/wifi_config/blufi/blufi-esp32/main/blufi_example.h deleted file mode 100644 index c5e9e84..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/main/blufi_example.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - */ - - -#pragma once - -#define BLUFI_EXAMPLE_TAG "BLUFI_EXAMPLE" -#define BLUFI_INFO(fmt, ...) ESP_LOGI(BLUFI_EXAMPLE_TAG, fmt, ##__VA_ARGS__) -#define BLUFI_ERROR(fmt, ...) ESP_LOGE(BLUFI_EXAMPLE_TAG, fmt, ##__VA_ARGS__) - -void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free); -int blufi_aes_encrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len); -int blufi_aes_decrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len); -uint16_t blufi_crc_checksum(uint8_t iv8, uint8_t *data, int len); - -int blufi_security_init(void); -void blufi_security_deinit(void); -int esp_blufi_gap_register_callback(void); -esp_err_t esp_blufi_host_init(void); -esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *callbacks); -esp_err_t esp_blufi_host_deinit(void); -esp_err_t esp_blufi_controller_init(void); -esp_err_t esp_blufi_controller_deinit(void); diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/main/blufi_example_main.c b/code/09.extra/wifi_config/blufi/blufi-esp32/main/blufi_example_main.c deleted file mode 100644 index 69a3380..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/main/blufi_example_main.c +++ /dev/null @@ -1,484 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - */ - - -/**************************************************************************** -* This is a demo for bluetooth config wifi connection to ap. You can config ESP32 to connect a softap -* or config ESP32 as a softap to be connected by other device. APP can be downloaded from github -* android source code: https://github.com/EspressifApp/EspBlufi -* iOS source code: https://github.com/EspressifApp/EspBlufiForiOS -****************************************************************************/ - -#include -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_mac.h" -#include "esp_wifi.h" -#include "esp_event.h" -#include "esp_log.h" -#include "nvs_flash.h" -#if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED -#include "esp_bt.h" -#endif - -#include "esp_blufi_api.h" -#include "blufi_example.h" - -#include "esp_blufi.h" - -#define EXAMPLE_WIFI_CONNECTION_MAXIMUM_RETRY CONFIG_EXAMPLE_WIFI_CONNECTION_MAXIMUM_RETRY -#define EXAMPLE_INVALID_REASON 255 -#define EXAMPLE_INVALID_RSSI -128 - -static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param); - -#define WIFI_LIST_NUM 10 - -static wifi_config_t sta_config; -static wifi_config_t ap_config; - -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -/* The event group allows multiple bits for each event, - but we only care about one event - are we connected - to the AP with an IP? */ -const int CONNECTED_BIT = BIT0; - -static uint8_t example_wifi_retry = 0; - -/* store the station info for send back to phone */ -static bool gl_sta_connected = false; -static bool gl_sta_got_ip = false; -static bool ble_is_connected = false; -static uint8_t gl_sta_bssid[6]; -static uint8_t gl_sta_ssid[32]; -static int gl_sta_ssid_len; -static wifi_sta_list_t gl_sta_list; -static bool gl_sta_is_connecting = false; -static esp_blufi_extra_info_t gl_sta_conn_info; - -static void example_record_wifi_conn_info(int rssi, uint8_t reason) -{ - memset(&gl_sta_conn_info, 0, sizeof(esp_blufi_extra_info_t)); - if (gl_sta_is_connecting) { - gl_sta_conn_info.sta_max_conn_retry_set = true; - gl_sta_conn_info.sta_max_conn_retry = EXAMPLE_WIFI_CONNECTION_MAXIMUM_RETRY; - } else { - gl_sta_conn_info.sta_conn_rssi_set = true; - gl_sta_conn_info.sta_conn_rssi = rssi; - gl_sta_conn_info.sta_conn_end_reason_set = true; - gl_sta_conn_info.sta_conn_end_reason = reason; - } -} - -static void example_wifi_connect(void) -{ - example_wifi_retry = 0; - gl_sta_is_connecting = (esp_wifi_connect() == ESP_OK); - example_record_wifi_conn_info(EXAMPLE_INVALID_RSSI, EXAMPLE_INVALID_REASON); -} - -static bool example_wifi_reconnect(void) -{ - bool ret; - if (gl_sta_is_connecting && example_wifi_retry++ < EXAMPLE_WIFI_CONNECTION_MAXIMUM_RETRY) { - BLUFI_INFO("BLUFI WiFi starts reconnection\n"); - gl_sta_is_connecting = (esp_wifi_connect() == ESP_OK); - example_record_wifi_conn_info(EXAMPLE_INVALID_RSSI, EXAMPLE_INVALID_REASON); - ret = true; - } else { - ret = false; - } - return ret; -} - -static int softap_get_current_connection_number(void) -{ - esp_err_t ret; - ret = esp_wifi_ap_get_sta_list(&gl_sta_list); - if (ret == ESP_OK) - { - return gl_sta_list.num; - } - - return 0; -} - -static void ip_event_handler(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) -{ - wifi_mode_t mode; - - switch (event_id) { - case IP_EVENT_STA_GOT_IP: { - esp_blufi_extra_info_t info; - - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - esp_wifi_get_mode(&mode); - - memset(&info, 0, sizeof(esp_blufi_extra_info_t)); - memcpy(info.sta_bssid, gl_sta_bssid, 6); - info.sta_bssid_set = true; - info.sta_ssid = gl_sta_ssid; - info.sta_ssid_len = gl_sta_ssid_len; - gl_sta_got_ip = true; - if (ble_is_connected == true) { - esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, softap_get_current_connection_number(), &info); - } else { - BLUFI_INFO("BLUFI BLE is not connected yet\n"); - } - break; - } - default: - break; - } - return; -} - -static void wifi_event_handler(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) -{ - wifi_event_sta_connected_t *event; - wifi_event_sta_disconnected_t *disconnected_event; - wifi_mode_t mode; - - switch (event_id) { - case WIFI_EVENT_STA_START: - example_wifi_connect(); - break; - case WIFI_EVENT_STA_CONNECTED: - gl_sta_connected = true; - gl_sta_is_connecting = false; - event = (wifi_event_sta_connected_t*) event_data; - memcpy(gl_sta_bssid, event->bssid, 6); - memcpy(gl_sta_ssid, event->ssid, event->ssid_len); - gl_sta_ssid_len = event->ssid_len; - break; - case WIFI_EVENT_STA_DISCONNECTED: - /* Only handle reconnection during connecting */ - if (gl_sta_connected == false && example_wifi_reconnect() == false) { - gl_sta_is_connecting = false; - disconnected_event = (wifi_event_sta_disconnected_t*) event_data; - example_record_wifi_conn_info(disconnected_event->rssi, disconnected_event->reason); - } - /* This is a workaround as ESP32 WiFi libs don't currently - auto-reassociate. */ - gl_sta_connected = false; - gl_sta_got_ip = false; - memset(gl_sta_ssid, 0, 32); - memset(gl_sta_bssid, 0, 6); - gl_sta_ssid_len = 0; - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - case WIFI_EVENT_AP_START: - esp_wifi_get_mode(&mode); - - /* TODO: get config or information of softap, then set to report extra_info */ - if (ble_is_connected == true) { - if (gl_sta_connected) { - esp_blufi_extra_info_t info; - memset(&info, 0, sizeof(esp_blufi_extra_info_t)); - memcpy(info.sta_bssid, gl_sta_bssid, 6); - info.sta_bssid_set = true; - info.sta_ssid = gl_sta_ssid; - info.sta_ssid_len = gl_sta_ssid_len; - esp_blufi_send_wifi_conn_report(mode, gl_sta_got_ip ? ESP_BLUFI_STA_CONN_SUCCESS : ESP_BLUFI_STA_NO_IP, softap_get_current_connection_number(), &info); - } else if (gl_sta_is_connecting) { - esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONNECTING, softap_get_current_connection_number(), &gl_sta_conn_info); - } else { - esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, softap_get_current_connection_number(), &gl_sta_conn_info); - } - } else { - BLUFI_INFO("BLUFI BLE is not connected yet\n"); - } - break; - case WIFI_EVENT_SCAN_DONE: { - uint16_t apCount = 0; - esp_wifi_scan_get_ap_num(&apCount); - if (apCount == 0) { - BLUFI_INFO("Nothing AP found"); - break; - } - wifi_ap_record_t *ap_list = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * apCount); - if (!ap_list) { - BLUFI_ERROR("malloc error, ap_list is NULL"); - esp_wifi_clear_ap_list(); - break; - } - ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&apCount, ap_list)); - esp_blufi_ap_record_t * blufi_ap_list = (esp_blufi_ap_record_t *)malloc(apCount * sizeof(esp_blufi_ap_record_t)); - if (!blufi_ap_list) { - if (ap_list) { - free(ap_list); - } - BLUFI_ERROR("malloc error, blufi_ap_list is NULL"); - break; - } - for (int i = 0; i < apCount; ++i) - { - blufi_ap_list[i].rssi = ap_list[i].rssi; - memcpy(blufi_ap_list[i].ssid, ap_list[i].ssid, sizeof(ap_list[i].ssid)); - } - - if (ble_is_connected == true) { - esp_blufi_send_wifi_list(apCount, blufi_ap_list); - } else { - BLUFI_INFO("BLUFI BLE is not connected yet\n"); - } - - esp_wifi_scan_stop(); - free(ap_list); - free(blufi_ap_list); - break; - } - case WIFI_EVENT_AP_STACONNECTED: { - wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data; - BLUFI_INFO("station "MACSTR" join, AID=%d", MAC2STR(event->mac), event->aid); - break; - } - case WIFI_EVENT_AP_STADISCONNECTED: { - wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data; - BLUFI_INFO("station "MACSTR" leave, AID=%d, reason=%d", MAC2STR(event->mac), event->aid, event->reason); - break; - } - - default: - break; - } - return; -} - -static void initialise_wifi(void) -{ - ESP_ERROR_CHECK(esp_netif_init()); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_event_loop_create_default()); - esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); - assert(sta_netif); - esp_netif_t *ap_netif = esp_netif_create_default_wifi_ap(); - assert(ap_netif); - ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); - ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL)); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - example_record_wifi_conn_info(EXAMPLE_INVALID_RSSI, EXAMPLE_INVALID_REASON); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - -static esp_blufi_callbacks_t example_callbacks = { - .event_cb = example_event_callback, - .negotiate_data_handler = blufi_dh_negotiate_data_handler, - .encrypt_func = blufi_aes_encrypt, - .decrypt_func = blufi_aes_decrypt, - .checksum_func = blufi_crc_checksum, -}; - -static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param) -{ - /* actually, should post to blufi_task handle the procedure, - * now, as a example, we do it more simply */ - switch (event) { - case ESP_BLUFI_EVENT_INIT_FINISH: - BLUFI_INFO("BLUFI init finish\n"); - - esp_blufi_adv_start(); - break; - case ESP_BLUFI_EVENT_DEINIT_FINISH: - BLUFI_INFO("BLUFI deinit finish\n"); - break; - case ESP_BLUFI_EVENT_BLE_CONNECT: - BLUFI_INFO("BLUFI ble connect\n"); - ble_is_connected = true; - esp_blufi_adv_stop(); - blufi_security_init(); - break; - case ESP_BLUFI_EVENT_BLE_DISCONNECT: - BLUFI_INFO("BLUFI ble disconnect\n"); - ble_is_connected = false; - blufi_security_deinit(); - esp_blufi_adv_start(); - break; - case ESP_BLUFI_EVENT_SET_WIFI_OPMODE: - BLUFI_INFO("BLUFI Set WIFI opmode %d\n", param->wifi_mode.op_mode); - ESP_ERROR_CHECK( esp_wifi_set_mode(param->wifi_mode.op_mode) ); - break; - case ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP: - BLUFI_INFO("BLUFI requset wifi connect to AP\n"); - /* there is no wifi callback when the device has already connected to this wifi - so disconnect wifi before connection. - */ - esp_wifi_disconnect(); - example_wifi_connect(); - break; - case ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP: - BLUFI_INFO("BLUFI requset wifi disconnect from AP\n"); - esp_wifi_disconnect(); - break; - case ESP_BLUFI_EVENT_REPORT_ERROR: - BLUFI_ERROR("BLUFI report error, error code %d\n", param->report_error.state); - esp_blufi_send_error_info(param->report_error.state); - break; - case ESP_BLUFI_EVENT_GET_WIFI_STATUS: { - wifi_mode_t mode; - esp_blufi_extra_info_t info; - - esp_wifi_get_mode(&mode); - - if (gl_sta_connected) { - memset(&info, 0, sizeof(esp_blufi_extra_info_t)); - memcpy(info.sta_bssid, gl_sta_bssid, 6); - info.sta_bssid_set = true; - info.sta_ssid = gl_sta_ssid; - info.sta_ssid_len = gl_sta_ssid_len; - esp_blufi_send_wifi_conn_report(mode, gl_sta_got_ip ? ESP_BLUFI_STA_CONN_SUCCESS : ESP_BLUFI_STA_NO_IP, softap_get_current_connection_number(), &info); - } else if (gl_sta_is_connecting) { - esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONNECTING, softap_get_current_connection_number(), &gl_sta_conn_info); - } else { - esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, softap_get_current_connection_number(), &gl_sta_conn_info); - } - BLUFI_INFO("BLUFI get wifi status from AP\n"); - - break; - } - case ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE: - BLUFI_INFO("blufi close a gatt connection"); - esp_blufi_disconnect(); - break; - case ESP_BLUFI_EVENT_DEAUTHENTICATE_STA: - /* TODO */ - break; - case ESP_BLUFI_EVENT_RECV_STA_BSSID: - memcpy(sta_config.sta.bssid, param->sta_bssid.bssid, 6); - sta_config.sta.bssid_set = 1; - esp_wifi_set_config(WIFI_IF_STA, &sta_config); - BLUFI_INFO("Recv STA BSSID %s\n", sta_config.sta.ssid); - break; - case ESP_BLUFI_EVENT_RECV_STA_SSID: - strncpy((char *)sta_config.sta.ssid, (char *)param->sta_ssid.ssid, param->sta_ssid.ssid_len); - sta_config.sta.ssid[param->sta_ssid.ssid_len] = '\0'; - esp_wifi_set_config(WIFI_IF_STA, &sta_config); - BLUFI_INFO("Recv STA SSID %s\n", sta_config.sta.ssid); - break; - case ESP_BLUFI_EVENT_RECV_STA_PASSWD: - strncpy((char *)sta_config.sta.password, (char *)param->sta_passwd.passwd, param->sta_passwd.passwd_len); - sta_config.sta.password[param->sta_passwd.passwd_len] = '\0'; - esp_wifi_set_config(WIFI_IF_STA, &sta_config); - BLUFI_INFO("Recv STA PASSWORD %s\n", sta_config.sta.password); - break; - case ESP_BLUFI_EVENT_RECV_SOFTAP_SSID: - strncpy((char *)ap_config.ap.ssid, (char *)param->softap_ssid.ssid, param->softap_ssid.ssid_len); - ap_config.ap.ssid[param->softap_ssid.ssid_len] = '\0'; - ap_config.ap.ssid_len = param->softap_ssid.ssid_len; - esp_wifi_set_config(WIFI_IF_AP, &ap_config); - BLUFI_INFO("Recv SOFTAP SSID %s, ssid len %d\n", ap_config.ap.ssid, ap_config.ap.ssid_len); - break; - case ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD: - strncpy((char *)ap_config.ap.password, (char *)param->softap_passwd.passwd, param->softap_passwd.passwd_len); - ap_config.ap.password[param->softap_passwd.passwd_len] = '\0'; - esp_wifi_set_config(WIFI_IF_AP, &ap_config); - BLUFI_INFO("Recv SOFTAP PASSWORD %s len = %d\n", ap_config.ap.password, param->softap_passwd.passwd_len); - break; - case ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM: - if (param->softap_max_conn_num.max_conn_num > 4) { - return; - } - ap_config.ap.max_connection = param->softap_max_conn_num.max_conn_num; - esp_wifi_set_config(WIFI_IF_AP, &ap_config); - BLUFI_INFO("Recv SOFTAP MAX CONN NUM %d\n", ap_config.ap.max_connection); - break; - case ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE: - if (param->softap_auth_mode.auth_mode >= WIFI_AUTH_MAX) { - return; - } - ap_config.ap.authmode = param->softap_auth_mode.auth_mode; - esp_wifi_set_config(WIFI_IF_AP, &ap_config); - BLUFI_INFO("Recv SOFTAP AUTH MODE %d\n", ap_config.ap.authmode); - break; - case ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL: - if (param->softap_channel.channel > 13) { - return; - } - ap_config.ap.channel = param->softap_channel.channel; - esp_wifi_set_config(WIFI_IF_AP, &ap_config); - BLUFI_INFO("Recv SOFTAP CHANNEL %d\n", ap_config.ap.channel); - break; - case ESP_BLUFI_EVENT_GET_WIFI_LIST:{ - wifi_scan_config_t scanConf = { - .ssid = NULL, - .bssid = NULL, - .channel = 0, - .show_hidden = false - }; - esp_err_t ret = esp_wifi_scan_start(&scanConf, true); - if (ret != ESP_OK) { - esp_blufi_send_error_info(ESP_BLUFI_WIFI_SCAN_FAIL); - } - break; - } - case ESP_BLUFI_EVENT_RECV_CUSTOM_DATA: - BLUFI_INFO("Recv Custom Data %" PRIu32 "\n", param->custom_data.data_len); - esp_log_buffer_hex("Custom Data", param->custom_data.data, param->custom_data.data_len); - break; - case ESP_BLUFI_EVENT_RECV_USERNAME: - /* Not handle currently */ - break; - case ESP_BLUFI_EVENT_RECV_CA_CERT: - /* Not handle currently */ - break; - case ESP_BLUFI_EVENT_RECV_CLIENT_CERT: - /* Not handle currently */ - break; - case ESP_BLUFI_EVENT_RECV_SERVER_CERT: - /* Not handle currently */ - break; - case ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY: - /* Not handle currently */ - break;; - case ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY: - /* Not handle currently */ - break; - default: - break; - } -} - -void app_main(void) -{ - esp_err_t ret; - - // Initialize NVS - ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK( ret ); - - initialise_wifi(); - -#if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED - ret = esp_blufi_controller_init(); - if (ret) { - BLUFI_ERROR("%s BLUFI controller init failed: %s\n", __func__, esp_err_to_name(ret)); - return; - } -#endif - - ret = esp_blufi_host_and_cb_init(&example_callbacks); - if (ret) { - BLUFI_ERROR("%s initialise failed: %s\n", __func__, esp_err_to_name(ret)); - return; - } - - BLUFI_INFO("BLUFI VERSION %04x\n", esp_blufi_get_version()); -} diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/main/blufi_init.c b/code/09.extra/wifi_config/blufi/blufi-esp32/main/blufi_init.c deleted file mode 100644 index efb81ea..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/main/blufi_init.c +++ /dev/null @@ -1,283 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - */ - -#include -#include "esp_err.h" -#include "esp_blufi_api.h" -#include "esp_log.h" -#include "esp_blufi.h" -#include "blufi_example.h" -#if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED -#include "esp_bt.h" -#endif -#ifdef CONFIG_BT_BLUEDROID_ENABLED -#include "esp_bt_main.h" -#include "esp_bt_device.h" -#endif - -#ifdef CONFIG_BT_NIMBLE_ENABLED -#include "nimble/nimble_port.h" -#include "nimble/nimble_port_freertos.h" -#include "host/ble_hs.h" -#include "host/util/util.h" -#include "services/gap/ble_svc_gap.h" -#include "services/gatt/ble_svc_gatt.h" -#include "console/console.h" -#endif - -#ifdef CONFIG_BT_BLUEDROID_ENABLED -esp_err_t esp_blufi_host_init(void) -{ - int ret; - ret = esp_bluedroid_init(); - if (ret) { - BLUFI_ERROR("%s init bluedroid failed: %s\n", __func__, esp_err_to_name(ret)); - return ESP_FAIL; - } - - ret = esp_bluedroid_enable(); - if (ret) { - BLUFI_ERROR("%s init bluedroid failed: %s\n", __func__, esp_err_to_name(ret)); - return ESP_FAIL; - } - BLUFI_INFO("BD ADDR: "ESP_BD_ADDR_STR"\n", ESP_BD_ADDR_HEX(esp_bt_dev_get_address())); - - return ESP_OK; - -} - -esp_err_t esp_blufi_host_deinit(void) -{ - int ret; - ret = esp_blufi_profile_deinit(); - if(ret != ESP_OK) { - return ret; - } - - ret = esp_bluedroid_disable(); - if (ret) { - BLUFI_ERROR("%s deinit bluedroid failed: %s\n", __func__, esp_err_to_name(ret)); - return ESP_FAIL; - } - - ret = esp_bluedroid_deinit(); - if (ret) { - BLUFI_ERROR("%s deinit bluedroid failed: %s\n", __func__, esp_err_to_name(ret)); - return ESP_FAIL; - } - - return ESP_OK; - -} - -esp_err_t esp_blufi_gap_register_callback(void) -{ - int rc; - rc = esp_ble_gap_register_callback(esp_blufi_gap_event_handler); - if(rc){ - return rc; - } - return esp_blufi_profile_init(); -} - -esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *example_callbacks) -{ - esp_err_t ret = ESP_OK; - - ret = esp_blufi_host_init(); - if (ret) { - BLUFI_ERROR("%s initialise host failed: %s\n", __func__, esp_err_to_name(ret)); - return ret; - } - - ret = esp_blufi_register_callbacks(example_callbacks); - if(ret){ - BLUFI_ERROR("%s blufi register failed, error code = %x\n", __func__, ret); - return ret; - } - - ret = esp_blufi_gap_register_callback(); - if(ret){ - BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret); - return ret; - } - - return ESP_OK; - -} - -#endif /* CONFIG_BT_BLUEDROID_ENABLED */ - -#if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED -esp_err_t esp_blufi_controller_init() { - esp_err_t ret = ESP_OK; -#if CONFIG_IDF_TARGET_ESP32 - ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)); -#endif - - esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); - ret = esp_bt_controller_init(&bt_cfg); - if (ret) { - BLUFI_ERROR("%s initialize bt controller failed: %s\n", __func__, esp_err_to_name(ret)); - return ret; - } - - ret = esp_bt_controller_enable(ESP_BT_MODE_BLE); - if (ret) { - BLUFI_ERROR("%s enable bt controller failed: %s\n", __func__, esp_err_to_name(ret)); - return ret; - } - return ret; -} -#endif - -#if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED -esp_err_t esp_blufi_controller_deinit() { - esp_err_t ret = ESP_OK; - ret = esp_bt_controller_disable(); - if (ret) { - BLUFI_ERROR("%s disable bt controller failed: %s\n", __func__, esp_err_to_name(ret)); - return ret; - } - - ret = esp_bt_controller_deinit(); - if (ret) { - BLUFI_ERROR("%s deinit bt controller failed: %s\n", __func__, esp_err_to_name(ret)); - return ret; - } - - return ret; -} -#endif - -#ifdef CONFIG_BT_NIMBLE_ENABLED -void ble_store_config_init(void); -static void blufi_on_reset(int reason) -{ - MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason); -} - -static void -blufi_on_sync(void) -{ - esp_blufi_profile_init(); -} - -void bleprph_host_task(void *param) -{ - ESP_LOGI("BLUFI_EXAMPLE", "BLE Host Task Started"); - /* This function will return only when nimble_port_stop() is executed */ - nimble_port_run(); - - nimble_port_freertos_deinit(); -} - -esp_err_t esp_blufi_host_init(void) -{ - esp_err_t err; - err = esp_nimble_init(); - if (err) { - BLUFI_ERROR("%s failed: %s\n", __func__, esp_err_to_name(err)); - return ESP_FAIL; - } - -/* Initialize the NimBLE host configuration. */ - ble_hs_cfg.reset_cb = blufi_on_reset; - ble_hs_cfg.sync_cb = blufi_on_sync; - ble_hs_cfg.gatts_register_cb = esp_blufi_gatt_svr_register_cb; - ble_hs_cfg.store_status_cb = ble_store_util_status_rr; - - ble_hs_cfg.sm_io_cap = 4; -#ifdef CONFIG_EXAMPLE_BONDING - ble_hs_cfg.sm_bonding = 1; -#endif -#ifdef CONFIG_EXAMPLE_MITM - ble_hs_cfg.sm_mitm = 1; -#endif -#ifdef CONFIG_EXAMPLE_USE_SC - ble_hs_cfg.sm_sc = 1; -#else - ble_hs_cfg.sm_sc = 0; -#ifdef CONFIG_EXAMPLE_BONDING - ble_hs_cfg.sm_our_key_dist = 1; - ble_hs_cfg.sm_their_key_dist = 1; -#endif -#endif - - int rc; - rc = esp_blufi_gatt_svr_init(); - assert(rc == 0); - - /* Set the default device name. */ - rc = ble_svc_gap_device_name_set(BLUFI_DEVICE_NAME); - assert(rc == 0); - - /* XXX Need to have template for store */ - ble_store_config_init(); - - esp_blufi_btc_init(); - - err = esp_nimble_enable(bleprph_host_task); - if (err) { - BLUFI_ERROR("%s failed: %s\n", __func__, esp_err_to_name(err)); - return ESP_FAIL; - } - - return ESP_OK; -} - -esp_err_t esp_blufi_host_deinit(void) -{ - esp_err_t ret = ESP_OK; - - ret = nimble_port_stop(); - - if (ret == 0) { - esp_nimble_deinit(); - } - - ret = esp_blufi_profile_deinit(); - if(ret != ESP_OK) { - return ret; - } - - esp_blufi_btc_deinit(); - - return ret; -} - -esp_err_t esp_blufi_gap_register_callback(void) -{ - return ESP_OK; -} - -esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *example_callbacks) -{ - esp_err_t ret = ESP_OK; - - ret = esp_blufi_register_callbacks(example_callbacks); - if(ret){ - BLUFI_ERROR("%s blufi register failed, error code = %x\n", __func__, ret); - return ret; - } - - ret = esp_blufi_gap_register_callback(); - if(ret){ - BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret); - return ret; - } - - ret = esp_blufi_host_init(); - if (ret) { - BLUFI_ERROR("%s initialise host failed: %s\n", __func__, esp_err_to_name(ret)); - return ret; - } - - return ret; -} - - -#endif /* CONFIG_BT_NIMBLE_ENABLED */ diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/main/blufi_security.c b/code/09.extra/wifi_config/blufi/blufi-esp32/main/blufi_security.c deleted file mode 100644 index c651e19..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/main/blufi_security.c +++ /dev/null @@ -1,227 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - */ - -#include -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event.h" -#include "esp_log.h" -#include "nvs_flash.h" -#include "esp_random.h" -#if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED -#include "esp_bt.h" -#endif - -#include "esp_blufi_api.h" -#include "blufi_example.h" - -#include "mbedtls/aes.h" -#include "mbedtls/dhm.h" -#include "mbedtls/md5.h" -#include "esp_crc.h" - -/* - The SEC_TYPE_xxx is for self-defined packet data type in the procedure of "BLUFI negotiate key" - If user use other negotiation procedure to exchange(or generate) key, should redefine the type by yourself. - */ -#define SEC_TYPE_DH_PARAM_LEN 0x00 -#define SEC_TYPE_DH_PARAM_DATA 0x01 -#define SEC_TYPE_DH_P 0x02 -#define SEC_TYPE_DH_G 0x03 -#define SEC_TYPE_DH_PUBLIC 0x04 - - -struct blufi_security { -#define DH_SELF_PUB_KEY_LEN 128 -#define DH_SELF_PUB_KEY_BIT_LEN (DH_SELF_PUB_KEY_LEN * 8) - uint8_t self_public_key[DH_SELF_PUB_KEY_LEN]; -#define SHARE_KEY_LEN 128 -#define SHARE_KEY_BIT_LEN (SHARE_KEY_LEN * 8) - uint8_t share_key[SHARE_KEY_LEN]; - size_t share_len; -#define PSK_LEN 16 - uint8_t psk[PSK_LEN]; - uint8_t *dh_param; - int dh_param_len; - uint8_t iv[16]; - mbedtls_dhm_context dhm; - mbedtls_aes_context aes; -}; -static struct blufi_security *blufi_sec; - -static int myrand( void *rng_state, unsigned char *output, size_t len ) -{ - esp_fill_random(output, len); - return( 0 ); -} - -extern void btc_blufi_report_error(esp_blufi_error_state_t state); - -void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free) -{ - int ret; - uint8_t type = data[0]; - - if (blufi_sec == NULL) { - BLUFI_ERROR("BLUFI Security is not initialized"); - btc_blufi_report_error(ESP_BLUFI_INIT_SECURITY_ERROR); - return; - } - - switch (type) { - case SEC_TYPE_DH_PARAM_LEN: - blufi_sec->dh_param_len = ((data[1]<<8)|data[2]); - if (blufi_sec->dh_param) { - free(blufi_sec->dh_param); - blufi_sec->dh_param = NULL; - } - blufi_sec->dh_param = (uint8_t *)malloc(blufi_sec->dh_param_len); - if (blufi_sec->dh_param == NULL) { - btc_blufi_report_error(ESP_BLUFI_DH_MALLOC_ERROR); - BLUFI_ERROR("%s, malloc failed\n", __func__); - return; - } - break; - case SEC_TYPE_DH_PARAM_DATA:{ - if (blufi_sec->dh_param == NULL) { - BLUFI_ERROR("%s, blufi_sec->dh_param == NULL\n", __func__); - btc_blufi_report_error(ESP_BLUFI_DH_PARAM_ERROR); - return; - } - uint8_t *param = blufi_sec->dh_param; - memcpy(blufi_sec->dh_param, &data[1], blufi_sec->dh_param_len); - ret = mbedtls_dhm_read_params(&blufi_sec->dhm, ¶m, ¶m[blufi_sec->dh_param_len]); - if (ret) { - BLUFI_ERROR("%s read param failed %d\n", __func__, ret); - btc_blufi_report_error(ESP_BLUFI_READ_PARAM_ERROR); - return; - } - free(blufi_sec->dh_param); - blufi_sec->dh_param = NULL; - - const int dhm_len = mbedtls_dhm_get_len(&blufi_sec->dhm); - ret = mbedtls_dhm_make_public(&blufi_sec->dhm, dhm_len, blufi_sec->self_public_key, dhm_len, myrand, NULL); - if (ret) { - BLUFI_ERROR("%s make public failed %d\n", __func__, ret); - btc_blufi_report_error(ESP_BLUFI_MAKE_PUBLIC_ERROR); - return; - } - - ret = mbedtls_dhm_calc_secret( &blufi_sec->dhm, - blufi_sec->share_key, - SHARE_KEY_BIT_LEN, - &blufi_sec->share_len, - myrand, NULL); - if (ret) { - BLUFI_ERROR("%s mbedtls_dhm_calc_secret failed %d\n", __func__, ret); - btc_blufi_report_error(ESP_BLUFI_DH_PARAM_ERROR); - return; - } - - ret = mbedtls_md5(blufi_sec->share_key, blufi_sec->share_len, blufi_sec->psk); - - if (ret) { - BLUFI_ERROR("%s mbedtls_md5 failed %d\n", __func__, ret); - btc_blufi_report_error(ESP_BLUFI_CALC_MD5_ERROR); - return; - } - - mbedtls_aes_setkey_enc(&blufi_sec->aes, blufi_sec->psk, 128); - - /* alloc output data */ - *output_data = &blufi_sec->self_public_key[0]; - *output_len = dhm_len; - *need_free = false; - - } - break; - case SEC_TYPE_DH_P: - break; - case SEC_TYPE_DH_G: - break; - case SEC_TYPE_DH_PUBLIC: - break; - } -} - -int blufi_aes_encrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len) -{ - int ret; - size_t iv_offset = 0; - uint8_t iv0[16]; - - memcpy(iv0, blufi_sec->iv, sizeof(blufi_sec->iv)); - iv0[0] = iv8; /* set iv8 as the iv0[0] */ - - ret = mbedtls_aes_crypt_cfb128(&blufi_sec->aes, MBEDTLS_AES_ENCRYPT, crypt_len, &iv_offset, iv0, crypt_data, crypt_data); - if (ret) { - return -1; - } - - return crypt_len; -} - -int blufi_aes_decrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len) -{ - int ret; - size_t iv_offset = 0; - uint8_t iv0[16]; - - memcpy(iv0, blufi_sec->iv, sizeof(blufi_sec->iv)); - iv0[0] = iv8; /* set iv8 as the iv0[0] */ - - ret = mbedtls_aes_crypt_cfb128(&blufi_sec->aes, MBEDTLS_AES_DECRYPT, crypt_len, &iv_offset, iv0, crypt_data, crypt_data); - if (ret) { - return -1; - } - - return crypt_len; -} - -uint16_t blufi_crc_checksum(uint8_t iv8, uint8_t *data, int len) -{ - /* This iv8 ignore, not used */ - return esp_crc16_be(0, data, len); -} - -esp_err_t blufi_security_init(void) -{ - blufi_sec = (struct blufi_security *)malloc(sizeof(struct blufi_security)); - if (blufi_sec == NULL) { - return ESP_FAIL; - } - - memset(blufi_sec, 0x0, sizeof(struct blufi_security)); - - mbedtls_dhm_init(&blufi_sec->dhm); - mbedtls_aes_init(&blufi_sec->aes); - - memset(blufi_sec->iv, 0x0, 16); - return 0; -} - -void blufi_security_deinit(void) -{ - if (blufi_sec == NULL) { - return; - } - if (blufi_sec->dh_param){ - free(blufi_sec->dh_param); - blufi_sec->dh_param = NULL; - } - mbedtls_dhm_free(&blufi_sec->dhm); - mbedtls_aes_free(&blufi_sec->aes); - - memset(blufi_sec, 0x0, sizeof(struct blufi_security)); - - free(blufi_sec); - blufi_sec = NULL; -} diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults b/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults deleted file mode 100644 index 7efb350..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults +++ /dev/null @@ -1,18 +0,0 @@ -# Override some defaults so BT stack is enabled -# in this example - -# -# Partition Table -# (It's possible to fit Blufi in 1MB app partition size with some other optimizations, but -# default config is close to 1MB.) -CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y - -# -# BT config -# -CONFIG_BT_ENABLED=y -CONFIG_BT_GATTC_ENABLE=n -CONFIG_BT_BLE_SMP_ENABLE=n -CONFIG_BT_BLE_BLUFI_ENABLE=y -CONFIG_MBEDTLS_HARDWARE_MPI=n -CONFIG_MBEDTLS_DHM_C=y diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32 b/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32 deleted file mode 100644 index 0043387..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32 +++ /dev/null @@ -1,7 +0,0 @@ -# This file was generated using idf.py save-defconfig. It can be edited manually. -# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration -# -CONFIG_IDF_TARGET="esp32" -CONFIG_BT_ENABLED=y -# CONFIG_BT_BLE_50_FEATURES_SUPPORTED is not set -CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32c2 b/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32c2 deleted file mode 100644 index 47fa261..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32c2 +++ /dev/null @@ -1,18 +0,0 @@ -# This file was generated using idf.py save-defconfig. It can be edited manually. -# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration -# -CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y -CONFIG_BT_ENABLED=y -CONFIG_BT_NIMBLE_BLUFI_ENABLE=y -# CONFIG_BT_GATTC_ENABLE is not set -# CONFIG_BT_BLE_SMP_ENABLE is not set -# CONFIG_BT_BLE_50_FEATURES_SUPPORTED is not set -CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y -CONFIG_MBEDTLS_DHM_C=y -# The config items for NIMBLE HOST -CONFIG_BT_NIMBLE_ENABLED=y -CONFIG_BT_NIMBLE_ROLE_CENTRAL=n -CONFIG_BT_NIMBLE_ROLE_OBSERVER=n -CONFIG_BT_NIMBLE_50_FEATURE_SUPPORT=n -CONFIG_BT_NIMBLE_SECURITY_ENABLE=n -CONFIG_BT_NIMBLE_CRYPTO_STACK_MBEDTLS=n diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32c3 b/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32c3 deleted file mode 100644 index 2cfad28..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32c3 +++ /dev/null @@ -1,7 +0,0 @@ -# This file was generated using idf.py save-defconfig. It can be edited manually. -# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration -# -CONFIG_IDF_TARGET="esp32c3" -CONFIG_BT_ENABLED=y -# CONFIG_BT_BLE_50_FEATURES_SUPPORTED is not set -CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32c6 b/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32c6 deleted file mode 100644 index 47fa261..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32c6 +++ /dev/null @@ -1,18 +0,0 @@ -# This file was generated using idf.py save-defconfig. It can be edited manually. -# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration -# -CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y -CONFIG_BT_ENABLED=y -CONFIG_BT_NIMBLE_BLUFI_ENABLE=y -# CONFIG_BT_GATTC_ENABLE is not set -# CONFIG_BT_BLE_SMP_ENABLE is not set -# CONFIG_BT_BLE_50_FEATURES_SUPPORTED is not set -CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y -CONFIG_MBEDTLS_DHM_C=y -# The config items for NIMBLE HOST -CONFIG_BT_NIMBLE_ENABLED=y -CONFIG_BT_NIMBLE_ROLE_CENTRAL=n -CONFIG_BT_NIMBLE_ROLE_OBSERVER=n -CONFIG_BT_NIMBLE_50_FEATURE_SUPPORT=n -CONFIG_BT_NIMBLE_SECURITY_ENABLE=n -CONFIG_BT_NIMBLE_CRYPTO_STACK_MBEDTLS=n diff --git a/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32s3 b/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32s3 deleted file mode 100644 index 631822e..0000000 --- a/code/09.extra/wifi_config/blufi/blufi-esp32/sdkconfig.defaults.esp32s3 +++ /dev/null @@ -1,7 +0,0 @@ -# This file was generated using idf.py save-defconfig. It can be edited manually. -# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration -# -CONFIG_IDF_TARGET="esp32s3" -CONFIG_BT_ENABLED=y -# CONFIG_BT_BLE_50_FEATURES_SUPPORTED is not set -CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y diff --git a/docs/docs/guide/.obsidian/workspace.json b/docs/docs/guide/.obsidian/workspace.json index 26f384d..f2878c6 100644 --- a/docs/docs/guide/.obsidian/workspace.json +++ b/docs/docs/guide/.obsidian/workspace.json @@ -4,22 +4,22 @@ "type": "split", "children": [ { - "id": "dbcfd28f34b2a13b", + "id": "8ff7430257e8e5f4", "type": "tabs", "children": [ { - "id": "ba89e06e4ba5dcbd", + "id": "c787cb3c2720eaca", "type": "leaf", "state": { "type": "markdown", "state": { - "file": "10.项目实战/10.1-模块驱动案例/10.1.2-SG90舵机/SG90舵机.md", + "file": "10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/42步进电机.md", "mode": "source", "source": false, "backlinks": false }, "icon": "lucide-file", - "title": "SG90舵机" + "title": "42步进电机" } } ] @@ -69,7 +69,7 @@ } ], "direction": "horizontal", - "width": 300 + "width": 240.5 }, "right": { "id": "5efe8913089ec8d6", @@ -85,13 +85,13 @@ "state": { "type": "outline", "state": { - "file": "10.项目实战/10.1-模块驱动案例/10.1.2-SG90舵机/SG90舵机.md", + "file": "10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/42步进电机.md", "followCursor": false, "showSearch": false, "searchQuery": "" }, "icon": "lucide-list", - "title": "SG90舵机 的大纲" + "title": "42步进电机 的大纲" } } ] @@ -107,23 +107,39 @@ "templates:插入模板": false } }, - "active": "ba89e06e4ba5dcbd", + "active": "70853e127a5d7008", "lastOpenFiles": [ - "10.项目实战/10.1-模块驱动案例/10.1.1-28BYJ-48步进电机/28BYJ-48步进电机.md", - "04.外设学习/4.1-基础外设/4.1.6-LED_PWM/LED_PWM控制.md", - "10.项目实战/10.1-模块驱动案例/10.1.2-SG90舵机/SG90舵机.md", - "10.项目实战/10.1-模块驱动案例/10.1.2-SG90舵机/attachments/Pasted image 20250528152414.png", - "10.项目实战/10.1-模块驱动案例/10.1.2-SG90舵机/attachments/Pasted image 20250528152307.png", - "10.项目实战/10.1-模块驱动案例/10.1.2-SG90舵机/attachments", - "10.项目实战/10.1-模块驱动案例/10.1.1-28BYJ-48步进电机/attachments/Pasted image 20250527165130.png", - "10.项目实战/10.1-模块驱动案例/10.1.1-28BYJ-48步进电机/attachments/Pasted image 20250527163637.png", - "10.项目实战/10.1-模块驱动案例/10.1.1-28BYJ-48步进电机/attachments/Pasted image 20250527163522.png", - "10.项目实战/10.1-模块驱动案例/10.1.1-28BYJ-48步进电机/attachments/Pasted image 20250527162100.png", - "10.项目实战/10.1-模块驱动案例/10.1.1-28BYJ-48步进电机/attachments/Pasted image 20250527161829.png", - "10.项目实战/10.1-模块驱动案例/10.1.1-28BYJ-48步进电机/attachments", + "10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/rc522模块.md", + "10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/42步进电机.md", "目录.md", - "04.外设学习/4.1-基础外设/4.1.1-GPIO/GPIO入门.md", "attachments/Templates.md", + "attachments/20250202192226.png", + "04.外设学习/4.3-其他外设/4.3.3-RMT/RMT.md", + "04.外设学习/4.3-其他外设/4.3.2-TWAI/TWAI.md", + "04.外设学习/4.3-其他外设/4.3.3-RMT", + "10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/attachments/Pasted image 20250624174842.png", + "10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/attachments/Pasted image 20250624174733.png", + "10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/attachments/Pasted image 20250624165127.png", + "10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/attachments", + "04.外设学习/4.1-基础外设/4.1.9-I2S/I2S入门.md", + "10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块", + "04.外设学习/4.1-基础外设/4.1.7-I2C/I2C通信.md", + "10.项目实战/10.1-模块驱动案例/10.1.1-28BYJ-48步进电机/28BYJ-48步进电机.md", + "10.项目实战/10.1-模块驱动案例/10.1.2-SG90舵机/SG90舵机.md", + "04.外设学习/4.3-其他外设/4.3.1-MCPWM/MCPWM.md", + "04.外设学习/4.3-其他外设/4.3.2-TWAI", + "10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604165352.png", + "10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604164929.png", + "10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604164852.png", + "10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604162407.png", + "10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604162227.png", + "10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604162038.png", + "10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments", + "10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机", + "04.外设学习/4.1-基础外设/4.1.6-LED_PWM/LED_PWM控制.md", + "10.项目实战/10.1-模块驱动案例/10.1.2-SG90舵机/attachments", + "10.项目实战/10.1-模块驱动案例/10.1.1-28BYJ-48步进电机/attachments", + "04.外设学习/4.1-基础外设/4.1.1-GPIO/GPIO入门.md", "04.外设学习/4.1-基础外设/4.1.4-ADC/ADC模数转换.md", "01.认识ESP32/1.1-ESP32介绍/ESP32介绍.md", "00.常见问题及解决方案/常见问题及解决方案.md", @@ -131,12 +147,7 @@ "10.项目实战/10.1-模块驱动案例/未命名.md", "10.项目实战/10.1-模块驱动案例/10.1.1-28BYJ-48步进电机", "04.外设学习/4.4-外设使用案例/未命名.md", - "04.外设学习/4.4-外设使用案例/未命名", - "10.项目实战/10.1-模块驱动案例", - "00.常见问题及解决方案/attachments/20250210094506.png", - "attachments/20250202192226.png", "01.认识ESP32/1.2-ESP32开发环境搭建/1.环境搭建-Windows.md", - "03.FreeRTOS基础/3.4-FreeRTOS软件定时器/FreeRTOS软件定时器.md", - "00.常见问题及解决方案/attachments/20250506154522.png" + "03.FreeRTOS基础/3.4-FreeRTOS软件定时器/FreeRTOS软件定时器.md" ] } \ No newline at end of file diff --git a/docs/docs/guide/04.外设学习/4.1-基础外设/4.1.9-I2S/I2S入门.md b/docs/docs/guide/04.外设学习/4.1-基础外设/4.1.9-I2S/I2S入门.md index 6a37918..45e2a2e 100644 --- a/docs/docs/guide/04.外设学习/4.1-基础外设/4.1.9-I2S/I2S入门.md +++ b/docs/docs/guide/04.外设学习/4.1-基础外设/4.1.9-I2S/I2S入门.md @@ -29,10 +29,8 @@ Inter-IC Sount Bus(I2S)是飞利浦半导体公司(现为恩智浦半导体公 **SCK: (continuous serial clock) 串行时钟**:标准名称为“连续串列主频” - **WS: (word select) 字段(声道)选择:** 也称为“帧同步线” - **SD: (serial data) 串行数据 :** 也可称为SDATA,SDIN,SDOUT,DACDAT,ADCDAT等 diff --git a/docs/docs/guide/04.外设学习/4.3-其他外设/4.3.2-TWAI/TWAI.md b/docs/docs/guide/04.外设学习/4.3-其他外设/4.3.2-TWAI/TWAI.md new file mode 100644 index 0000000..50c5807 --- /dev/null +++ b/docs/docs/guide/04.外设学习/4.3-其他外设/4.3.2-TWAI/TWAI.md @@ -0,0 +1,99 @@ +# 双线汽车接口 (TWAI) -(CAN) + +> [!TIP] 🚀 ESP32 外设之双线汽车接口 (TWAI) | +> - 💡 **碎碎念**😎:本节将介绍 ESP32 的双线汽车接口 (TWAI) ,他是是一种适用于汽车和工业应用的实时串行通信协议。 +> - 📺 **视频教程**:🚧 *开发中* +> - 💾 **示例代码**:🚧 *开发中* + +双线汽车接口 (TWAI) 是一种适用于汽车和工业应用的实时串行通信协议。它兼容 ISO11898-1 经典帧,因此可以支持标准帧格式(11 位 ID)和扩展帧格式(29 位 ID)。ESP32 包含 1 个 TWAI 控制器,经配置可以在 TWAI 总线上使用外部收发器通信。 + +注意:TWAI 控制器不兼容 ISO11898-1 FD 格式帧,并会将这些帧解析为错误。 + +## 一、前言简介 + +- 简介:TWAI(Two-Wire Automotive Interface)是 ESP32 提供的 CAN 协议兼容实现。 +- 协议兼容性:支持 ISO11898-1 标准帧和扩展帧格式,不支持 CAN-FD。 +- 应用场景: + - 车载设备通信 + - 工业自动化系统 + - 多主多从嵌入式网络 + +--- + +## 二、协议基础 + +- TWAI = Classic CAN(兼容) +- 帧格式: + - 标准帧(11 位 ID) + - 扩展帧(29 位 ID) + - 数据帧、远程帧、错误帧 +- 仲裁机制与优先级 +- 位定时与波特率选择(如 500kbps) + +--- + +## 三、硬件准备 + +- ESP32 模块(推荐 WROOM/WROVER 系列) +- CAN 收发器(如 SN65HVD230、TJA1050) +- 接线说明: + - ESP32 TX ➝ 收发器 TXD + - ESP32 RX ➝ 收发器 RXD + - CANH、CANL ➝ 总线 +- 加 120Ω 终端电阻于总线两端 +- 电源要求与隔离建议(如长距离通信建议使用隔离型收发器) + +--- + +## 四、软件环境配置(ESP-IDF) + +- 驱动初始化步骤: + - `twai_driver_install()` + - `twai_start()` +- 主要结构体: + - `twai_message_t`:数据帧定义 + - `twai_general_config_t`、`twai_timing_config_t`、`twai_filter_config_t` +- 示例配置波特率、引脚、模式等参数 +- 使用 FreeRTOS 任务接收数据(推荐) + +--- + +## 五、发送与接收示例 + +- ✅ 发送帧示例(标准帧、扩展帧) +- ✅ 接收帧示例(阻塞与非阻塞) +- 🔁 循环发送测试数据 +- 🛑 超时处理和帧校验 +- 🚦 使用中断或轮询方式收发 + +--- + +## 六、调试与验证 + +- 使用逻辑分析仪/示波器查看 CANH/CANL 波形 +- 多设备组网互通验证(例如连接另一个 STM32/开发板) +- 常见问题排查: + - 没有数据 ➝ 检查终端电阻、电压、接线 + - 接收错误 ➝ 检查帧格式、波特率一致性 + - 控制器状态 ➝ 使用 `twai_get_status_info()` 查看错误码 + +--- + +## 七、高级应用(可选) + +- 设置 ID 过滤器(精确匹配 / 范围匹配) +- 发送优先级控制(低 ID = 高优先级) +- 状态监测:总线关闭、错误计数 +- 灵活切换正常模式 / 回环模式 / 监听模式 + +--- + +## 八、总结与资料 + +- 官方文档: + - [ESP-IDF TWAI 文档](https://docs.espressif.com/projects/esp-idf/zh/latest/esp32/api-reference/peripherals/twai.html) +- 推荐资料: + - CAN 协议详解(推荐阅读 Bosch CAN spec) + - 收发器芯片选型参考 +- 视频教程:🚧 *开发中* +- 示例代码:🚧 *开发中* diff --git a/docs/docs/guide/04.外设学习/4.3-其他外设/4.3.3-RMT/RMT.md b/docs/docs/guide/04.外设学习/4.3-其他外设/4.3.3-RMT/RMT.md new file mode 100644 index 0000000..0a84751 --- /dev/null +++ b/docs/docs/guide/04.外设学习/4.3-其他外设/4.3.3-RMT/RMT.md @@ -0,0 +1,4 @@ +# 红外遥控 (RMT) + + +红外遥控 (RMT) 外设是一个红外发射和接收控制器。其数据格式灵活,可进一步扩展为多功能的通用收发器,发送或接收多种类型的信号。就网络分层而言,RMT 硬件包含物理层和数据链路层。物理层定义通信介质和比特信号的表示方式,数据链路层定义 RMT 帧的格式。 \ No newline at end of file diff --git a/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/42步进电机.md b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/42步进电机.md new file mode 100644 index 0000000..0b0b840 --- /dev/null +++ b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/42步进电机.md @@ -0,0 +1,108 @@ + +# ESP32驱动42步进电机 + +> [!TIP] 🚀 ESP32驱动42步进电机 | XXXXX +> - 💡 **碎碎念**😎:本节将介绍XXXXXXXXXXXXXXXXXXX +> - 📺 **视频教程**:🚧 *开发中* +> - 💾 **示例代码**:🚧 *开发中* +> + +## 一、介绍 + +### 1.1 什么是42步进电机? + +42步进电机是一种常见的小型两相步进电机,因其电机外壳边长为 42mm 而得名(即 NEMA 17 尺寸标准)。该类电机广泛应用于 3D打印机、激光雕刻机、CNC加工设备、机器人 等对位置控制要求较高的场景。 + +常见参数如下: + +- 步距角:1.8°(每转200步)或 0.9°(每转400步) +- 工作电压:一般为 12V 或 24V +- 峰值电流:0.8A ~ 2A + +### 1.2 步进电机工作原理 + +步进电机是一种将电脉冲信号转换为角位移的电机。当控制器发出一个脉冲信号,电机就按照设定角度旋转一步。通过调节脉冲频率,可以控制电机的转速;通过控制脉冲数量,可以精准控制角位移。 + +具体的工作原理在此不再赘述,大家可自行上网查阅。由于其不需要编码器即可进行位置控制,特别适合需要开环控制的系统。 + +下面简单对比步进电机与直流电机 / 伺服电机的区别: + +| 特性 | 步进电机 | 直流电机 | 伺服电机 | +| ---- | --------------- | --------- | ---------- | +| 控制方式 | 开环(可闭环) | 电压/电流控制 | 闭环(带编码器) | +| 精度 | 高(0.9°\~1.8°/步) | 低 | 非常高(编码器支持) | +| 启停性能 | 好 | 中 | 非常好 | +| 振动噪音 | 较大(尤其低速) | 小 | 极小 | +| 价格 | 便宜 | 更便宜 | 昂贵 | +| 适合应用 | 中低速定位控制 | 普通旋转/风扇/泵 | 高速高精定位控制 | + +值得注意的是,我们需要了解一下细分技术: + +传统步进电机的步距角通常为 1.8°(每圈 200 步),在一些对平稳性和精度要求更高的场合,1.8° 的步进角可能仍然过大,运行过程中会出现抖动、不平滑等问题。为此,引入了 细分驱动(Microstepping) 技术。 + +细分驱动器不是简单地控制电机绕组全电流通断,而是通过 PWM 控制 或 电流恒流分级控制,将每个完整步细分为若干个微小步。比如: + +- 半步细分:每步 0.9°,一圈 400 步; +- 1/8 细分:每步 0.225°,一圈 1600 步; +- 1/16 细分:每步 0.1125°,一圈 3200 步; + +细分的优势在于提高分辨率和控制精度,运行更平滑、减少共振,降低噪音和震动和改善电机低速运行性能。虽然细分技术能提高控制平滑度,但并不能无限提高“实际定位精度”,因为电机本身的机械和磁滞误差仍然存在。高细分主要用于改善运动特性,而不是绝对定位精度。并且市面上常见的步进电机驱动器基本都支持细分。 +### 1.3 常用驱动器 + +步进电机无法直接由电源驱动,必须通过驱动器精确控制线圈的电流方向与大小。以下是几种常用驱动器及其特点: +#### 1. A4988 驱动芯片 + +A4988 是一种体积小、成本低的双极步进电机驱动模块,广泛应用于 3D 打印机、桌面级 CNC 等入门级设备。它支持全步、半步、1/4、1/8 和 1/16 微步细分,并带有可调电流限制功能。模块自带过热保护和欠压保护,接口简单、体积小巧,适合面包板快速开发。但需要注意的是,该模块在高电流下易发热,接线错误或通电顺序错误容易烧毁,因此在使用时要特别注意接线安全与散热。 + +![](attachments/Pasted%20image%2020250604162038.png) +#### 2. tb6600 驱动器 + +TB6600 驱动器是面向工业级场景设计的中功率步进电机驱动器,支持工作电压 9V 到 42V,最大输出电流可达 4.5A,常用于控制较大扭矩的 42、57、86 步进电机。它支持的细分模式从全步到 32步,用户可通过拨码开关手动配置。TB6600 驱动器通常带有铝合金壳体和散热片,接口清晰,安装方便,适用于 CNC 雕刻机、激光切割、机械臂等中大型场合。相比 A4988,TB6600 功率更大、稳定性更好,但体积较大,不适合空间受限的设计,且价格略高。 + +![](attachments/Pasted%20image%2020250604162227.png) + +#### 3.DRV8825驱动芯片 + +DRV8825 是德州仪器推出的一款高性能步进电机驱动器,与 A4988 引脚兼容但性能更强。其支持最多 1/32 步细分。相比 A4988,它支持更高的工作电压,能够在更高速度下提供更大的扭矩,同时运行更安静,抖动更小。它适用于对噪音和稳定性有更高要求的项目,如中高端 3D 打印机或机器人项目。不过也需注意散热问题,可以在模块上添加散热片。 + +![](attachments/Pasted%20image%2020250604162407.png) + + + +## 二、使用 + +下面简单介绍一下使用ESP32配合DRV8825驱动芯片驱动42步进电机: + +首先介绍DRV8825驱动模块,我们买到的DRV8825步进驱动模块一般如下图: + +![](attachments/Pasted%20image%2020250604162407.png) + +DRV8825 是德州仪器设计的集成电机驱动芯片。 芯片内部集成了2个H桥电路和1/32微步进分度器, 可以驱动一个双极型电机或两个直流有刷电机。主要有如下特点: + +1. 适合驱动8.2V~45V2.5A以下的步进电机: +2. 只有简单的步进和方向控制接口; +3. 六个不同的步进模式:全、半、1/4、1/8、1/16、1/32; +4. 可调电位器可以调节最大电流输出,从而获得更高的步进率; +5. 自动电流衰减模式检测/选择; +6. 过热关闭电路、欠压锁定、交叉电流保护; + +它的引脚定义是这样的: + + +![](attachments/Pasted%20image%2020250604164929.png) + + +这是芯片手册中的简化控制示意图: + +![](attachments/Pasted%20image%2020250604164852.png) + +## 三、示例 + + + + + + + +# 参考链接 + diff --git a/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604162038.png b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604162038.png new file mode 100644 index 0000000..c768dc7 Binary files /dev/null and b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604162038.png differ diff --git a/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604162227.png b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604162227.png new file mode 100644 index 0000000..3e24b32 Binary files /dev/null and b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604162227.png differ diff --git a/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604162407.png b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604162407.png new file mode 100644 index 0000000..be74915 Binary files /dev/null and b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604162407.png differ diff --git a/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604164852.png b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604164852.png new file mode 100644 index 0000000..a5e1f34 Binary files /dev/null and b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604164852.png differ diff --git a/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604164929.png b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604164929.png new file mode 100644 index 0000000..ca9efcd Binary files /dev/null and b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604164929.png differ diff --git a/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604165352.png b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604165352.png new file mode 100644 index 0000000..5207080 Binary files /dev/null and b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.3-42步进电机/attachments/Pasted image 20250604165352.png differ diff --git a/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/attachments/Pasted image 20250624165127.png b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/attachments/Pasted image 20250624165127.png new file mode 100644 index 0000000..0aaae78 Binary files /dev/null and b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/attachments/Pasted image 20250624165127.png differ diff --git a/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/attachments/Pasted image 20250624174733.png b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/attachments/Pasted image 20250624174733.png new file mode 100644 index 0000000..6c47a38 Binary files /dev/null and b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/attachments/Pasted image 20250624174733.png differ diff --git a/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/attachments/Pasted image 20250624174842.png b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/attachments/Pasted image 20250624174842.png new file mode 100644 index 0000000..b5379e4 Binary files /dev/null and b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/attachments/Pasted image 20250624174842.png differ diff --git a/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/rc522模块.md b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/rc522模块.md new file mode 100644 index 0000000..1a1803d --- /dev/null +++ b/docs/docs/guide/10.项目实战/10.1-模块驱动案例/10.1.4-rc522模块/rc522模块.md @@ -0,0 +1,140 @@ +# ESP32驱动RC522模块与 RFID / NFC 卡通信 + +> [!TIP] 🚀 ESP32驱动RC522射频IC卡识别模块 | 轻松实现 RFID / NFC 卡通信 +> +> * 💡 **碎碎念**😎:本节将介绍如何使用 ESP32 驱动 RC522 模块,快速实现 RFID/NFC 卡片的检测和读取。 +> * 📺 **视频教程**:🚧 *开发中* +> * 💾 **示例代码**:🚧 *开发中* + +## 一、介绍 + +![](attachments/Pasted%20image%2020250624165127.png) + +RC522 是一款常见的 RFID 读卡器模块,工作在 **13.56MHz** 频段,支持对标准 NFC / IC 卡片进行检测和通信。结合 ESP32 可快速构建: + +* 门禁控制器 +* NFC 标签检测器 +* RFID 学生证管理器 +* 简单的考勤、登录、支付等应用场景 + +RC522 通常通过 **SPI 接口** 与 ESP32 通信,库和示例代码完善,适合入门和快速验证。 + +## 二、使用 + +### 1️⃣ 硬件连接 + +| ESP32引脚 | RC522引脚 | 功能 | +| ------- | ------- | -------- | +| 3V3 | 3.3V | 供电 | +| GND | GND | 地线 | +| GPIO18 | SCK | SPI时钟 | +| GPIO19 | MISO | SPI MISO | +| GPIO23 | MOSI | SPI MOSI | +| GPIO5 | RST | 重置 | +| GPIO4 | SDA | SPI片选 | + +### 2️⃣ 配套库 + +* ESP-IDF: https://components.espressif.com/components/abobija/rc522/versions/3.3.2/versions?language=en + +### 3️⃣ 通信流程 + +1. ESP32 通过 SPI 与 RC522 通信。 +2. 检测是否有 RFID/NFC 标签进入。 +3. 获取 UID。 +4. 可选:读取或写入 IC 卡数据。 + +## 三、示例 + +接线: + +``` c +#define RC522_SPI_BUS_GPIO_MISO (25) +#define RC522_SPI_BUS_GPIO_MOSI (23) +#define RC522_SPI_BUS_GPIO_SCLK (19) +#define RC522_SPI_SCANNER_GPIO_SDA (22) +``` + +![](attachments/Pasted%20image%2020250624174733.png) + +代码: + +```cpp +#include +#include "rc522.h" +#include "driver/rc522_spi.h" +#include "rc522_picc.h" + +static const char *TAG = "rc522-basic-example"; + +#define RC522_SPI_BUS_GPIO_MISO (25) +#define RC522_SPI_BUS_GPIO_MOSI (23) +#define RC522_SPI_BUS_GPIO_SCLK (19) +#define RC522_SPI_SCANNER_GPIO_SDA (22) +#define RC522_SCANNER_GPIO_RST (-1) + +static rc522_spi_config_t driver_config = { + .host_id = SPI2_HOST, + .bus_config = &(spi_bus_config_t){ + .miso_io_num = RC522_SPI_BUS_GPIO_MISO, + .mosi_io_num = RC522_SPI_BUS_GPIO_MOSI, + .sclk_io_num = RC522_SPI_BUS_GPIO_SCLK, + }, + .dev_config = { + .spics_io_num = RC522_SPI_SCANNER_GPIO_SDA, + }, + .rst_io_num = RC522_SCANNER_GPIO_RST, +}; + +static rc522_driver_handle_t driver; +static rc522_handle_t scanner; + +static void on_picc_state_changed(void *arg, esp_event_base_t base, int32_t event_id, void *data) +{ + rc522_picc_state_changed_event_t *event = (rc522_picc_state_changed_event_t *)data; + rc522_picc_t *picc = event->picc; + + if (picc->state == RC522_PICC_STATE_ACTIVE) { + rc522_picc_print(picc); + } + else if (picc->state == RC522_PICC_STATE_IDLE && event->old_state >= RC522_PICC_STATE_ACTIVE) { + ESP_LOGI(TAG, "Card has been removed"); + } +} + +void app_main() +{ + rc522_spi_create(&driver_config, &driver); + rc522_driver_install(driver); + + rc522_config_t scanner_config = { + .driver = driver, + }; + + rc522_create(&scanner_config, &scanner); + rc522_register_events(scanner, RC522_EVENT_PICC_STATE_CHANGED, on_picc_state_changed, NULL); + rc522_start(scanner); +} + +``` + +效果: + +![](attachments/Pasted%20image%2020250624174842.png) + + +### 其他 + +在使用esp-idf-rc522这个库时,发现通信引脚不能随意配置 + +对于ESP32来说,能够正常使用的配置如下: + +``` +#define RC522_SPI_BUS_GPIO_MISO (25) +#define RC522_SPI_BUS_GPIO_MOSI (23) +#define RC522_SPI_BUS_GPIO_SCLK (19) +#define RC522_SPI_SCANNER_GPIO_SDA (22) +#define RC522_SCANNER_GPIO_RST (-1) +``` + +具体见: https://github.com/abobija/esp-idf-rc522/issues/71 \ No newline at end of file