Code Monkey home page Code Monkey logo

dkim's Introduction

dkim

Library for signing emails with DKIM.

The purpose of this library is to create a solution for DKIM signing in Go that is fast, simple, and suitable for bulk mail applications.

To create the body hash, the body can be written in chunks, written all at once, or copied from an io.Reader using io.Copy. There is no need to present the complete message or body as a byte slice in memory. At this moment only "simple" body canonicalization is supported, which is efficient and secure. The only prerequisite is that body lines have proper CRLF line terminators.

The create the header hash, a standard mail.Header is used for input. This saves parsing when a mail.Header is already available. At this moment only "relaxed" header canonicalization is supported, which is reliable and most common. Part of the canonicalization is done by mail.ReadMessage or textproto.Reader.ReadMIMEHeader when a mail.Header is created.

Because mail.Header is implemented with a map, the order of headers in the "h=" tag is non-deterministic. Repeated headers are represented as array values in mail.Header and are signed bottom most first. This should be fine, since validators will follow the ordering in the "h=" tag and repeated headers are processed in the same order as they are signed.

The hashing algorithm is considered a global policy and can be changed from SHA256 to SHA1. The headers that are included in the signature can also be changed by modifying a global parameter. For headers that are repeated all instances will be signed.

Using

First create a signing domain from a private key in memory or read it from disk:

domain, err := dkim.ReadDomain("emailfabric.com", "test", "testdata/test.rsa")
if err != nil {
	return
}

You need a mail.Header for the email header and a io.Reader for the email body. You can use mail.ReadMessage to create these:

msg, err := mail.ReadMessage(bytes.NewReader(data))
if err != nil {
	return
}

Creating the signature goes as follows:

sig := dkim.NewSignature(domain)

// step 1
_, err = io.Copy(sig.BodyWriter(), msg.Body)
if err != nil {
	return
}

// step 2
err = sig.SignHeader(msg.Header)
if err != nil {
	return
}

The signature must be prepended to the email header. Just write it before writing the email message:

_, err = sig.WriteTo(writer)
if err != nil {
	return
}

Complete API documentation can be found at GoDoc.

dkim's People

Contributors

postmastery avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.