Files
Euler/Tests/EulerTests/Cryptography.swift
Arthur Guiot 97140521a3 Improved roots reliablity and performance
Improved overall performance by simplifying when possible (decrease perf on small numbers but makes eerything a bit smoother)
2020-05-25 11:32:19 -04:00

35 lines
2.1 KiB
Swift

//
// Cryptogrpahy.swift
//
//
// Created by Arthur Guiot on 2020-01-08.
//
import Foundation
import XCTest
import Euler
class CryptographyTests: XCTestCase {
func testSha256() {
XCTAssertEqual("Euler".sha256, "586ca27cfbb81dde884d5f0bbb195ff4b2ff3b2f4304530e5e24194f3a391eda")
}
func testMd5() {
XCTAssertEqual("Euler".md5, "0a7532036415f2491bf5f952220827b8")
}
func testRSA() {
var p = RSA.generatePrime(128)
XCTAssert(p.bitWidth >= 120) // Sometimes, less than 128 because leading random numbers are zeros. 8 consecutive zeros is highly unprobable
p = BigInt("133081876506421923962564199110125448453704937284249367915614783184430716172428728198095674708718741991443516991416111660167888335861107680074358055605571417392208406194264346635072293912609713085260354070700055888678514690878149253177960273775659537560220378850112471985434373425534121373466492101182463962031")!
let q = BigInt("170729544226571454895473088123333689250079490545012049838639583558971720931737831010822659694399955378425256465062476627613315758673350478461613830570116841015780784336308507083874651158029602582993233111593356512531869546706885170044355115669728424124141763799008880327106952436883614887277350838425336156327")!
XCTAssert(RSA.genKeys(p: p, q: q).pri.exponent == BigInt("1396466434386901475973635048077683799260450090398970338320236629190555899627771977822086142456362972689566985925179681282432115598451765899180050962461295573831370692379342918841065848209981469650855314331951066867454742222226209868586965916983653246883515441255452115210364245315889536341764067661170454278457697437495445789456921660619938185093118762690200980720312508614337759620606992462563490422766695595565689175332684791909489595603975795727615298528912462835396045456912448999969287715867664304211866261387586350401612983709922304068751268453269452710980742873307409704484365002175294665608486688146261327793"))
}
static var allTests = [
("SHA256", testSha256),
("MD5", testMd5),
("RSA", testRSA)
]
}