使用Swift生成base64 url​​编码的X.509格式2048位RSA公钥? [英] Generate base64 url-encoded X.509 format 2048-bit RSA public key with Swift?

查看:250
本文介绍了使用Swift生成base64 url​​编码的X.509格式2048位RSA公钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Apple Swift for iOS中工作。我必须为后端生成这个,因为它是一个安全的应用程序。

Working in Apple Swift for iOS. I have to generate this for the backend as it's a secure app.

我是安全和证书的新手,现在已经搜索了一天没有结果。

I'm new to security and certificates and have been searching for a day now with no results.

如何使用swift生成base64 url​​编码的X.509格式2048位RSA公钥?

How can I generate base64 url-encoded X.509 format 2048-bit RSA public key with swift?

任何帮助都非常感谢。

推荐答案

我最近创建的一个用于处理Swift中的公钥 - 私钥对的库名为 Heimdall ,它允许您轻松导出公共密钥的X.509格式化Base64字符串。

There's a library for handling public-private key pairs in Swift that I recently created called Heimdall, which allows you to easily export the X.509 formatted Base64 string of the public key.

为遵守SO规则,我还将在此答案中包含实施(以便它不言自明)

To comply with SO rules, I will also include the implementation in this answer (so that it is self-explanatory)

public func X509PublicKey() -> NSString? {
    // Fetch the key, so that key = NSData of the public key
    let result = NSMutableData()

    let encodingLength: Int = {
        if key.length + 1 < 128 {
            return 1
        } else {
            return ((key.length + 1) / 256) + 2
        }
    }()

    let OID: [CUnsignedChar] = [0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
        0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00]

    var builder: [CUnsignedChar] = []

    // ASN.1 SEQUENCE
    builder.append(0x30)

    // Overall size, made of OID + bitstring encoding + actual key
    let size = OID.count + 2 + encodingLength + key.length
    let encodedSize = encodeLength(size)
    builder.extend(encodedSize)
    result.appendBytes(builder, length: builder.count)
    result.appendBytes(OID, length: OID.count)
    builder.removeAll(keepCapacity: false)

    builder.append(0x03)
    builder.extend(encodeLength(key.length + 1))
    builder.append(0x00)
    result.appendBytes(builder, length: builder.count)

    // Actual key bytes
    result.appendData(key)

    // Convert to Base64 and make safe for URLs
    var string = result.base64EncodedStringWithOptions(.allZeros)
    string = string.stringByReplacingOccurrencesOfString("/", withString: "_")
    string = string.stringByReplacingOccurrencesOfString("+", withString: "-")

    return string
}

public func encodeLength(length: Int) -> [CUnsignedChar] {
    if length < 128 {
        return [CUnsignedChar(length)];
    }

    var i = (length / 256) + 1
    var len = length
    var result: [CUnsignedChar] = [CUnsignedChar(i + 0x80)]

    for (var j = 0; j < i; j++) {
        result.insert(CUnsignedChar(len & 0xFF), atIndex: 1)
        len = len >> 8
    }

    return result
}

我删除了数据提取代码,请参考 Heimdall 或Jeff Hay的answer

I have removed the data fetching code, refer to either source of Heimdall or Jeff Hay's answer

这篇关于使用Swift生成base64 url​​编码的X.509格式2048位RSA公钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