智汀云盘-开发指南iOS(文件夹加密逻辑)

?
1.加密文件夹—加密过程
1.文件夹的创建可设置私人文件夹及加密过程:
智汀云盘-开发指南iOS(文件夹加密逻辑)
文章图片

2.访问服务器接口,创建私人文件夹

NetworkManager.shared.createFolder(name: name, pool_name: pool_name, partition_name: partition_name, is_encrypt: is_encrypt, pwd: pwd, confirm_pwd: confirmPwd, mode: mode, auth: members) { [weak self] _ in self?.showToast("保存成功".localizedString) self?.navigationController?.popViewController(animated: true)} failureCallback: { [weak self] code, err in self?.hideLoading() self?.showToast(err) }

?
3.创建给可访问用户后可在该用户的文件列表内展示。
智汀云盘-开发指南iOS(文件夹加密逻辑)
文章图片

2.加密文件夹—解密过程
1.文件列表数据Model中属性is_encrypt 为 1 时,则为加密文件或文件夹
2.加密文件夹样式的区分。
3.加密文件夹进入的逻辑如下
智汀云盘-开发指南iOS(文件夹加密逻辑)
文章图片

  • tableview点击相应代理方法,具体参考MyFileViewController.swift
if file.is_encrypt == 1 {//加密文件 //存储的密码对象 let key = AreaManager.shared.currentArea.scope_token + file.path let pwdJsonStr:String = UserDefaults.standard.value(forKey: key) as? String ?? "" let pwdModel = PasswordModel.deserialize(from: pwdJsonStr) if pwdModel != nil { //计算时间差 let timeTemp = TimeTool.TimeInterval(FromTime: pwdModel!.saveTime) if timeTemp > 72 {//大于72小时 //重新输入密码 pushToFolder(isNeedPwd: true, file: file) }else{ //无需输入密码 pushToFolder(isNeedPwd: false, file: file) } }else{ //需要重新输入密码 pushToFolder(isNeedPwd: true, file: file) } }else{ //无需输入密码 pushToFolder(isNeedPwd: false, file: file) }

private func pushToFolder(isNeedPwd:Bool,file:FileModel){ let vc = FolderViewController() vc.currentPath = file.path vc.currentPaths = ["文件",file.name] vc.encrytRootFile = file vc.isWriteRoot = (file.write == 1)let key = AreaManager.shared.currentArea.scope_token + file.path if file.is_encrypt == 1 { vc.rootPasswordKey = key }else{ vc.rootPasswordKey = "" } if isNeedPwd { //存储文件夹根目录Key self.tipsTestFieldAlert = TipsTestFieldAlertView.show(message: "请输入密码", sureCallback: { pwd in print("密码是\(pwd)") NetworkManager.shared.decryptFolder(name: file.path, password: pwd) {[weak self] response in guard let self = self else {return} //存储时间和密码 let pwdModel = PasswordModel() pwdModel.password = pwd //当前时间 let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" pwdModel.saveTime = dateFormatter.string(from: Date()) UserDefaults.standard.setValue(pwdModel.toJSONString(prettyPrint:true), forKey: key)//解密成功,进入文件夹 let nav = BaseNavigationViewController(rootViewController: vc) nav.modalPresentationStyle = .fullScreen nav.transitioningDelegate = self.transitionUtil self.navigationController?.present(nav, animated: true, completion: nil) } failureCallback: { code, err in self.showToast(err) } }) }else{ //无需输入密码 let nav = BaseNavigationViewController(rootViewController: vc) nav.modalPresentationStyle = .fullScreen nav.transitioningDelegate = transitionUtil self.navigationController?.present(nav, animated: true, completion: nil) } }

  • 加密文件夹进入子集目录需携带最初加密的文件,方便获取路径以及密码
  • 解密过程逻辑如下:(其中规则根据自身项目的需求来定,本项目以超出72小时则自动失效为准则,所以需存储首次解密的时间)

    //存储文件夹根目录Key self.tipsTestFieldAlert = TipsTestFieldAlertView.show(message: "请输入密码", sureCallback: { pwd in print("密码是\(pwd)") NetworkManager.shared.decryptFolder(name: file.path, password: pwd) {[weak self] response in guard let self = self else {return} //存储时间和密码 let pwdModel = PasswordModel() pwdModel.password = pwd //当前时间 let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" pwdModel.saveTime = dateFormatter.string(from: Date()) UserDefaults.standard.setValue(pwdModel.toJSONString(prettyPrint:true), forKey: key)//解密成功,进入文件夹 let nav = BaseNavigationViewController(rootViewController: vc) nav.modalPresentationStyle = .fullScreen nav.transitioningDelegate = self.transitionUtil self.navigationController?.present(nav, animated: true, completion: nil) } failureCallback: { code, err in self.showToast(err) } })

【智汀云盘-开发指南iOS(文件夹加密逻辑)】?

    推荐阅读