Skip to content

Commit

Permalink
Update MRZ hash checking
Browse files Browse the repository at this point in the history
  • Loading branch information
romanmazeev committed Aug 30, 2021
1 parent 12fe67e commit d3f92ce
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
22 changes: 15 additions & 7 deletions Sources/MRZParser/Private/MRZCode/MRZCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation

struct MRZCode {
let format: MRZFormat
var documentTypeField: Field
var countryCodeField: Field
var documentNumberField: ValidatedField<String>
Expand All @@ -22,15 +23,22 @@ struct MRZCode {

var isValid: Bool {
if !finalCheckDigit.isEmpty {
var fieldsValidate: [ValidatedFieldProtocol] = [
documentNumberField,
birthdateField,
expiryDateField,
optionalDataField
]
var fieldsValidate: [ValidatedFieldProtocol] = [ documentNumberField ]

if let optionalData2Field = optionalData2Field {
if format == .td1, let optionalData2Field = optionalData2Field {
fieldsValidate.append(optionalDataField)
fieldsValidate.append(contentsOf: [
birthdateField,
expiryDateField
])
fieldsValidate.append(optionalData2Field)
} else {
fieldsValidate.append(contentsOf: [
birthdateField,
expiryDateField
])

fieldsValidate.append(optionalDataField)
}

let compositedValue = fieldsValidate.reduce("", { $0 + $1.rawValue + $1.checkDigit })
Expand Down
1 change: 1 addition & 0 deletions Sources/MRZParser/Private/MRZCode/MRZCodeFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ struct MRZCodeFactory {
}

return MRZCode(
format: format,
documentTypeField: formatter.createField(from: firstLine, at: 0, length: 2, fieldType: .documentType),
countryCodeField: formatter.createField(from: firstLine, at: 2, length: 3, fieldType: .countryCode),
documentNumberField: documentNumberField,
Expand Down
3 changes: 3 additions & 0 deletions Sources/MRZParser/Public/MRZResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public struct MRZResult: Hashable {
case visa
case passport
case id
case residencePermit
case undefined

var identifiers: [String] {
Expand All @@ -46,6 +47,8 @@ public struct MRZResult: Hashable {
return ["P", "PN"]
case .id:
return ["I"]
case .residencePermit:
return ["IR"]
case .undefined:
return []
}
Expand Down

0 comments on commit d3f92ce

Please sign in to comment.