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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
#![deny(missing_docs)] //! Wifi QR codes are a way to encode wifi connection information and credentials into a QR code so that it can be scanned. They are supported via the latest Android and iOS phones, as well as other platforms. //! //! It is important to take into account that QR codes do not provide any security mechanisms that would prevent someone from just reading the code and recovering the password for the network. Android requires that you re-authenticate before it will display the QR code on the screen to make sure the user is allowed to share that information, for example. pub use qrcode_generator::QrCodeEcc; use std::io::Write; /// Encode credentials as a matrix of boolean values. This is useful when manually generating an image. /// /// # Examples /// /// ``` /// use wifi_qr_code::QrCodeEcc; /// use wifi_qr_code::{AuthenticationType, Visibility, WifiCredentials}; /// /// let wifi_credentials = WifiCredentials { /// ssid: String::from("example ssid"), /// authentication_type: AuthenticationType::WPA(String::from("example password")), /// visibility: Visibility::Hidden, /// }; /// wifi_qr_code::encode_as_matrix(&wifi_credentials, QrCodeEcc::Medium); /// ``` pub fn encode_as_matrix( wifi_credentials: &WifiCredentials, qr_code_error_checking: QrCodeEcc, ) -> Result<Vec<Vec<bool>>, std::io::Error> { qrcode_generator::to_matrix(wifi_credentials.encode(), qr_code_error_checking) } /// Encode credentials as raw image data. This is useful when generating the QR code and then manipulating it with an image library. /// /// # Examples /// /// ``` /// use wifi_qr_code::QrCodeEcc; /// use wifi_qr_code::{AuthenticationType, Visibility, WifiCredentials}; /// /// let wifi_credentials = WifiCredentials { /// ssid: String::from("example ssid"), /// authentication_type: AuthenticationType::WPA(String::from("example password")), /// visibility: Visibility::Hidden, /// }; /// wifi_qr_code::encode_as_image(&wifi_credentials, QrCodeEcc::Medium, 100); /// ``` pub fn encode_as_image( wifi_credentials: &WifiCredentials, qr_code_error_checking: QrCodeEcc, image_size: usize, ) -> Result<Vec<u8>, std::io::Error> { qrcode_generator::to_image( wifi_credentials.encode(), qr_code_error_checking, image_size, ) } /// Encode credentials as a PNG image. /// /// # Examples /// /// ``` /// use wifi_qr_code::QrCodeEcc; /// use wifi_qr_code::{AuthenticationType, Visibility, WifiCredentials}; /// /// use std::fs::File; /// /// let wifi_credentials = WifiCredentials { /// ssid: String::from("example ssid"), /// authentication_type: AuthenticationType::WPA(String::from("example password")), /// visibility: Visibility::Hidden, /// }; /// let png_file = File::create("wifi_qr.png").expect("Failed to create example PNG file."); /// wifi_qr_code::encode_as_png(&wifi_credentials, QrCodeEcc::Medium, 100, png_file); /// ``` pub fn encode_as_png( wifi_credentials: &WifiCredentials, qr_code_error_checking: QrCodeEcc, image_size: usize, writer: impl Write, ) -> Result<(), std::io::Error> { qrcode_generator::to_png( wifi_credentials.encode(), qr_code_error_checking, image_size, writer, ) } /// Encode credentials as an SVG image. /// /// # Examples /// /// ``` /// use wifi_qr_code::QrCodeEcc; /// use wifi_qr_code::{AuthenticationType, Visibility, WifiCredentials}; /// /// use std::fs::File; /// /// let wifi_credentials = WifiCredentials { /// ssid: String::from("example ssid"), /// authentication_type: AuthenticationType::WPA(String::from("example password")), /// visibility: Visibility::Hidden, /// }; /// let svg_file = File::create("wifi_qr.svg").expect("Failed to create example SVG file."); /// wifi_qr_code::encode_as_svg(&wifi_credentials, QrCodeEcc::Medium, 100, Some("Example Wifi QR Code"), svg_file); /// ``` pub fn encode_as_svg( wifi_credentials: &WifiCredentials, qr_code_error_checking: QrCodeEcc, image_size: usize, description: Option<&str>, writer: impl Write, ) -> Result<(), std::io::Error> { qrcode_generator::to_svg( wifi_credentials.encode(), qr_code_error_checking, image_size, description, writer, ) } /// Declare whether the network is authenticated via WEP with a password, WPA with a password, or if the network is open. pub enum AuthenticationType { /// WEP authentication is an older family of protocols. It is not particularly secure and wireless access points should use a more modern methods such as the WPA family of authentication protocols. WEP(String), /// WPA authentication is a more modern family of protocols. Typically, wireless networks will use WPA2 as their protocol implementation. WPA(String), /// No password / open access is particularly rare because it is possible for malicious actors to read all unencrypted traffic going across the network. NoPassword, } impl AuthenticationType { fn encode(&self) -> String { match self { Self::WEP(password) => format!("T:WEP;P:{};", escape(password)), Self::WPA(password) => format!("T:WPA;P:{};", escape(password)), Self::NoPassword => String::from("T:nopass;"), } } } /// Declare whether the network is broadcasting its availability. pub enum Visibility { /// Visible wifi networks display in lists of networks when a device scans an area. Visible, /// Hidden wifi networks do not show up on scans and must be known by their SSID to be accessed. Hidden, } impl Visibility { fn encode(&self) -> String { match self { Self::Visible => String::from("H:false;"), Self::Hidden => String::from("H:true;"), } } } /// The credentials needed to completely connect to a wifi network. pub struct WifiCredentials { /// The SSID of a wifi network is the name used to access it. pub ssid: String, /// The authentication type of a wifi network determines the protocol used to access it and the password required to properly authenticate to it. pub authentication_type: AuthenticationType, /// The visibility of a wifi network determines if it can be seen by any device or if it must be known by SSID beforehand. pub visibility: Visibility, } impl WifiCredentials { /// Encode the credentials into the form expected for a wifi QR Code. Special characters (i.e. ";,:\) will be escaped in the output. /// /// # Examples /// /// ``` /// use wifi_qr_code::{AuthenticationType, Visibility, WifiCredentials}; /// /// let wifi_credentials = WifiCredentials { /// ssid: String::from("example ssid"), /// authentication_type: AuthenticationType::WPA(String::from("example password")), /// visibility: Visibility::Hidden, /// }; /// assert_eq!("WIFI:S:example ssid;T:WPA;P:example password;H:true;;", wifi_credentials.encode()); /// ``` pub fn encode(&self) -> String { format!( "WIFI:{}{}{};", self.encode_ssid(), self.authentication_type.encode(), self.visibility.encode() ) } fn encode_ssid(&self) -> String { format!("S:{};", escape(&self.ssid)) } } fn escape(input: &str) -> String { String::from(input) .replace(r#"\"#, r#"\\"#) .replace(r#"""#, r#"\""#) .replace(";", r#"\;"#) .replace(",", r#"\,"#) .replace(":", r#"\:"#) } #[cfg(test)] mod tests { use super::*; #[test] fn it_encodes_valid_wifi_forms() { // WIFI:S:<SSID>;T:<WPA|WEP|>;P:<password>;H:<true|false|>; let wifi_credentials = WifiCredentials { ssid: String::from("test ssid"), authentication_type: AuthenticationType::WEP(String::from("test password")), visibility: Visibility::Visible, }; assert_eq!( "WIFI:S:test ssid;T:WEP;P:test password;H:false;;", &wifi_credentials.encode() ); let wifi_credentials = WifiCredentials { ssid: String::from("test ssid"), authentication_type: AuthenticationType::WPA(String::from("test password")), visibility: Visibility::Hidden, }; assert_eq!( "WIFI:S:test ssid;T:WPA;P:test password;H:true;;", &wifi_credentials.encode() ); let wifi_credentials = WifiCredentials { ssid: String::from("test ssid"), authentication_type: AuthenticationType::NoPassword, visibility: Visibility::Visible, }; assert_eq!( "WIFI:S:test ssid;T:nopass;H:false;;", &wifi_credentials.encode() ); } #[test] fn it_properly_handles_escaped_characters() { let wifi_credentials = WifiCredentials { ssid: String::from(r#"special_characters ";,:\"#), authentication_type: AuthenticationType::WEP(String::from( r#"special_characters ";,:\"#, )), visibility: Visibility::Visible, }; assert_eq!( r#"WIFI:S:special_characters \"\;\,\:\\;T:WEP;P:special_characters \"\;\,\:\\;H:false;;"#, &wifi_credentials.encode() ); } }