<commit_before>//Copyright 2014 <NAME>. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file

package objects

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"log"
	"strconv"
	"strings"
)

//Commit is the type for defining a repository at a moment in time
type Commit struct {
	ListHash  HCID
	Version   int64
	Parents   Parents
	Hkid      HKID
	Signature []byte //131 byte max
}

//Hash gets the HCID for the Commit
func (c Commit) Hash() HCID {
	h := sha256.New()
	h.Write(c.Bytes())
	return h.Sum(nil)
}

//Bytes gets the data in a Commit in the form of a []byte
func (c Commit) Bytes() []byte {
	return []byte(c.String())
}

//String gets the data in a Commit in the form of a string
func (c Commit) String() string {
	return fmt.Sprintf("%s,\n%d,\n%s,\n%s,\n%s",
		c.ListHash.Hex(),
		c.Version,
		c.Parents,
		c.Hkid.Hex(),
		hex.EncodeToString(c.Signature))
}

//Log sends a go string escaped Commit to the log
func (c Commit) Log() {
	log.Printf(
		"list %s\n-----BEGIN COMMIT-------\n%q\n-------END COMMIT-------",
		c.Hash(),
		c,
	)
}

//Verify returns wether the Commit has a valid Signature
func (c Commit) Verify() bool {
	ObjectHash := c.genCommitHash(c.ListHash, c.Version, c.Parents, c.Hkid)
	pubkey := ecdsa.PublicKey(geterPoster.getPublicKeyForHkid(c.Hkid))
	if pubkey.Curve == nil || pubkey.X == nil || pubkey.Y == nil {
		return false
	}
	r, s := elliptic.Unmarshal(pubkey.Curve, c.Signature)
	//log.Println(pubkey, " pubkey\n", ObjectHash, " ObjectHash\n", r, " r\n", s, "s")
	if r.BitLen() == 0 || s.BitLen() == 0 {
		return false
	}
	return ecdsa.Verify(&pubkey, ObjectHash, r, s)
}

//Update the Commit to point at the list who's hash is passed in
func (c Commit) Update(listHash HCID) Commit {<commit_msg>nil commit check in validation<commit_after>//Copyright 2014 <NAME>. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file

package objects

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"log"
	"strconv"
	"strings"
)

//Commit is the type for defining a repository at a moment in time
type Commit struct {
	ListHash  HCID
	Version   int64
	Parents   Parents
	Hkid      HKID
	Signature []byte //131 byte max
}

//Hash gets the HCID for the Commit
func (c Commit) Hash() HCID {
	h := sha256.New()
	h.Write(c.Bytes())
	return h.Sum(nil)
}

//Bytes gets the data in a Commit in the form of a []byte
func (c Commit) Bytes() []byte {
	return []byte(c.String())
}

//String gets the data in a Commit in the form of a string
func (c Commit) String() string {
	return fmt.Sprintf("%s,\n%d,\n%s,\n%s,\n%s",
		c.ListHash.Hex(),
		c.Version,
		c.Parents,
		c.Hkid.Hex(),
		hex.EncodeToString(c.Signature))
}

//Log sends a go string escaped Commit to the log
func (c Commit) Log() {
	log.Printf(
		"list %s\n-----BEGIN COMMIT-------\n%q\n-------END COMMIT-------",
		c.Hash(),
		c,
	)
}

//Verify returns wether the Commit has a valid Signature
func (c Commit) Verify() bool {
	if c == nil { //  nil Commit are not valid
		return false
	}
	ObjectHash := c.genCommitHash(c.ListHash, c.Version, c.Parents, c.Hkid)
	pubkey := ecdsa.PublicKey(geterPoster.getPublicKeyForHkid(c.Hkid))
	if pubkey.Curve == nil || pubkey.X == nil || pubkey.Y == nil {
		return false
	}
	r, s := elliptic.Unmarshal(pubkey.Curve, c.Signature)
	//log.Println(pubkey, " pubkey\n", ObjectHash, " ObjectHash\n", r, " r\n", s, "s")
	if r.BitLen() == 0 || s.BitLen() == 0 {
		return false
	}
	return ecdsa.Verify(&pubkey, ObjectHash, r, s)
}

//Update the Commit to point at the list who's hash is passed in
func (c Commit) Update(listHash HCID) Commit {
{'fraction_non_alphanumeric': 0.15197246491924807, 'fraction_numerical': 0.009001853322742918, 'mean_word_length': 3.3325688073394497, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3780103', 'n_tokens_mistral': 1451, 'n_tokens_neox': 1389, 'n_words': 458}