<commit_before>package otp

import (
	"bytes"
	"encoding/base32"
	"fmt"
	"strings"
	"text/template"
)

var METHODS = []string{"totp", "hotp"}
var HASHES = []string{"SHA1", "SHA256", "SHA512", "MD5"}

type KeyError struct {
	param string
	msg   string
}

func (e KeyError) Error() string {
	return fmt.Sprintf("KeyError - %v - %v", e.param, e.msg)
}

// Defines a key per otpauth specifications. See https://code.google.com/p/google-authenticator/wiki/KeyFormat for more information.
type Key struct {
	Method  string // Describes the initialization method used to generate the code. Acceptable values are either 'totp' or 'hotp' for time-based or counter-based, respectively.
	Label   string // A descriptive label for the key. Generally, this is an account identifier.
	Secret  string // String representation of the base32 encoded integer that serves the HMAC secret key.
	Issuer  string // The issuer of the key.
	Algo    string // The hash algorithm used in the HMAC. 'SHA1', 'SHA256', 'SHA512', and 'MD5' are supported.
	Digits  int    // The length of the code. 6 or 8 are acceptable.
	Period  int    // The number of seconds the code is valid for. Applies only to 'totp'.
	Counter int    // The initial counter value. Applies only to 'hotp'.
}

func (k Key) IsValid() (bool, error) {

	/*
	   check method
	*/

	if !stringInSlice(k.Method, METHODS) {
		keyErr := KeyError{
			"Method",
			"Must match one of {" + strings.Join(METHODS, ", ") + "}",
		}
		return false, keyErr
	}

	/*<commit_msg>trim comments<commit_after>package otp

import (
	"bytes"
	"encoding/base32"
	"fmt"
	"strings"
	"text/template"
)

var METHODS = []string{"totp", "hotp"}
var HASHES = []string{"SHA1", "SHA256", "SHA512", "MD5"}

type KeyError struct {
	param string
	msg   string
}

func (e KeyError) Error() string {
	return fmt.Sprintf("KeyError - %v - %v", e.param, e.msg)
}

// Defines a key per otpauth specifications. See https://code.google.com/p/google-authenticator/wiki/KeyFormat for more information.
type Key struct {
	Method  string // Initialization method. Acceptable values are either 'totp' or 'hotp' for time-based or counter-based, respectively.
	Label   string // Label for the key.
	Secret  string // String representation of base32 encoded integer.
	Issuer  string // The issuer of the key.
	Algo    string // The hash algorithm used in the HMAC.
	Digits  int    // The length of the code. 6 or 8 are acceptable.
	Period  int    // The number of seconds the code is valid for. Applies only to 'totp'.
	Counter int    // The initial counter value. Applies only to 'hotp'.
}

func (k Key) IsValid() (bool, error) {

	/*
	   check method
	*/

	if !stringInSlice(k.Method, METHODS) {
		keyErr := KeyError{
			"Method",
			"Must match one of {" + strings.Join(METHODS, ", ") + "}",
		}
		return false, keyErr
	}

	/*
{'fraction_non_alphanumeric': 0.12140575079872204, 'fraction_numerical': 0.012779552715654952, 'mean_word_length': 3.302290076335878, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1471367', 'n_tokens_mistral': 973, 'n_tokens_neox': 909, 'n_words': 340}