<commit_before>// TRANSACTION SIGNATURE (TSIG)
// 
// A TSIG or transaction signature adds a HMAC TSIG record to each message sent. 
// Basic use pattern when querying with TSIG:
//
//      m := new(Msg)
//      m.SetAxfr("miek.nl.")
//      // Add a stub TSIG record.
//      m.SetTsig("axfr.", HmacMD5, 300, uint64(time.Seconds()))
//      // Generate the contents of the complete TSIG record.
//      TsigGenerate(m, "so6ZGir4GPAqINNh9U5c3A==", "", false)
//      // A map holds all the secrets
//      secrets := make(map[string]string)      
//      secrets["axfr."] = "so6ZGir4GPAqINNh9U5c3A=="        // don't forget the . here
//
// The secrets' map index is set to 'axfr.'. This must match the ownername of the
// TSIG record, which in the above example, is also set to 'axfr.' The supported algorithm
// include: HmacMD5, HmacSHA1 and HmacSHA256.
//
// The message requesting an AXFR (almost all TSIG usage is when requesting zone transfers)
// for miek.nl with the TSIG record added is now ready to use. 
// We now need a new client with access to the secrets:
//
//      c := NewClient()
//      c.TsigSecret = secrets
//      err := c.XfrReceive(m, "172.16.17.32:53")
//
// You can now read the records from the AXFR as they come in. Each envelope is checked with TSIG.
// If something is not correct an error is returned.
//
// Basic use pattern replying to a message that has TSIG set.
// TODO(mg)
//
package dns

import (
	"crypto/hmac"
	"crypto/md5"
	"crypto/sha1"
	"crypto/sha256"
	"encoding/hex"
	"hash"
	"io"
	"strings"
	"time"
)

// HMAC hashing codes. These are transmitted as domain names.
const (
	HmacMD5    = "hmac-md5.sig-alg.reg.int."
	HmacSHA1   = "hmac-sha1."
	HmacSHA256 = "hmac-sha256."
)

// The following values must be put in wireformat, so that the MAC can be calculated.
// RFC 2845, section 3.4.2. TSIG Variables.
type tsigWireFmt struct {
	// From RR_HEADER
	Name  string "domain-name"
	Class uint16
	Ttl   uint32
	// Rdata of the TSIG
	Algorithm  string "domain-name"
	TimeSigned uint64
	Fudge      uint16
	// MACSize, MAC and OrigId excluded
	Error     uint16
	OtherLen  uint16
	OtherData string "size-hex"
}

// If we have the MAC use this type to convert it to wiredata.
// Section 3.4.3. Request MAC
type macWireFmt struct {
	MACSize uint16
	MAC     string "size-hex"
}

// 3.3. Time values used in TSIG calculations
type timerWireFmt struct {
	TimeSigned uint64
	Fudge      uint16
}

// TsigGenerate adds an TSIG RR to a message. The message should contain
// a "stub" TsigRR with the algorithm, key name (owner name of the RR), 
// time fudge (defaults to 300 seconds) and the current time
// The TSIG MAC is saved in that Tsig RR.
// When TsigGenerate is called for the
// first time requestMAC is set to the empty string.
// If something goes wrong an error is returned, otherwise it is nil.
func TsigGenerate(m *Msg, secret, requestMAC string, timersOnly bool) error {
	if !m.IsTsig() {
		// panic? panic?
		panic("TSIG not last RR in additional")
	}
	// If we barf here, the caller is to blame
	rawsecret, err := packBase64([]byte(secret))
	if err != nil {
		return err<commit_msg>Add TODO documentation<commit_after>// TRANSACTION SIGNATURE (TSIG)
// 
// A TSIG or transaction signature adds a HMAC TSIG record to each message sent. 
// Basic use pattern when querying with TSIG:
//
//      m := new(Msg)
//      m.SetAxfr("miek.nl.")
//      // Add a stub TSIG record.
//      m.SetTsig("axfr.", HmacMD5, 300, uint64(time.Seconds()))
//      // Generate the contents of the complete TSIG record.
//      TsigGenerate(m, "so6ZGir4GPAqINNh9U5c3A==", "", false)
//      // A map holds all the secrets
//      secrets := make(map[string]string)      
//      secrets["axfr."] = "so6ZGir4GPAqINNh9U5c3A=="        // don't forget the . here
//
// The secrets' map index is set to 'axfr.'. This must match the ownername of the
// TSIG record, which in the above example, is also set to 'axfr.' The supported algorithm
// include: HmacMD5, HmacSHA1 and HmacSHA256.
//
// The message requesting an AXFR (almost all TSIG usage is when requesting zone transfers)
// for miek.nl with the TSIG record added is now ready to use. 
// We now need a new client with access to the secrets:
//
//      c := NewClient()
//      c.TsigSecret = secrets
//      err := c.XfrReceive(m, "172.16.17.32:53")
//
// You can now read the records from the AXFR as they come in. Each envelope is checked with TSIG.
// If something is not correct an error is returned.
//
// Basic use pattern replying to a message that has TSIG set.
// TODO(mg)
//
package dns

import (
	"crypto/hmac"
	"crypto/md5"
	"crypto/sha1"
	"crypto/sha256"
	"encoding/hex"
	"hash"
	"io"
	"strings"
	"time"
)

// HMAC hashing codes. These are transmitted as domain names.
const (
	HmacMD5    = "hmac-md5.sig-alg.reg.int."
	HmacSHA1   = "hmac-sha1."
	HmacSHA256 = "hmac-sha256."
)

// The following values must be put in wireformat, so that the MAC can be calculated.
// RFC 2845, section 3.4.2. TSIG Variables.
type tsigWireFmt struct {
	// From RR_HEADER
	Name  string "domain-name"
	Class uint16
	Ttl   uint32
	// Rdata of the TSIG
	Algorithm  string "domain-name"
	TimeSigned uint64
	Fudge      uint16
	// MACSize, MAC and OrigId excluded
	Error     uint16
	OtherLen  uint16
	OtherData string "size-hex"
}

// If we have the MAC use this type to convert it to wiredata.
// Section 3.4.3. Request MAC
type macWireFmt struct {
	MACSize uint16
	MAC     string "size-hex"
}

// 3.3. Time values used in TSIG calculations
type timerWireFmt struct {
	TimeSigned uint64
	Fudge      uint16
}

// TsigGenerate adds an TSIG RR to a message. The message should contain
// a "stub" TsigRR with the algorithm, key name (owner name of the RR), 
// time fudge (defaults to 300 seconds) and the current time
// The TSIG MAC is saved in that Tsig RR.
// When TsigGenerate is called for the
// first time requestMAC is set to the empty string.
// If something goes wrong an error is returned, otherwise it is nil.
// TODO this needs to work on []byte, not *Msg, to take
// compression into account
// This
func TsigGenerate(m *Msg, secret, requestMAC string, timersOnly bool) error {
	if !m.IsTsig() {
		// panic? panic?
		panic("TSIG not last RR in additional")
	}
	// If we barf here, the caller is to blame
	rawsecret, err := packBase64([]byte(secret))
	if err != nil {
		return err
{'fraction_non_alphanumeric': 0.10859657394310859, 'fraction_numerical': 0.025774005972025775, 'mean_word_length': 2.8453172205438064, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3411003', 'n_tokens_mistral': 2240, 'n_tokens_neox': 2123, 'n_words': 838}