mozilla-services/pkcs7

Name: pkcs7

Owner: Mozilla Services

Description: Implements a subset of PKCS#7/Cryptographic Message Syntax (rfc2315, rfc5652)

Forked from: fullsailor/pkcs7

Created: 2016-12-29 21:39:06.0

Updated: 2018-03-21 13:44:56.0

Pushed: 2018-04-05 16:38:49.0

Homepage: https://go.mozilla.org/pkcs7

Size: 144

Language: Go

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

pkcs7

GoDoc Build Status

pkcs7 implements parsing and creating signed and enveloped messages.

age main

rt (
"bytes"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"

"go.mozilla.org/pkcs7"


 SignAndDetach(content []byte, cert *x509.Certificate, privkey *rsa.PrivateKey) (signed []byte, err error) {
toBeSigned, err := NewSignedData(content)
if err != nil {
    err = fmt.Errorf("Cannot initialize signed data: %s", err)
    return
}
if err = toBeSigned.AddSigner(cert, privkey, SignerInfoConfig{}); err != nil {
    err = fmt.Errorf("Cannot add signer: %s", err)
    return
}

// Detach signature, omit if you want an embedded signature
toBeSigned.Detach()

signed, err = toBeSigned.Finish()
if err != nil {
    err = fmt.Errorf("Cannot finish signing data: %s", err)
    return
}

// Verify the signature
pem.Encode(os.Stdout, &pem.Block{Type: "PKCS7", Bytes: signed})
p7, err := pkcs7.Parse(signed)
if err != nil {
    err = fmt.Errorf("Cannot parse our signed data: %s", err)
    return
}

// since the signature was detached, reattach the content here
p7.Content = content

if bytes.Compare(content, p7.Content) != 0 {
    err = fmt.Errorf("Our content was not in the parsed data:\n\tExpected: %s\n\tActual: %s", content, p7.Content)
    return
}
if err = p7.Verify(); err != nil {
    err = fmt.Errorf("Cannot verify our signed data: %s", err)
    return
}

return signed, nil

Credits

This is a fork of fullsailor/pkcs7


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.