MCPcopy Create free account
hub / github.com/russellhaering/goxmldsig

github.com/russellhaering/goxmldsig

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.4.0 ↗ · + Follow · compare 2 versions
165 symbols 470 edges 16 files 45 documented · 27% updated 4mo agov1.6.0 · 2026-03-18★ 17825 open issues

Browse by type

Functions 130 Types & classes 35
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

goxmldsig

Build Status GoDoc

XML Digital Signatures implemented in pure Go.

Installation

Install goxmldsig using go get:

$ go get github.com/russellhaering/goxmldsig

Usage

Include the types.Signature struct from this package in your application messages.

import (
    sigtypes "github.com/russellhaering/goxmldsig/types"
)

type AppHdr struct {
    ...
    Signature *sigtypes.Signature
}

Signing

package main

import (
    "github.com/beevik/etree"
    "github.com/russellhaering/goxmldsig"
)

func main() {
    // Generate a key and self-signed certificate for signing
    randomKeyStore := dsig.RandomKeyStoreForTest()
    ctx := dsig.NewDefaultSigningContext(randomKeyStore)
    elementToSign := &etree.Element{
        Tag: "ExampleElement",
    }
    elementToSign.CreateAttr("ID", "id1234")

    // Sign the element
    signedElement, err := ctx.SignEnveloped(elementToSign)
    if err != nil {
        panic(err)
    }

    // Serialize the signed element. It is important not to modify the element
    // after it has been signed - even pretty-printing the XML will invalidate
    // the signature.
    doc := etree.NewDocument()
    doc.SetRoot(signedElement)
    str, err := doc.WriteToString()
    if err != nil {
        panic(err)
    }

    println(str)
}

Signature Validation

// Validate an element against a root certificate
func validate(root *x509.Certificate, el *etree.Element) {
    // Construct a signing context with one or more roots of trust.
    ctx := dsig.NewDefaultValidationContext(&dsig.MemoryX509CertificateStore{
        Roots: []*x509.Certificate{root},
    })

    // It is important to only use the returned validated element.
    // See: https://www.w3.org/TR/xmldsig-bestpractices/#check-what-is-signed
    validated, err := ctx.Validate(el)
    if err != nil {
        panic(err)
    }

    doc := etree.NewDocument()
    doc.SetRoot(validated)
    str, err := doc.WriteToString()
    if err != nil {
        panic(err)
    }

    println(str)
}

Limitations

This library was created in order to implement SAML 2.0 without needing to execute a command line tool to create and validate signatures. It currently only implements the subset of relevant standards needed to support that implementation, but I hope to make it more complete over time. Contributions are welcome.

Extension points exported contracts — how you extend this code

browse all types & interfaces →

Core symbols most depended-on inside this repo

browse all functions →

Shape

Function 75
Method 55
Struct 26
Interface 5
TypeAlias 3
FuncType 1

Languages

Go100%

Modules by API surface

canonicalize.go29 symbols
etreeutils/namespace.go24 symbols
validate.go15 symbols
types/signature.go15 symbols
sign.go15 symbols
canonicalize_test.go13 symbols
keystore.go11 symbols
validate_test.go10 symbols
clock.go8 symbols
sign_test.go7 symbols
xml_constants.go4 symbols
etreeutils/unmarshal.go4 symbols

Dependencies from manifests, versioned

github.com/jonboulle/clockworkv0.2.2 · 1×
gopkg.in/check.v1v1.0.0-2020113013444 · 1×
gopkg.in/yaml.v3v3.0.0-2021010719292 · 1×

For agents

$ claude mcp add goxmldsig \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page