Skip to content
FutureBlitz

    Install

    Import std.crypto.pem

    Key

    std.crypto.pem

    struct

    Public Struct Key

    A private or public key, as its kind's Methods take it: an ECDSA P-256 private key's 32 bytes or public key's 65; an RSA key's PKCS #1 DER, whose public one Rsa verifies with; and an X25519 or Ed25519 key's 32 bytes.

    Examples

    Local key = Key(KeyKind.ED25519, True, Ed25519.NewPrivateKey())
    Local pem = key.ToPem()
    Expect(pem.StartsWith("-----BEGIN PRIVATE KEY-----"))
    Local read = Try Key.FromPem(pem)
    Expect(read.Kind == KeyKind.ED25519 && read.IsPrivate && read.Bytes == key.Bytes)

    std/crypto/pem.fb, line 52

    • Public ReadOnly Field Kind: KeyKind field #

      What kind of key it is.

    • Public ReadOnly Field IsPrivate: Bool field #

      Whether it's a private key, or a public one.

    • Public ReadOnly Field Bytes: UInt8[] field #

      Its bytes, as its kind's Methods take them.

    • Public New(kind: KeyKind, isPrivate: Bool, bytes: UInt8[]) constructor #

      A key of kind, private or public, of bytes.

    • Public Static Method FromPem(pem: String): Key Throws ParseError static method #

      The first key in pem, whatever comes after it: a private one in PKCS #8 ("PRIVATE KEY"), SEC 1 ("EC PRIVATE KEY"), or PKCS #1 ("RSA PRIVATE KEY"), or a public one ("PUBLIC KEY", or "RSA PUBLIC KEY"). Throws a ParseError of SYNTAX when pem has no key, or one that doesn't read, and of WRONG_TYPE when it's encrypted, or of a kind there's no KeyKind for, like another curve's.

    • @PanicsAtCaller Public Method ToPem(): String method #

      The key as PEM, as OpenSSL writes it: a private one in PKCS #8, but an RSA one in PKCS #1 and a P-256 one in SEC 1, and a public one, RSA's too, in SubjectPublicKeyInfo. Panics when Bytes isn't a key of its Kind.

    Report an issue with this page or with FutureBlitz on GitHub.