简体   繁体   English

Ubuntu 上的 swift CryptoKit

[英]swift CryptoKit on Ubuntu

I am trying to compile a small swift program, " main.swift " to an executable on Ubuntu 18.08.我正在尝试将一个小型 swift 程序“ main.swift ”编译为 Ubuntu 18.08 上的可执行文件。 I use the Swift Package Manager to manage my dependencies.我使用Swift Package 管理器来管理我的依赖项。 In this very simple case I only have one dependency, namely this open-source CryptoKit .在这个非常简单的情况下,我只有一个依赖项,即这个开源 CryptoKit I have one swift file which just tries to import CryptoKit.我有一个 swift 文件,它只是试图导入 CryptoKit。

import Foundation
import CryptoKit
print("phew")

My Package.swift file looks like this:我的Package.swift文件如下所示:

// swift-tools-version:5.2

import PackageDescription

let package = Package(
    name: "decryp",
    dependencies: [
      .package(url: "https://github.com/apple/swift-crypto.git", .upToNextMajor(from: "1.0.1"))
    ],
    targets: [
        .target(
            name: "decryp",
            dependencies: ["swift-crypto"]
        ),
        .testTarget(
            name: "decrypTests",
            dependencies: ["decryp"]),
    ]
)

When I try to build the executable with swift build it fetches the repository, but then gives an error with a product not found.当我尝试使用swift build构建可执行文件时,它会获取存储库,但随后会给出未找到产品的错误。 stdout from swift build:来自 swift 构建的标准输出:

Fetching https://github.com/apple/swift-crypto.git
Cloning https://github.com/apple/swift-crypto.git
Resolving https://github.com/apple/swift-crypto.git at 1.0.2
'decryp' /home/kah/decryp: error: product 'swift-crypto' not found. It is required by target 'decryp'.
warning: dependency 'swift-crypto' is not used by any target

Maybe I am missing something obvious?也许我错过了一些明显的东西? I am still a beginner in the swift world.我仍然是 swift 世界的初学者。

Looking atswift-crypto Package.swift gave a clue.查看swift-crypto Package.swift给出了一个线索。 The swift-crypto library name is "Crypto", so I tried with that instead which gave an error: swift-crypto 库名称是“Crypto”,所以我尝试使用它,但它给出了一个错误:

 error: dependency 'Crypto' in target 'decryp' requires explicit declaration; 
 reference the package in the target dependency with '.product(name: "Crypto", 
 package: "swift-crypto")'

But that says what to do, so when I changed my Package.swift to the following and imported "Crypto" instead of "CryptoKit" everything works!但这说明了该怎么做,所以当我将 Package.swift 更改为以下并导入“Crypto”而不是“CryptoKit”时,一切正常!

// swift-tools-version:5.2

import PackageDescription

let package = Package(
    name: "decryp",
    dependencies: [
      .package(url: "https://github.com/apple/swift-crypto.git", .upToNextMajor(from: "1.0.1"))
    ],
    targets: [
        .target(
            name: "decryp",
            dependencies: [
              .product(name: "Crypto", package: "swift-crypto")
]
        ),
        .testTarget(
            name: "decrypTests",
            dependencies: ["decryp"]),
    ]
)

Now I can send an encrypted message from my phone to a server and then decrypt it locally on my ubuntu laptop, thanks to the swift-crypto team!现在我可以从手机向服务器发送加密消息,然后在我的 ubuntu 笔记本电脑上本地解密,这要感谢 swift-crypto 团队!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM