-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkeys.swift
66 lines (57 loc) · 2.05 KB
/
keys.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import BigNum
import Crypto
import Foundation
/// Wrapper for keys used by SRP
public struct SRPKey {
/// SRPKey internal storage
public let number: BigNum
/// padding
public let padding: Int
/// Representation as a byte array
public var bytes: [UInt8] { number.bytes.pad(to: padding) }
/// Representation as a byte array without padding
public var unpaddedBytes: [UInt8] { number.bytes }
/// Representation as a hex string
public var hex: String { number.bytes.pad(to: padding).hexdigest() }
/// Representation as a hex string without padding
public var unpaddedHex: String { number.bytes.hexdigest() }
/// Initialize with an array of bytes
@inlinable public init<C: Collection & ContiguousBytes>(_ bytes: C, padding: Int? = nil) {
number = BigNum(bytes: bytes)
self.padding = padding ?? bytes.count
}
/// Initialize with a crypto digest
@inlinable public init<D: Digest>(_ digest: D, padding: Int? = nil) {
number = BigNum(bytes: digest)
self.padding = padding ?? D.byteCount
}
/// Initialize with a hex string
@inlinable public init?(hex: String, padding: Int? = nil) {
guard let number = BigNum(hex: hex) else { return nil }
self.number = number
self.padding = padding ?? (hex.count + 1) / 2
}
/// Initialize with a BigNum
@usableFromInline init(_ number: BigNum, padding: Int = 0) {
self.number = number
self.padding = padding
}
/// Return SRPKey with padding
@inlinable public func with(padding: Int) -> SRPKey {
.init(number, padding: padding)
}
}
extension SRPKey: Equatable {}
/// Contains a private and a public key
public struct SRPKeyPair {
public let `public`: SRPKey
public let `private`: SRPKey
/// Initialise a SRPKeyPair object
/// - Parameters:
/// - public: The public key of the key pair
/// - private: The private key of the key pair
public init(public: SRPKey, private: SRPKey) {
self.private = `private`
self.public = `public`
}
}