Skip to content

Commit

Permalink
fix: change CryptoSha test names, change PEM to DER
Browse files Browse the repository at this point in the history
Signed-off-by: Ricky Saechao <[email protected]>
  • Loading branch information
RickyLB committed Nov 1, 2023
1 parent 7e4d197 commit 9c614e4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
19 changes: 4 additions & 15 deletions Tests/HederaTests/CryptoAesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import CommonCrypto
import CryptoKit
import SwiftASN1
import XCTest

@testable import Hedera
Expand All @@ -28,33 +29,21 @@ internal final class CryptoAesTests: XCTestCase {
internal static var testPassphrase = "testpassphrase13d14"

internal func testAesDecryption() throws {
let s = """
-----BEGIN EC PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,0046A9EED8D16BE8BD6F0CAA6A197CE8
9VU9gReUmrn4XswjMx0F0A3oGzoHIksEXma72TCSdcxI7zHy0mtzuGq4Wd25O38s
H9c6kvhTPS1N/c6iNhc154B0HUoND8jvAvfxbGR/R87vpZUsOoKCmRxGqrxG8HER
FIHQ2jy16DrAbi95kDyLsiF1dy2vUY/HoqFZwxl/IVc=
-----END EC PRIVATE KEY-----
"""

let bytesDer = PrivateKey.toBytesDer(Resources.privateKey)
let iv = Data(hexEncoded: "0046A9EED8D16BE8BD6F0CAA6A197CE8")!

let doc = try Crypto.Pem.decode(s)

var hash = CryptoKit.Insecure.MD5()

hash.update(data: Self.testPassphrase.data(using: .utf8)!)
hash.update(data: iv[slicing: ..<8]!)

let password = Data(hash.finalize().bytes)

let decrypted = try Crypto.Aes.aes128CbcPadDecrypt(key: password, iv: iv, message: doc.der)
let decrypted = try Crypto.Aes.aes128CbcPadDecrypt(key: password, iv: iv, message: bytesDer())

XCTAssertEqual(
decrypted.hexStringEncoded(),
"0d94cebb6511da46b8003f432567e982eb7264045b34b6baaf0ce9f81bc1bfd7adf6f88d77f5043e5633591661e16da27a16d2023cf161510ab4cefc14c1ce78066aeaa9be0d43ad87ebeee9c06d8768fea43d91e3e061656b8f2cab42df89a2c682f6119ed56b31dcc1778d8d82b1932ab7768430b1de396dd3ca6d0756c5b6"
"d8ea1c72c322bc67ad533333a0b1a9e2215e34e466c913bed40c5a301a3f7fa9d376b475781c326b91e02599dc7a4412"
)
}
}
10 changes: 5 additions & 5 deletions Tests/HederaTests/CryptoSha2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import XCTest
@testable import Hedera

internal final class CryptoSha2Tests: XCTestCase {
internal func testSha256Encrypt() throws {
internal func testSha256Hash() throws {
let input = "testingSha256".data(using: .utf8)!

let sha = Crypto.Sha2.sha256(input)
Expand All @@ -32,7 +32,7 @@ internal final class CryptoSha2Tests: XCTestCase {
XCTAssertEqual(sha.count, 32)
}

internal func testSha384Encrypt() throws {
internal func testSha384Hash() throws {
let input = "testingSha384".data(using: .utf8)!

let sha = Crypto.Sha2.sha384(input)
Expand All @@ -43,7 +43,7 @@ internal final class CryptoSha2Tests: XCTestCase {
XCTAssertEqual(sha.count, 48)
}

internal func testSha256DigestEncrypt() throws {
internal func testSha256HashDigest() throws {
let input = "testingSha256digest".data(using: .utf8)!

let sha = Crypto.Sha2.digest(Crypto.Sha2.sha256, input)
Expand All @@ -54,7 +54,7 @@ internal final class CryptoSha2Tests: XCTestCase {
XCTAssertEqual(sha.count, 32)
}

internal func testSha384EncryptDigest() throws {
internal func testSha384HashDigest() throws {
let input = "testingSha384digest".data(using: .utf8)!

let sha = Crypto.Sha2.digest(Crypto.Sha2.sha384, input)
Expand All @@ -65,7 +65,7 @@ internal final class CryptoSha2Tests: XCTestCase {
XCTAssertEqual(sha.count, 48)
}

internal func testSha512EncryptDigest() throws {
internal func testSha512HashDigest() throws {
let input = "testingSha512digest".data(using: .utf8)!

let sha = Crypto.Sha2.digest(Crypto.Sha2.sha512, input)
Expand Down
4 changes: 2 additions & 2 deletions Tests/HederaTests/CryptoSha3Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import XCTest
@testable import Hedera

internal final class CryptoSha3Tests: XCTestCase {
internal func testKeccak256Encrypt() throws {
internal func testKeccak256Hash() throws {
let input = "testingKeccak256".data(using: .utf8)!

let sha = Crypto.Sha3.keccak256(input)
Expand All @@ -32,7 +32,7 @@ internal final class CryptoSha3Tests: XCTestCase {
XCTAssertEqual(sha.count, 32)
}

internal func testKeccak256EncryptDigest() throws {
internal func testKeccak256HashDigest() throws {
let input = "testingKeccak256Digest".data(using: .utf8)!

let sha = Crypto.Sha3.digest(Crypto.Sha3.keccak256, input)
Expand Down

0 comments on commit 9c614e4

Please sign in to comment.