Polish codes of QR code scanner
This commit is contained in:
parent
e4630e950d
commit
6f295be74d
5 changed files with 57 additions and 107 deletions
|
|
@ -17,54 +17,34 @@ class GitSSHKeyArmorSettingTableViewController: AutoCellHeightUITableViewControl
|
|||
let passwordStore = PasswordStore.shared
|
||||
|
||||
class ScannedSSHKey {
|
||||
static let maxNumberOfGif = 100
|
||||
var numberOfSegments = 0
|
||||
var previousSegment = ""
|
||||
var key = ""
|
||||
var segments = [String]()
|
||||
var message = ""
|
||||
var hasStarted = false
|
||||
var isDone = false
|
||||
|
||||
func reset() {
|
||||
numberOfSegments = 0
|
||||
previousSegment = ""
|
||||
key = ""
|
||||
self.segments.removeAll()
|
||||
message = "LookingForStartingFrame.".localize()
|
||||
hasStarted = false
|
||||
isDone = false
|
||||
}
|
||||
|
||||
func addSegment(segment: String) {
|
||||
// skip duplicated segments
|
||||
guard segment != previousSegment else {
|
||||
return
|
||||
}
|
||||
previousSegment = segment
|
||||
|
||||
// check whether we have found the first block
|
||||
if hasStarted == false {
|
||||
hasStarted = segment.contains("-----BEGIN")
|
||||
}
|
||||
guard hasStarted == true else {
|
||||
return
|
||||
func addSegment(segment: String) -> (accept: Bool, message: String) {
|
||||
// Skip duplicated segments.
|
||||
guard segment != self.segments.last else {
|
||||
return (accept: false, message: self.message)
|
||||
}
|
||||
|
||||
// check the number of segments
|
||||
numberOfSegments = numberOfSegments + 1
|
||||
guard numberOfSegments <= ScannedSSHKey.maxNumberOfGif else {
|
||||
key = "TooManyQrCodes".localize()
|
||||
return
|
||||
// Check whether we have found the first block.
|
||||
guard !self.segments.isEmpty || segment.contains("-----BEGIN") else {
|
||||
return (accept: false, message: self.message)
|
||||
}
|
||||
|
||||
// update full text and check whether we are done
|
||||
key.append(segment)
|
||||
if let index1 = key.range(of: "-----END")?.lowerBound,
|
||||
let _ = key.suffix(from: index1).range(of: "KEY-----")?.lowerBound {
|
||||
isDone = true
|
||||
// Update the list of scanned segment and return.
|
||||
self.segments.append(segment)
|
||||
if segment.range(of: "-----END.*KEY-----", options: .regularExpression, range: nil, locale: nil) != nil {
|
||||
self.message = "Done".localize()
|
||||
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()
|
||||
|
|
@ -109,17 +89,12 @@ class GitSSHKeyArmorSettingTableViewController: AutoCellHeightUITableViewControl
|
|||
|
||||
// MARK: - QRScannerControllerDelegate Methods
|
||||
func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
|
||||
scanned.addSegment(segment: line)
|
||||
if scanned.isDone {
|
||||
return (accept: true, message: "Done".localize())
|
||||
} else {
|
||||
return (accept: false, message: scanned.message)
|
||||
}
|
||||
return scanned.addSegment(segment: line)
|
||||
}
|
||||
|
||||
// MARK: - QRScannerControllerDelegate Methods
|
||||
func handleScannedOutput(line: String) {
|
||||
armorPrivateKeyTextView.text = scanned.key
|
||||
armorPrivateKeyTextView.text = scanned.segments.joined(separator: "")
|
||||
}
|
||||
|
||||
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
||||
|
|
|
|||
|
|
@ -19,71 +19,47 @@ class PGPKeyArmorSettingTableViewController: AutoCellHeightUITableViewController
|
|||
let keychain = AppKeychain.shared
|
||||
|
||||
class ScannedPGPKey {
|
||||
static let maxNumberOfGif = 100
|
||||
enum KeyType {
|
||||
case publicKey, privateKey
|
||||
}
|
||||
var keyType = KeyType.publicKey
|
||||
var numberOfSegments = 0
|
||||
var previousSegment = ""
|
||||
var key = ""
|
||||
var segments = [String]()
|
||||
var message = ""
|
||||
var hasStarted = false
|
||||
var isDone = false
|
||||
|
||||
func reset(keytype: KeyType) {
|
||||
self.keyType = keytype
|
||||
numberOfSegments = 0
|
||||
previousSegment = ""
|
||||
key = ""
|
||||
self.segments.removeAll()
|
||||
message = "LookingForStartingFrame.".localize()
|
||||
hasStarted = false
|
||||
isDone = false
|
||||
}
|
||||
|
||||
func addSegment(segment: String) {
|
||||
// skip duplicated segments
|
||||
guard segment != previousSegment else {
|
||||
return
|
||||
}
|
||||
previousSegment = segment
|
||||
func addSegment(segment: String) -> (accept: Bool, message: String) {
|
||||
let keyTypeStr = self.keyType == .publicKey ? "Public" : "Private"
|
||||
let theOtherKeyTypeStr = self.keyType == .publicKey ? "Private" : "Public"
|
||||
|
||||
// check whether we have found the first block
|
||||
if hasStarted == false {
|
||||
let findPublic = segment.contains("-----BEGIN PGP PUBLIC KEY BLOCK-----")
|
||||
let findPrivate = segment.contains("-----BEGIN PGP PRIVATE KEY BLOCK-----")
|
||||
switch keyType {
|
||||
case .publicKey:
|
||||
if findPrivate {
|
||||
message = "ScanPrivateKey.".localize()
|
||||
}
|
||||
hasStarted = findPublic
|
||||
case .privateKey:
|
||||
if findPublic {
|
||||
message = "ScanPrivateKey.".localize()
|
||||
}
|
||||
hasStarted = findPrivate
|
||||
}
|
||||
}
|
||||
guard hasStarted == true else {
|
||||
return
|
||||
// Skip duplicated segments.
|
||||
guard segment != self.segments.last else {
|
||||
return (accept: false, message: self.message)
|
||||
}
|
||||
|
||||
// check the number of segments
|
||||
numberOfSegments = numberOfSegments + 1
|
||||
guard numberOfSegments <= ScannedPGPKey.maxNumberOfGif else {
|
||||
key = "TooManyQrCodes".localize()
|
||||
return
|
||||
// Check whether we have found the first block.
|
||||
guard !self.segments.isEmpty || segment.contains("-----BEGIN PGP \(keyTypeStr.uppercased()) KEY BLOCK-----") else {
|
||||
// Check whether we are scanning the wrong key type.
|
||||
if segment.contains("-----BEGIN PGP \(theOtherKeyTypeStr.uppercased()) KEY BLOCK-----") {
|
||||
self.message = "Scan\(keyTypeStr)Key.".localize()
|
||||
}
|
||||
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 the list of scanned segment and return.
|
||||
self.segments.append(segment)
|
||||
if segment.contains("-----END PGP .* KEY BLOCK-----") {
|
||||
self.message = "Done".localize()
|
||||
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 message
|
||||
message = "ScannedQrCodes(%d)".localize(numberOfSegments)
|
||||
}
|
||||
}
|
||||
var scanned = ScannedPGPKey()
|
||||
|
|
@ -158,21 +134,17 @@ class PGPKeyArmorSettingTableViewController: AutoCellHeightUITableViewController
|
|||
|
||||
// MARK: - QRScannerControllerDelegate Methods
|
||||
func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
|
||||
scanned.addSegment(segment: line)
|
||||
if scanned.isDone {
|
||||
return (accept: true, message: "Done".localize())
|
||||
} else {
|
||||
return (accept: false, message: scanned.message)
|
||||
}
|
||||
return scanned.addSegment(segment: line)
|
||||
}
|
||||
|
||||
// MARK: - QRScannerControllerDelegate Methods
|
||||
func handleScannedOutput(line: String) {
|
||||
let key = scanned.segments.joined(separator: "")
|
||||
switch scanned.keyType {
|
||||
case .publicKey:
|
||||
armorPublicKeyTextView.text = scanned.key
|
||||
armorPublicKeyTextView.text = key
|
||||
case .privateKey:
|
||||
armorPrivateKeyTextView.text = scanned.key
|
||||
armorPrivateKeyTextView.text = key
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)
|
||||
guard let captureDevice = AVCaptureDevice.default(for: .video) else {
|
||||
scannerOutput.text = "CameraAccessDenied.".localize()
|
||||
return
|
||||
}
|
||||
do {
|
||||
// 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.
|
||||
captureSession = AVCaptureSession()
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
<key>ScannedQrCodes(%d)</key>
|
||||
<dict>
|
||||
<key>NSStringLocalizedFormatKey</key>
|
||||
<string>%#@code@</string>
|
||||
<string>%#@codes@</string>
|
||||
<key>codes</key>
|
||||
<dict>
|
||||
<key>NSStringFormatSpecTypeKey</key>
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
<key>ScannedQrCodes(%d)</key>
|
||||
<dict>
|
||||
<key>NSStringLocalizedFormatKey</key>
|
||||
<string>%#@code@</string>
|
||||
<string>%#@codes@</string>
|
||||
<key>codes</key>
|
||||
<dict>
|
||||
<key>NSStringFormatSpecTypeKey</key>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue