SPB Git

Repository Public

SensitiveDataPolicy.swift 5.2 KB 127 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP ID & Card Vault7// File             : SPVault/Core/Security/SensitiveDataPolicy.swift8// Module           : SPVault9// Repository       : https://git.spboucher.ai/sp-id-card-vault10// Project Website  : Not specified11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// Rules about what the vault refuses to keep. A bank card's security code, its PIN44/// and magnetic-stripe data are never stored, whatever the entry path (OCR,45/// editor, restore).46enum SensitiveDataPolicy {47    enum Violation: Error, Equatable {48        case forbiddenLabel(String)49        case magneticStripeData50    }5152    private static let forbiddenLabelTokens: Set<String> = [53        "cvv", "cvv2", "cvc", "cvc2", "cid", "csc", "cvn", "cav", "pin", "nip",54        "securitycode", "codedesecurite", "cryptogramme", "track1", "track2", "magstripe",55    ]5657    static func normalized(_ text: String) -> String {58        text.folding(options: [.diacriticInsensitive, .caseInsensitive], locale: nil)59            .filter { $0.isLetter || $0.isNumber }60    }6162    static func isForbiddenLabel(_ label: String) -> Bool {63        let whole = normalized(label)64        if forbiddenLabelTokens.contains(whole) { return true }65        let words = label.folding(options: [.diacriticInsensitive, .caseInsensitive], locale: nil)66            .split { !($0.isLetter || $0.isNumber) }.map(String.init)67        return words.contains { forbiddenLabelTokens.contains($0) }68    }6970    /// ISO/IEC 7813 track data: `%B<PAN>^NAME^YYMM…?` or `;<PAN>=YYMM…?`.71    static func looksLikeMagneticStripe(_ value: String) -> Bool {72        let v = value.trimmingCharacters(in: .whitespacesAndNewlines)73        if v.hasPrefix("%B"), v.contains("^") { return true }74        if v.hasPrefix(";"), v.contains("="), v.hasSuffix("?") { return true }75        return false76    }7778    static func validate(_ card: Card) throws {79        for field in card.fields {80            if isForbiddenLabel(field.label) { throw Violation.forbiddenLabel(field.label) }81            if looksLikeMagneticStripe(field.value) { throw Violation.magneticStripeData }82        }83        if looksLikeMagneticStripe(card.number) || looksLikeMagneticStripe(card.notes) {84            throw Violation.magneticStripeData85        }86    }8788    /// Drops forbidden fields instead of failing; used on OCR output and restores.89    static func sanitized(_ card: Card) -> Card {90        var copy = card91        copy.fields.removeAll { isForbiddenLabel($0.label) || looksLikeMagneticStripe($0.value) }92        if looksLikeMagneticStripe(copy.number) { copy.number = "" }93        return copy94    }95}9697enum PaymentCard {98    /// Luhn check (ISO/IEC 7812) on a digit string of 12 to 19 digits.99    static func isValidNumber(_ text: String) -> Bool {100        let digits = text.compactMap(\.wholeNumberValue)101        guard (12...19).contains(digits.count), digits.count == text.filter({ !$0.isWhitespace && $0 != "-" }).count102        else { return false }103        var sum = 0104        for (offset, digit) in digits.reversed().enumerated() {105            if offset % 2 == 1 {106                let doubled = digit * 2107                sum += doubled > 9 ? doubled - 9 : doubled108            } else {109                sum += digit110            }111        }112        return sum % 10 == 0113    }114115    static func network(for number: String) -> String? {116        let d = number.filter(\.isNumber)117        guard d.count >= 4, let two = Int(d.prefix(2)), let four = Int(d.prefix(4)) else { return nil }118        if d.hasPrefix("4") { return "Visa" }119        if (51...55).contains(two) || (2221...2720).contains(four) { return "Mastercard" }120        if two == 34 || two == 37 { return "American Express" }121        if four == 6011 || two == 65 { return "Discover" }122        if (3528...3589).contains(four) { return "JCB" }123        if two == 62 { return "UnionPay" }124        return nil125    }126}127
Files main
Menu

Theme