Polish codes of QR code scanner

This commit is contained in:
Yishi Lin 2019-10-03 14:27:00 +08:00
parent e4630e950d
commit 6f295be74d
5 changed files with 57 additions and 107 deletions

View file

@ -17,54 +17,34 @@ class GitSSHKeyArmorSettingTableViewController: AutoCellHeightUITableViewControl
let passwordStore = PasswordStore.shared let passwordStore = PasswordStore.shared
class ScannedSSHKey { class ScannedSSHKey {
static let maxNumberOfGif = 100 var segments = [String]()
var numberOfSegments = 0
var previousSegment = ""
var key = ""
var message = "" var message = ""
var hasStarted = false
var isDone = false
func reset() { func reset() {
numberOfSegments = 0 self.segments.removeAll()
previousSegment = ""
key = ""
message = "LookingForStartingFrame.".localize() message = "LookingForStartingFrame.".localize()
hasStarted = false
isDone = false
} }
func addSegment(segment: String) { func addSegment(segment: String) -> (accept: Bool, message: String) {
// skip duplicated segments // Skip duplicated segments.
guard segment != previousSegment else { guard segment != self.segments.last else {
return return (accept: false, message: self.message)
}
previousSegment = segment
// check whether we have found the first block
if hasStarted == false {
hasStarted = segment.contains("-----BEGIN")
}
guard hasStarted == true else {
return
} }
// check the number of segments // Check whether we have found the first block.
numberOfSegments = numberOfSegments + 1 guard !self.segments.isEmpty || segment.contains("-----BEGIN") else {
guard numberOfSegments <= ScannedSSHKey.maxNumberOfGif else { return (accept: false, message: self.message)
key = "TooManyQrCodes".localize()
return
} }
// update full text and check whether we are done // Update the list of scanned segment and return.
key.append(segment) self.segments.append(segment)
if let index1 = key.range(of: "-----END")?.lowerBound, if segment.range(of: "-----END.*KEY-----", options: .regularExpression, range: nil, locale: nil) != nil {
let _ = key.suffix(from: index1).range(of: "KEY-----")?.lowerBound { self.message = "Done".localize()
isDone = true return (accept: true, message: self.message)
} else {
self.message = "ScannedQrCodes(%d)".localize(self.segments.count)
return (accept: false, message: self.message)
} }
// update message
message = "ScannedQrCodes(%d)".localize(numberOfSegments)
} }
} }
var scanned = ScannedSSHKey() var scanned = ScannedSSHKey()
@ -109,17 +89,12 @@ class GitSSHKeyArmorSettingTableViewController: AutoCellHeightUITableViewControl
// MARK: - QRScannerControllerDelegate Methods // MARK: - QRScannerControllerDelegate Methods
func checkScannedOutput(line: String) -> (accept: Bool, message: String) { func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
scanned.addSegment(segment: line) return scanned.addSegment(segment: line)
if scanned.isDone {
return (accept: true, message: "Done".localize())
} else {
return (accept: false, message: scanned.message)
}
} }
// MARK: - QRScannerControllerDelegate Methods // MARK: - QRScannerControllerDelegate Methods
func handleScannedOutput(line: String) { func handleScannedOutput(line: String) {
armorPrivateKeyTextView.text = scanned.key armorPrivateKeyTextView.text = scanned.segments.joined(separator: "")
} }
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

View file

