-
Notifications
You must be signed in to change notification settings - Fork 14
/
wifiqr.go
48 lines (40 loc) · 1.3 KB
/
wifiqr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package wifiqr
import (
"strconv"
"strings"
"github.com/skip2/go-qrcode"
)
// There are several levels of error detection/recovery capacity. Higher levels
// of error recovery are able to correct more errors, with the trade-off of
// increased symbol size.
var defaultRecoveryLevel qrcode.RecoveryLevel = qrcode.High
// InitCode returns the qrcode.QRCode based on the configuration.
func InitCode(config *Config) (*qrcode.QRCode, error) {
return qrcode.New(buildSchema(config), defaultRecoveryLevel)
}
// escapeString escapes the special characters with a backslash.
func escapeString(s string) string {
// https://github.com/zxing/zxing/wiki/Barcode-Contents#wi-fi-network-config-android-ios-11
for _, c := range []byte{'\\', ';', ',', '"', ':'} {
s = strings.ReplaceAll(s, string(c), `\`+string(c))
}
return s
}
// WIFI:S:My_SSID;T:WPA;P:key goes here;H:false;
// ^ ^ ^ ^ ^
// | | | | +-- hidden SSID (true/false)
// | | | +-- WPA key
// | | +-- encryption type
// | +-- ESSID
// +-- code type
func buildSchema(config *Config) string {
return "WIFI:S:" +
escapeString(config.SSID) +
";T:" +
config.Encryption.Code() +
";P:" +
escapeString(config.Key) +
";H:" +
strconv.FormatBool(config.Hidden) +
";"
}