From cd9d70ff6dd967bd2be1789e3e0e60e37afdd40f Mon Sep 17 00:00:00 2001 From: Felipe Sueto Date: Fri, 5 Nov 2021 18:02:55 -0300 Subject: [PATCH] Change name of parameters --- Sources/starkbank-ecdsa/Ecdsa.swift | 2 +- Sources/starkbank-ecdsa/PrivateKey.swift | 14 +++--- Sources/starkbank-ecdsa/PublicKey.swift | 14 +++--- Sources/starkbank-ecdsa/Signature.swift | 6 +-- Sources/starkbank-ecdsa/Utils/Binary.swift | 26 +++++------ Sources/starkbank-ecdsa/Utils/Der.swift | 44 +++++++++---------- Sources/starkbank-ecdsa/Utils/Integer.swift | 12 ++--- Sources/starkbank-ecdsa/Utils/Oid.swift | 4 +- .../PrivateKeyTests.swift | 2 +- .../starkbank-ecdsaTests/PublicKeyTests.swift | 2 +- 10 files changed, 63 insertions(+), 63 deletions(-) diff --git a/Sources/starkbank-ecdsa/Ecdsa.swift b/Sources/starkbank-ecdsa/Ecdsa.swift index b0e2fea..3776e79 100644 --- a/Sources/starkbank-ecdsa/Ecdsa.swift +++ b/Sources/starkbank-ecdsa/Ecdsa.swift @@ -15,7 +15,7 @@ public class Ecdsa { let hashMessage = hashfunc.digest(message) let numberMessage = BinaryAscii.intFromHex(BinaryAscii.hexFromData(hashMessage as Data)) let curve = privateKey.curve - let randNum = try RandomInteger.between(minimum: BigInt(1), maximum: curve.N) + let randNum = try RandomInteger.between(min: BigInt(1), max: curve.N) let randomSignPoint = Math.multiply(curve.G, randNum, curve.N, curve.A, curve.P) let r = randomSignPoint.x.modulus(curve.N) let s = ((numberMessage + r * privateKey.secret) * (Math.inv(randNum, curve.N))).modulus(curve.N) diff --git a/Sources/starkbank-ecdsa/PrivateKey.swift b/Sources/starkbank-ecdsa/PrivateKey.swift index 2c268dc..9619037 100644 --- a/Sources/starkbank-ecdsa/PrivateKey.swift +++ b/Sources/starkbank-ecdsa/PrivateKey.swift @@ -17,7 +17,7 @@ public class PrivateKey { public init(curve: CurveFp = secp256k1, secret: BigInt? = nil) throws { self.curve = curve if (secret == nil) { - self.secret = try RandomInteger.between(minimum: BigInt(1), maximum: curve.N - BigInt(1)) + self.secret = try RandomInteger.between(min: BigInt(1), max: curve.N - BigInt(1)) return } self.secret = secret! @@ -48,7 +48,7 @@ public class PrivateKey { public func toDer() -> Data { let publicKeyString = publicKey().toString(encoded: true) let hexadecimal = Der.encodeConstructed( - Der.encodeInteger(number: 1), + Der.encodeInteger(1), Der.encodeOctetString(BinaryAscii.hexFromInt(self.secret)), Der.encodeOidContainer( Der.encodeObject(self.curve.oid)), @@ -59,10 +59,10 @@ public class PrivateKey { return BinaryAscii.dataFromHex(hexadecimal) } - public static func fromDer(_ data: Data) throws -> PrivateKey { - var hexadecimal = BinaryAscii.hexFromData(data) + public static func fromDer(_ string: Data) throws -> PrivateKey { + var hexadecimal = BinaryAscii.hexFromData(string) - let parsed = try Der.parse(hexadecimal: &hexadecimal)[0] as! [Any] + let parsed = try Der.parse(&hexadecimal)[0] as! [Any] let privateKeyFlag = parsed[0] as! BigInt let secretHex = parsed[1] as! String let curveData = (parsed[2] as! [Any])[0] as! [Int] @@ -73,7 +73,7 @@ public class PrivateKey { } let curve = try getCurveByOid(curveData) - let privateKey = try fromString(secretHex, curve) + let privateKey = try fromString(string: secretHex, curve: curve) if (privateKey.publicKey().toString(encoded: true) != publicKeyString) { throw Error.matchError("The public key described inside the private key file doesn't match the actual public key of the pair") @@ -85,7 +85,7 @@ public class PrivateKey { return BinaryAscii.hexFromInt(self.secret) } - public static func fromString(_ string: String, _ curve: CurveFp = secp256k1) throws -> PrivateKey { + public static func fromString(string: String, curve: CurveFp = secp256k1) throws -> PrivateKey { return try PrivateKey(curve: curve, secret: BinaryAscii.intFromHex(string)) } } diff --git a/Sources/starkbank-ecdsa/PublicKey.swift b/Sources/starkbank-ecdsa/PublicKey.swift index 9b27073..71dad47 100644 --- a/Sources/starkbank-ecdsa/PublicKey.swift +++ b/Sources/starkbank-ecdsa/PublicKey.swift @@ -23,8 +23,8 @@ public class PublicKey { return createPem(content: BinaryAscii.base64FromData(der), template: publicKeyPemTemplate) } - public static func fromPem(_ pem: String) throws -> PublicKey { - let publicKeyPem = try getPemContent(pem: pem, template: publicKeyPemTemplate) + public static func fromPem(_ string: String) throws -> PublicKey { + let publicKeyPem = try getPemContent(pem: string, template: publicKeyPemTemplate) return try fromDer(BinaryAscii.dataFromBase64(publicKeyPem)) } @@ -39,10 +39,10 @@ public class PublicKey { return BinaryAscii.dataFromHex(hexadecimal) } - public static func fromDer(_ data: Data) throws -> PublicKey{ - var hexadecimal = BinaryAscii.hexFromData(data) + public static func fromDer(_ string: Data) throws -> PublicKey{ + var hexadecimal = BinaryAscii.hexFromData(string) - let parsed = try Der.parse(hexadecimal: &hexadecimal)[0] as! [Any] + let parsed = try Der.parse(&hexadecimal)[0] as! [Any] let publicKeyOid = (parsed[0] as! [Any])[0] as! [Int] let curveOid = (parsed[0] as! [Any])[1] as! [Int] var pointString = parsed[1] as! String @@ -52,7 +52,7 @@ public class PublicKey { .replacingOccurrences(of: "{actualOid}", with: publicKeyOid.description)) } let curve = try getCurveByOid(curveOid) - return try fromString(&pointString, curve) + return try fromString(string: &pointString, curve: curve) } public func toString(encoded: Bool = false) -> String { @@ -66,7 +66,7 @@ public class PublicKey { return string } - public static func fromString(_ string: inout String, _ curve: CurveFp = secp256k1, validatePoint: Bool = true) throws -> PublicKey { + public static func fromString(string: inout String, curve: CurveFp = secp256k1, validatePoint: Bool = true) throws -> PublicKey { let baseLength = 2 * curve.length() if (string.count > 2 * baseLength && String(string.prefix(4)) == "0004") { string = String(string.suffix(string.count - 4)) diff --git a/Sources/starkbank-ecdsa/Signature.swift b/Sources/starkbank-ecdsa/Signature.swift index 8850a2a..6cf111d 100644 --- a/Sources/starkbank-ecdsa/Signature.swift +++ b/Sources/starkbank-ecdsa/Signature.swift @@ -41,13 +41,13 @@ public class Signature { public func toString() -> String { return Der.encodeConstructed( - Der.encodeInteger(number: self.r), - Der.encodeInteger(number: self.s) + Der.encodeInteger(self.r), + Der.encodeInteger(self.s) ) } public static func fromString(string: inout String) throws -> Signature { - let parsed = try Der.parse(hexadecimal: &string)[0] as! [Any] + let parsed = try Der.parse(&string)[0] as! [Any] let r = parsed[0] as! BigInt let s = parsed[1] as! BigInt return Signature(r, s) diff --git a/Sources/starkbank-ecdsa/Utils/Binary.swift b/Sources/starkbank-ecdsa/Utils/Binary.swift index 0771ed8..691115f 100644 --- a/Sources/starkbank-ecdsa/Utils/Binary.swift +++ b/Sources/starkbank-ecdsa/Utils/Binary.swift @@ -11,25 +11,25 @@ import Foundation class BinaryAscii { - static func intFromHex(_ hex: String) -> BigInt { - return BigInt(hex, radix: 16)! + static func intFromHex(_ hexadecimal: String) -> BigInt { + return BigInt(hexadecimal, radix: 16)! } - static func hexFromInt(_ int: BigInt) -> String { - var hexadecimal = String(int, radix: 16) + static func hexFromInt(_ number: BigInt) -> String { + var hexadecimal = String(number, radix: 16) if (hexadecimal.count % 2 == 1) { hexadecimal = "0" + hexadecimal } return hexadecimal } - static func bitsFromHex(_ hex: String) -> String { - let binary = String(BigInt(hex, radix: 16)!, radix: 2) - return StringHelper.zfill(binary, hex.count * 4) + static func bitsFromHex(_ hexadecimal: String) -> String { + let binary = String(BigInt(hexadecimal, radix: 16)!, radix: 2) + return StringHelper.zfill(binary, hexadecimal.count * 4) } - static func dataFromBase64(_ base64: String) -> Data { - return Data(base64Encoded: base64)! + static func dataFromBase64(_ base64String: String) -> Data { + return Data(base64Encoded: base64String)! } static func base64FromData(_ string: Data) -> String { @@ -41,12 +41,12 @@ class BinaryAscii { return data.map{ String(format:"%02x", $0) }.joined() } - static func dataFromHex(_ hex: String) -> Data { - var data = Data(capacity: hex.count / 2) + static func dataFromHex(_ hexadecimal: String) -> Data { + var data = Data(capacity: hexadecimal.count / 2) let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive) - regex.enumerateMatches(in: hex, range: NSRange(hex.startIndex..., in: hex)) { match, _, _ in - let byteString = (hex as NSString).substring(with: match!.range) + regex.enumerateMatches(in: hexadecimal, range: NSRange(hexadecimal.startIndex..., in: hexadecimal)) { match, _, _ in + let byteString = (hexadecimal as NSString).substring(with: match!.range) let num = UInt8(byteString, radix: 16)! data.append(num) } diff --git a/Sources/starkbank-ecdsa/Utils/Der.swift b/Sources/starkbank-ecdsa/Utils/Der.swift index 3dd126d..b4df07d 100644 --- a/Sources/starkbank-ecdsa/Utils/Der.swift +++ b/Sources/starkbank-ecdsa/Utils/Der.swift @@ -50,11 +50,11 @@ let hexTagtoType = [ public class Der { - public static func encodeConstructed(_ components: String...) -> String { - return encodeSequence(components.joined(separator: "")) + public static func encodeConstructed(_ encodedValues: String...) -> String { + return encodeSequence(encodedValues.joined(separator: "")) } - internal static func encodeInteger(number: BigInt) -> String { + internal static func encodeInteger(_ number: BigInt) -> String { var hexadecimal = BinaryAscii.hexFromInt(abs(number)) if (number < 0) { let bitCount = 4 * hexadecimal.count @@ -69,7 +69,7 @@ public class Der { } internal static func encodeObject(_ oid: Array) -> String { - let hexadecimal = Oid.oidToHex(oid: oid) + let hexadecimal = Oid.oidToHex(oid) return addPrefix(typeToHexTag.object.rawValue, hexadecimal) } @@ -97,7 +97,7 @@ public class Der { return String(format: "%@%@%@", tag, generateLengthBytes(hexadecimal: data), data) } - static func parse(hexadecimal: inout String) throws -> Array { + static func parse(_ hexadecimal: inout String) throws -> Array { if (hexadecimal == "") { return [] } @@ -124,48 +124,48 @@ public class Der { } var contentArray: Array = [content] - let tagData = getTagData(tag: typeByte) + let tagData = getTagData(typeByte) if (tagData["isConstructed"] as! Bool) { - contentArray = try parse(hexadecimal: &content) - return [contentArray] + (try parse(hexadecimal: &hexadecimal)) + contentArray = try parse(&content) + return [contentArray] + (try parse(&hexadecimal)) } switch tagData["type"]! as! String { case null: - contentArray = [parseNull(hexadecimal: content)] + contentArray = [parseNull(content)] case object: - contentArray = [parseOid(hexadecimal: content)] + contentArray = [parseOid(content)] case utcTime: - contentArray = [parseTime(hexadecimal: content)] + contentArray = [parseTime(content)] case integer: - contentArray = [parseInteger(hexadecimal: content)] + contentArray = [parseInteger(content)] case printableString: - contentArray = [parseString(hexadecimal: content)] + contentArray = [parseString(content)] default: break } - return contentArray + (try parse(hexadecimal: &hexadecimal)) + return contentArray + (try parse(&hexadecimal)) } - private static func parseOid(hexadecimal: String) -> [Int] { - return Oid.oidFromHex(hexadecimal: hexadecimal) + private static func parseOid(_ hexadecimal: String) -> [Int] { + return Oid.oidFromHex(hexadecimal) } - private static func parseTime(hexadecimal: String) -> Date { + private static func parseTime(_ hexadecimal: String) -> Date { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyMMddHHmmssZ" - return dateFormatter.date(from: parseString(hexadecimal: hexadecimal)) ?? Date() + return dateFormatter.date(from: parseString(hexadecimal)) ?? Date() } - private static func parseString(hexadecimal: String) -> String { + private static func parseString(_ hexadecimal: String) -> String { return String(data: BinaryAscii.dataFromHex(hexadecimal), encoding: .utf8)! } - private static func parseNull(hexadecimal: String) -> String { + private static func parseNull(_ content: String) -> String { return "" } - private static func parseInteger(hexadecimal: String) -> BigInt { + private static func parseInteger(_ hexadecimal: String) -> BigInt { let integer = BinaryAscii.intFromHex(hexadecimal) let bits = BinaryAscii.bitsFromHex(String(hexadecimal.prefix(1))) if (String(bits.prefix(1)) == "0") { @@ -207,7 +207,7 @@ public class Der { return BinaryAscii.hexFromInt(lengthLength) + length } - private static func getTagData(tag: String) -> Dictionary { + private static func getTagData(_ tag: String) -> Dictionary { let bits = BinaryAscii.bitsFromHex(tag) let bit8 = bits[bits.index(bits.startIndex, offsetBy: 0)] let bit7 = bits[bits.index(bits.startIndex, offsetBy: 1)] diff --git a/Sources/starkbank-ecdsa/Utils/Integer.swift b/Sources/starkbank-ecdsa/Utils/Integer.swift index 0965817..1fc95df 100644 --- a/Sources/starkbank-ecdsa/Utils/Integer.swift +++ b/Sources/starkbank-ecdsa/Utils/Integer.swift @@ -16,8 +16,8 @@ class RandomInteger { - Parameter maximum: maximum value of the integer - Returns: integer x in the range: minimum <= x <= maximum */ - public static func between(minimum: BigInt, maximum: BigInt) throws -> BigInt { - let result = getBytesNeeded(maximum - minimum) + public static func between(min: BigInt, max: BigInt) throws -> BigInt { + let result = getBytesNeeded(max - min) let bytesNeeded = result.0 let mask = result.1 @@ -43,13 +43,13 @@ class RandomInteger { // Taking the randomValue module would increase concentration in a specific region of the range and break the uniform distribution, raising security concerns. // To avoid this, we retry the method until the value satisfies the range. - if (minimum + randomValue > maximum) { - return try between(minimum: minimum, maximum: maximum) + if (min + randomValue > max) { + return try between(min: min, max: max) } - return minimum + randomValue + return min + randomValue } - public static func getBytesNeeded(_ request: BigInt) -> (Int, BigInt) { + internal static func getBytesNeeded(_ request: BigInt) -> (Int, BigInt) { var range = request var bitsNeeded = 0 var bytesNeeded = 0 diff --git a/Sources/starkbank-ecdsa/Utils/Oid.swift b/Sources/starkbank-ecdsa/Utils/Oid.swift index 8f9afd9..9504d6c 100644 --- a/Sources/starkbank-ecdsa/Utils/Oid.swift +++ b/Sources/starkbank-ecdsa/Utils/Oid.swift @@ -11,7 +11,7 @@ import BigInt public class Oid { - public static func oidFromHex(hexadecimal: String) -> [Int] { + public static func oidFromHex(_ hexadecimal: String) -> [Int] { let firstByte = String(hexadecimal.prefix(2)) var remainingBytes = String(hexadecimal.suffix(hexadecimal.count - 2)) let firstByteInt = BinaryAscii.intFromHex(firstByte) @@ -32,7 +32,7 @@ public class Oid { return oid } - public static func oidToHex(oid: [Int]) -> String { + public static func oidToHex(_ oid: [Int]) -> String { var hexadecimal = BinaryAscii.hexFromInt(BigInt(40 * oid[0]) + BigInt(oid[1])) var byteArray = [Int]() for var oidInt in oid[2..