-
Notifications
You must be signed in to change notification settings - Fork 11
Feature/issue 314 costructing wkd urls #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // | ||
| // WKDURLsConstructor.swift | ||
| // FlowCrypt | ||
| // | ||
| // Created by Yevhen Kyivskyi on 15.05.2021. | ||
| // Copyright © 2021 FlowCrypt Limited. All rights reserved. | ||
| // | ||
|
|
||
| import CryptoKit | ||
| import Foundation | ||
|
|
||
| /// WKD - Web Key Directory, follow this link for more information https://wiki.gnupg.org/WKD | ||
|
|
||
| enum WKDURLMode { | ||
| case direct | ||
| case advanced | ||
| } | ||
|
|
||
| protocol WKDURLsConstructorType { | ||
| func construct(from email: String, mode: WKDURLMode) -> String? | ||
| } | ||
|
|
||
| class WKDURLsConstructor: WKDURLsConstructorType { | ||
|
|
||
| func construct(from email: String, mode: WKDURLMode) -> String? { | ||
| let parts = email.split(separator: "@") | ||
| if parts.count != 2 { | ||
| return nil | ||
| } | ||
| let user = String(parts[0]) | ||
| let recipientDomain = String(parts[1]).lowercased() | ||
| let hu = String(decoding: user.lowercased().data().SHA1.zBase32EncodedBytes(), as: Unicode.UTF8.self) | ||
| let directURL = "https://\(recipientDomain)/.well-known/openpgpkey/hu/\(hu)?l=\(user)" | ||
| let advancedURL = "https://openpgpkey.\(recipientDomain)/.well-known/openpgpkey/\(recipientDomain)/hu/\(hu)?l=\(user)" | ||
|
|
||
| return mode == .advanced ? advancedURL : directURL | ||
| } | ||
| } |
158 changes: 158 additions & 0 deletions
158
FlowCryptCommon/Extensions/Data/DataExntensions+ZBase32Encoding.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // | ||
| // DataExntensions+Encoding.swift | ||
| // FlowCryptCommon | ||
| // | ||
| // Created by Yevhen Kyivskyi on 17.05.2021. | ||
| // Copyright © 2021 FlowCrypt Limited. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| public extension Data { | ||
|
|
||
| private enum Constants { | ||
| static let alphabet = "ybndrfg8ejkmcpqxot1uwisza345h769".unicodeScalars.map { UInt8(ascii: $0) } | ||
| static let bitsInZBase32Character = 5 | ||
| static let bitsInByte = 8 | ||
| } | ||
|
|
||
| func zBase32EncodedBytes() -> [UInt8] { | ||
|
|
||
| let bytes = [UInt8](self) | ||
| let capacity = (bytes.count / Constants.bitsInZBase32Character) * Constants.bitsInZBase32Character | ||
|
|
||
| var encoded: [UInt8] = [] | ||
|
|
||
| encoded.reserveCapacity(capacity) | ||
|
|
||
| var input = bytes.makeIterator() | ||
| while let firstByte = input.next() { | ||
| let secondByte = input.next() | ||
| let thirdByte = input.next() | ||
| let fourthByte = input.next() | ||
| let fifthByte = input.next() | ||
|
|
||
| let firstChar = encode(firstByte: firstByte) | ||
| let secondChar = encode(firstByte: firstByte, secondByte: secondByte) | ||
| let thirdChar = encode(secondByte: secondByte) | ||
| let fourthChar = encode(secondByte: secondByte, thirdByte: thirdByte) | ||
| let fifthChar = encode(thirdByte: thirdByte, fourthByte: fourthByte) | ||
| let sixthChar = encode(fourthByte: fourthByte) | ||
| let seventhChar = encode(fourthByte: fourthByte, fifthByte: fifthByte) | ||
| let eightChar = encode(fifthByte: fifthByte) | ||
|
|
||
| encoded.append(firstChar) | ||
| encoded.append(secondChar) | ||
| if let thirdChar = thirdChar { | ||
| encoded.append(thirdChar) | ||
| } | ||
| if let fourthChar = fourthChar { | ||
| encoded.append(fourthChar) | ||
| } | ||
| if let fifthChar = fifthChar { | ||
| encoded.append(fifthChar) | ||
| } | ||
| if let sixthChar = sixthChar { | ||
| encoded.append(sixthChar) | ||
| } | ||
| if let seventhChar = seventhChar { | ||
| encoded.append(seventhChar) | ||
| } | ||
| if let eightChar = eightChar { | ||
| encoded.append(eightChar) | ||
| } | ||
| } | ||
|
|
||
| return encoded | ||
| } | ||
|
|
||
| private func encode(firstByte: UInt8) -> UInt8 { | ||
| // First: 00000 --- | ||
| let index = firstByte >> 3 | ||
| return Constants.alphabet[Int(index)] | ||
| } | ||
|
|
||
| private func encode(firstByte: UInt8, secondByte: UInt8?) -> UInt8 { | ||
| // First: -----000 | ||
| var index = (firstByte & 0b00000111) << 2 | ||
|
|
||
| if let secondByte = secondByte { | ||
| // Second: 00 ------ | ||
| index |= (secondByte & 0b11000000) >> 6 | ||
| } | ||
|
|
||
| return Constants.alphabet[Int(index)] | ||
| } | ||
|
|
||
| private func encode(secondByte: UInt8?) -> UInt8? { | ||
| guard let secondByte = secondByte else { | ||
| return nil | ||
| } | ||
| // Second: --00000- | ||
| let index = (secondByte & 0b00111110) >> 1 | ||
| return Constants.alphabet[Int(index)] | ||
| } | ||
|
|
||
| private func encode(secondByte: UInt8?, thirdByte: UInt8?) -> UInt8? { | ||
| guard let secondByte = secondByte else { | ||
| return nil | ||
| } | ||
| // Second: -------0 | ||
| var index = (secondByte & 0b00000001) << 4 | ||
|
|
||
| if let thirdByte = thirdByte { | ||
| // Third: 0000---- | ||
| index |= (thirdByte & 0b11110000) >> 4 | ||
| } | ||
|
|
||
| return Constants.alphabet[Int(index)] | ||
| } | ||
|
|
||
| private func encode(thirdByte: UInt8?, fourthByte: UInt8?) -> UInt8? { | ||
| guard let thirdByte = thirdByte else { | ||
| return nil | ||
| } | ||
| // Third:----0000 | ||
| var index = (thirdByte & 0b00001111) << 1 | ||
|
|
||
| if let fourthByte = fourthByte { | ||
| // Fourth: 0------- | ||
| index |= (fourthByte & 0b10000000) >> 7 | ||
| } | ||
|
|
||
| return Constants.alphabet[Int(index)] | ||
| } | ||
|
|
||
| private func encode(fourthByte: UInt8?) -> UInt8? { | ||
| guard let fourthByte = fourthByte else { | ||
| return nil | ||
| } | ||
| // Fourth: -00000-- | ||
| let index = (fourthByte & 0b01111100) >> 2 | ||
| return Constants.alphabet[Int(index)] | ||
| } | ||
|
|
||
| private func encode(fourthByte: UInt8?, fifthByte: UInt8?) -> UInt8? { | ||
| guard let fourthByte = fourthByte else { | ||
| return nil | ||
| } | ||
| // Fourth: ------00 | ||
| var index = (fourthByte & 0b00000011) << 3 | ||
|
|
||
| if let fifthByte = fifthByte { | ||
| // Fifth: 000----- | ||
| index |= (fifthByte & 0b11100000) >> 5 | ||
| } | ||
|
|
||
| return Constants.alphabet[Int(index)] | ||
| } | ||
|
|
||
| private func encode(fifthByte: UInt8?) -> UInt8? { | ||
| guard let fifthByte = fifthByte else { | ||
| return nil | ||
| } | ||
| // // Fifth: ---00000 | ||
| let index = fifthByte & 0b00011111 | ||
| return Constants.alphabet[Int(index)] | ||
| } | ||
| } | ||
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.