@ -19,71 +19,47 @@ class PGPKeyArmorSettingTableViewController: AutoCellHeightUITableViewController
let keychain = AppKeychain.shared let keychain = AppKeychain.shared
class ScannedPGPKey { class ScannedPGPKey {
static let maxNumberOfGif = 100
enum KeyType { enum KeyType {
case publicKey, privateKey case publicKey, privateKey
} }
var keyType = KeyType.publicKey var keyType = KeyType.publicKey
var numberOfSegments = 0 var segments = [String]()
var previousSegment = ""
var key = ""
var message = "" var message = ""
var hasStarted = false
var isDone = false
func reset(keytype: KeyType) { func reset(keytype: KeyType) {
self.keyType = keytype self.keyType = keytype
numberOfSegments = 0 self.segments.removeAll()
previousSegment = ""
key = ""
message = "LookingForStartingFrame.".localize() message = "LookingForStartingFrame.".localize()
hasStarted = false
isDone = false
} }
func addSegment(segment: String) { func addSegment(segment: String) -> (accept: Bool, message: String) {
// skip duplicated segments let keyTypeStr = self.keyType == .publicKey ? "Public" : "Private"
guard segment != previousSegment else { let theOtherKeyTypeStr = self.keyType == .publicKey ? "Private" : "Public"
return
}
previousSegment = segment
// check whether we have found the first block // Skip duplicated segments.
if hasStarted == false { guard segment != self.segments.last else {
let findPublic = segment.contains("-----BEGIN PGP PUBLIC KEY BLOCK-----") return (accept: false, message: self.message)
let findPrivate = segment.contains("-----BEGIN PGP PRIVATE KEY BLOCK-----") }
switch keyType {
case .publicKey: // Check whether we have found the first block.
if findPrivate { guard !self.segments.isEmpty || segment.contains("-----BEGIN PGP \(keyTypeStr.uppercased()) KEY BLOCK-----") else {
message = "ScanPrivateKey.".localize() // Check whether we are scanning the wrong key type.
} if segment.contains("-----BEGIN PGP \(theOtherKeyTypeStr.uppercased()) KEY BLOCK-----") {
hasStarted = findPublic self.message = "Scan\(keyTypeStr)Key.".localize()
case .privateKey:
if findPublic {
message = "ScanPrivateKey.".localize()
}
hasStarted = findPrivate
} }
} return (accept: false, message: self.message)
guard hasStarted == true else {
return
} }
// check the number of segments // Update the list of scanned segment and return.
numberOfSegments = numberOfSegments + 1 self.segments.append(segment)
guard numberOfSegments <= ScannedPGPKey.maxNumberOfGif else { if segment.contains("-----END PGP .* KEY BLOCK-----") {
key = "TooManyQrCodes".localize() self.message = "Done".localize()
return return (accept: true, message: self.message)
} else {
self.message = "ScannedQrCodes(%d)".localize(self.segments.count)
print(self.message)
return (accept: false, message: self.message)
} }
// update full text and check whether we are done
key.append(segment)
if key.contains("-----END PGP PUBLIC KEY BLOCK-----") || key.contains("-----END PGP PRIVATE KEY BLOCK-----") {
isDone = true
}
// update message
message = "ScannedQrCodes(%d)".localize(numberOfSegments)
} }
} }
var scanned = ScannedPGPKey() var scanned = ScannedPGPKey()
@ -158,21 +134,17 @@ class PGPKeyArmorSettingTableViewController: AutoCellHeightUITableViewController
// MARK: - QRScannerControllerDelegate Methods // MARK: - QRScannerControllerDelegate Methods
func checkScannedOutput(line: String) -> (accept: Bool, message: String) { func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
scanned.addSegment(segment: line) return scanned.addSegment(segment: line)
if scanned.isDone {
return (accept: true, message: "Done".localize())
} else {
return (accept: false, message: scanned.message)
}
} }
// MARK: - QRScannerControllerDelegate Methods // MARK: - QRScannerControllerDelegate Methods
func handleScannedOutput(line: String) { func handleScannedOutput(line: String) {
let key = scanned.segments.joined(separator: "")
switch scanned.keyType { switch scanned.keyType {
case .publicKey: case .publicKey:
armorPublicKeyTextView.text = scanned.key armorPublicKeyTextView.text = key
case .privateKey: case .privateKey:
armorPrivateKeyTextView.text = scanned.key armorPrivateKeyTextView.text = key
} }
} }

View file

@ -37,10 +37,13 @@ class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDeleg
} }
// Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter. // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter.
let captureDevice = AVCaptureDevice.default(for: AVMediaType.video) guard let captureDevice = AVCaptureDevice.default(for: .video) else {
scannerOutput.text = "CameraAccessDenied.".localize()
return
}
do { do {
// Get an instance of the AVCaptureDeviceInput class using the previous device object. // Get an instance of the AVCaptureDeviceInput class using the previous device object.
let input = try AVCaptureDeviceInput(device: captureDevice!) let input = try AVCaptureDeviceInput(device: captureDevice)
// Initialize the captureSession object. // Initialize the captureSession object.
captureSession = AVCaptureSession() captureSession = AVCaptureSession()

View file

@ -5,7 +5,7 @@
<key>ScannedQrCodes(%d)</key> <key>ScannedQrCodes(%d)</key>
<dict> <dict>
<key>NSStringLocalizedFormatKey</key> <key>NSStringLocalizedFormatKey</key>
<string>%#@code@</string> <string>%#@codes@</string>
<key>codes</key> <key>codes</key>
<dict> <dict>
<key>NSStringFormatSpecTypeKey</key> <key>NSStringFormatSpecTypeKey</key>

View file

@ -5,7 +5,7 @@
<key>ScannedQrCodes(%d)</key> <key>ScannedQrCodes(%d)</key>
<dict> <dict>
<key>NSStringLocalizedFormatKey</key> <key>NSStringLocalizedFormatKey</key>
<string>%#@code@</string> <string>%#@codes@</string>
<key>codes</key> <key>codes</key>
<dict> <dict>
<key>NSStringFormatSpecTypeKey</key> <key>NSStringFormatSpecTypeKey</key>