<commit_before>package session

import (
  "net/url"
  "strings"
  "crypto/aes"
  "crypto/sha1"
  "crypto/cipher"
  "encoding/base64"

  "code.google.com/p/go.crypto/pbkdf2"
)

func generateSecret(base, salt string) []byte {
  return pbkdf2.Key([]byte(base), []byte(salt), key_iter_num, key_size, sha1.New)
}

func decodeCookieData(cookie []byte) (data, iv []byte, err error) {
  vectors := strings.SplitN(string(cookie), "--", 2)

  data, err = base64.StdEncoding.DecodeString(vectors[0])
  if err != nil {
    return
  }

  iv, err = base64.StdEncoding.DecodeString(vectors[1])
  if err != nil {
    return
  }

  return
}

func decryptCookie(cookie []byte, secret []byte) (string, error) {
  data, iv, err := decodeCookieData(cookie)

  c, err := aes.NewCipher(secret[:32])
  if err != nil {
    return "", err
  }

  cfb := cipher.NewCBCDecrypter(c, iv)

  dd := make([]byte, len(data))
  cfb.CryptBlocks(dd, data)

  return string(dd[:]), nil
}

func DecryptSignedCookie(signed_cookie, secret_key_base, salt string) (session string, err error) {
  cookie, err := url.QueryUnescape(signed_cookie)
  if err != nil {
    return
  }

  vectors := strings.SplitN(cookie, "--", 2)
  data, err := base64.StdEncoding.DecodeString(vectors[0])
  if err != nil {
    return
  }

  session, err = decryptCookie(data, generateSecret(secret_key_base, salt))
  if err != nil {
    return
  }

  return
}

// Rails 4.0 defaults
const (
  key_iter_num = 1000
  key_size     = 64
)<commit_msg>session.DecryptSignedCookie returns decrypted data as []byte instead of string for compatibility with gorails/marshal<commit_after>package session

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/sha1"
	"encoding/base64"
	"net/url"
	"strings"

	"code.google.com/p/go.crypto/pbkdf2"
)

func generateSecret(base, salt string) []byte {
	return pbkdf2.Key([]byte(base), []byte(salt), key_iter_num, key_size, sha1.New)
}

func decodeCookieData(cookie []byte) (data, iv []byte, err error) {
	vectors := strings.SplitN(string(cookie), "--", 2)

	data, err = base64.StdEncoding.DecodeString(vectors[0])
	if err != nil {
		return
	}

	iv, err = base64.StdEncoding.DecodeString(vectors[1])
	if err != nil {
		return
	}

	return
}

func decryptCookie(cookie []byte, secret []byte) (dd []byte, err error) {
	data, iv, err := decodeCookieData(cookie)

	c, err := aes.NewCipher(secret[:32])
	if err != nil {
		return
	}

	cfb := cipher.NewCBCDecrypter(c, iv)
	dd = make([]byte, len(data))
	cfb.CryptBlocks(dd, data)

	return
}

func DecryptSignedCookie(signed_cookie, secret_key_base, salt string) (session []byte, err error) {
	cookie, err := url.QueryUnescape(signed_cookie)
	if err != nil {
		return
	}

	vectors := strings.SplitN(cookie, "--", 2)
	data, err := base64.StdEncoding.DecodeString(vectors[0])
	if err != nil {
		return
	}

	session, err = decryptCookie(data, generateSecret(secret_key_base, salt))
	if err != nil {
		return
	}

	return
}

// Rails 4.0 defaults
const (
	key_iter_num = 1000
	key_size     = 64
)
{'fraction_non_alphanumeric': 0.14834614099565654, 'fraction_numerical': 0.01804209822920147, 'mean_word_length': 3.998330550918197, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '2712863', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1086, 'n_words': 253}