Archived
1
0

move nntpchand source to nntpchan repo

This commit is contained in:
Jeff Becker
2016-11-18 09:54:13 -05:00
parent 8dd77dcdf0
commit 91406d3400
156 changed files with 12200 additions and 1 deletions

View File

@@ -0,0 +1,56 @@
Package blake256
=====================
import "github.com/dchest/blake256"
Package blake256 implements BLAKE-256 and BLAKE-224 hash functions (SHA-3
candidate).
Public domain.
Constants
---------
``` go
const BlockSize = 64
```
The block size of the hash algorithm in bytes.
``` go
const Size = 32
```
The size of BLAKE-256 hash in bytes.
``` go
const Size224 = 28
```
The size of BLAKE-224 hash in bytes.
Functions
---------
### func New
func New() hash.Hash
New returns a new hash.Hash computing the BLAKE-256 checksum.
### func New224
func New224() hash.Hash
New224 returns a new hash.Hash computing the BLAKE-224 checksum.
### func New224Salt
func New224Salt(salt []byte) hash.Hash
New224Salt is like New224 but initializes salt with the given 16-byte slice.
### func NewSalt
func NewSalt(salt []byte) hash.Hash
NewSalt is like New but initializes salt with the given 16-byte slice.

View File

@@ -0,0 +1,194 @@
// Written in 2011-2012 by Dmitry Chestnykh.
//
// To the extent possible under law, the author have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
// http://creativecommons.org/publicdomain/zero/1.0/
// Package blake256 implements BLAKE-256 and BLAKE-224 hash functions (SHA-3
// candidate).
package blake256
import "hash"
// The block size of the hash algorithm in bytes.
const BlockSize = 64
// The size of BLAKE-256 hash in bytes.
const Size = 32
// The size of BLAKE-224 hash in bytes.
const Size224 = 28
type digest struct {
hashSize int // hash output size in bits (224 or 256)
h [8]uint32 // current chain value
s [4]uint32 // salt (zero by default)
t uint64 // message bits counter
nullt bool // special case for finalization: skip counter
x [BlockSize]byte // buffer for data not yet compressed
nx int // number of bytes in buffer
}
var (
// Initialization values.
iv256 = [8]uint32{
0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19}
iv224 = [8]uint32{
0xC1059ED8, 0x367CD507, 0x3070DD17, 0xF70E5939,
0xFFC00B31, 0x68581511, 0x64F98FA7, 0xBEFA4FA4}
pad = [64]byte{0x80}
)
// Reset resets the state of digest. It leaves salt intact.
func (d *digest) Reset() {
if d.hashSize == 224 {
d.h = iv224
} else {
d.h = iv256
}
d.t = 0
d.nx = 0
d.nullt = false
}
func (d *digest) Size() int { return d.hashSize >> 3 }
func (d *digest) BlockSize() int { return BlockSize }
func (d *digest) Write(p []byte) (nn int, err error) {
nn = len(p)
if d.nx > 0 {
n := len(p)
if n > BlockSize-d.nx {
n = BlockSize - d.nx
}
d.nx += copy(d.x[d.nx:], p)
if d.nx == BlockSize {
block(d, d.x[:])
d.nx = 0
}
p = p[n:]
}
if len(p) >= BlockSize {
n := len(p) &^ (BlockSize - 1)
block(d, p[:n])
p = p[n:]
}
if len(p) > 0 {
d.nx = copy(d.x[:], p)
}
return
}
// Sum returns the calculated checksum.
func (d0 *digest) Sum(in []byte) []byte {
// Make a copy of d0 so that caller can keep writing and summing.
d := *d0
nx := uint64(d.nx)
l := d.t + nx<<3
len := make([]byte, 8)
len[0] = byte(l >> 56)
len[1] = byte(l >> 48)
len[2] = byte(l >> 40)
len[3] = byte(l >> 32)
len[4] = byte(l >> 24)
len[5] = byte(l >> 16)
len[6] = byte(l >> 8)
len[7] = byte(l)
if nx == 55 {
// One padding byte.
d.t -= 8
if d.hashSize == 224 {
d.Write([]byte{0x80})
} else {
d.Write([]byte{0x81})
}
} else {
if nx < 55 {
// Enough space to fill the block.
if nx == 0 {
d.nullt = true
}
d.t -= 440 - nx<<3
d.Write(pad[0 : 55-nx])
} else {
// Need 2 compressions.
d.t -= 512 - nx<<3
d.Write(pad[0 : 64-nx])
d.t -= 440
d.Write(pad[1:56])
d.nullt = true
}
if d.hashSize == 224 {
d.Write([]byte{0x00})
} else {
d.Write([]byte{0x01})
}
d.t -= 8
}
d.t -= 64
d.Write(len)
out := make([]byte, d.Size())
j := 0
for _, s := range d.h[:d.hashSize>>5] {
out[j+0] = byte(s >> 24)
out[j+1] = byte(s >> 16)
out[j+2] = byte(s >> 8)
out[j+3] = byte(s >> 0)
j += 4
}
return append(in, out...)
}
func (d *digest) setSalt(s []byte) {
if len(s) != 16 {
panic("salt length must be 16 bytes")
}
d.s[0] = uint32(s[0])<<24 | uint32(s[1])<<16 | uint32(s[2])<<8 | uint32(s[3])
d.s[1] = uint32(s[4])<<24 | uint32(s[5])<<16 | uint32(s[6])<<8 | uint32(s[7])
d.s[2] = uint32(s[8])<<24 | uint32(s[9])<<16 | uint32(s[10])<<8 | uint32(s[11])
d.s[3] = uint32(s[12])<<24 | uint32(s[13])<<16 | uint32(s[14])<<8 | uint32(s[15])
}
// New returns a new hash.Hash computing the BLAKE-256 checksum.
func New() hash.Hash {
return &digest{
hashSize: 256,
h: iv256,
}
}
// NewSalt is like New but initializes salt with the given 16-byte slice.
func NewSalt(salt []byte) hash.Hash {
d := &digest{
hashSize: 256,
h: iv256,
}
d.setSalt(salt)
return d
}
// New224 returns a new hash.Hash computing the BLAKE-224 checksum.
func New224() hash.Hash {
return &digest{
hashSize: 224,
h: iv224,
}
}
// New224Salt is like New224 but initializes salt with the given 16-byte slice.
func New224Salt(salt []byte) hash.Hash {
d := &digest{
hashSize: 224,
h: iv224,
}
d.setSalt(salt)
return d
}

View File

@@ -0,0 +1,188 @@
// Written in 2011-2012 by Dmitry Chestnykh.
//
// To the extent possible under law, the author have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
// http://creativecommons.org/publicdomain/zero/1.0/
package blake256
import (
"bytes"
"fmt"
"hash"
"testing"
)
func Test256C(t *testing.T) {
// Test as in C program.
var hashes = [][]byte{
{
0x0C, 0xE8, 0xD4, 0xEF, 0x4D, 0xD7, 0xCD, 0x8D,
0x62, 0xDF, 0xDE, 0xD9, 0xD4, 0xED, 0xB0, 0xA7,
0x74, 0xAE, 0x6A, 0x41, 0x92, 0x9A, 0x74, 0xDA,
0x23, 0x10, 0x9E, 0x8F, 0x11, 0x13, 0x9C, 0x87,
},
{
0xD4, 0x19, 0xBA, 0xD3, 0x2D, 0x50, 0x4F, 0xB7,
0xD4, 0x4D, 0x46, 0x0C, 0x42, 0xC5, 0x59, 0x3F,
0xE5, 0x44, 0xFA, 0x4C, 0x13, 0x5D, 0xEC, 0x31,
0xE2, 0x1B, 0xD9, 0xAB, 0xDC, 0xC2, 0x2D, 0x41,
},
}
data := make([]byte, 72)
h := New()
h.Write(data[:1])
sum := h.Sum(nil)
if !bytes.Equal(hashes[0], sum) {
t.Errorf("0: expected %X, got %X", hashes[0], sum)
}
// Try to continue hashing.
h.Write(data[1:])
sum = h.Sum(nil)
if !bytes.Equal(hashes[1], sum) {
t.Errorf("1(1): expected %X, got %X", hashes[1], sum)
}
// Try with reset.
h.Reset()
h.Write(data)
sum = h.Sum(nil)
if !bytes.Equal(hashes[1], sum) {
t.Errorf("1(2): expected %X, got %X", hashes[1], sum)
}
}
type blakeVector struct {
out, in string
}
var vectors256 = []blakeVector{
{"7576698ee9cad30173080678e5965916adbb11cb5245d386bf1ffda1cb26c9d7",
"The quick brown fox jumps over the lazy dog"},
{"07663e00cf96fbc136cf7b1ee099c95346ba3920893d18cc8851f22ee2e36aa6",
"BLAKE"},
{"716f6e863f744b9ac22c97ec7b76ea5f5908bc5b2f67c61510bfc4751384ea7a",
""},
{"18a393b4e62b1887a2edf79a5c5a5464daf5bbb976f4007bea16a73e4c1e198e",
"'BLAKE wins SHA-3! Hooray!!!' (I have time machine)"},
{"fd7282ecc105ef201bb94663fc413db1b7696414682090015f17e309b835f1c2",
"Go"},
{"1e75db2a709081f853c2229b65fd1558540aa5e7bd17b04b9a4b31989effa711",
"HELP! I'm trapped in hash!"},
{"4181475cb0c22d58ae847e368e91b4669ea2d84bcd55dbf01fe24bae6571dd08",
`Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar a semper sed, adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis. Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nisl imperdiet. Ut convallis libero in urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis quam pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In rutrum accumsan ultricies. Mauris vitae nisi at sem facilisis semper ac in est.`,
},
{"af95fffc7768821b1e08866a2f9f66916762bfc9d71c4acb5fd515f31fd6785a", // test with one padding byte
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congu",
},
}
var vectors224 = []blakeVector{
{"c8e92d7088ef87c1530aee2ad44dc720cc10589cc2ec58f95a15e51b",
"The quick brown fox jumps over the lazy dog"},
{"cfb6848add73e1cb47994c4765df33b8f973702705a30a71fe4747a3",
"BLAKE"},
{"7dc5313b1c04512a174bd6503b89607aecbee0903d40a8a569c94eed",
""},
{"dde9e442003c24495db607b17e07ec1f67396cc1907642a09a96594e",
"Go"},
{"9f655b0a92d4155754fa35e055ce7c5e18eb56347081ea1e5158e751",
"Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo"},
}
func testVectors(t *testing.T, hashfunc func() hash.Hash, vectors []blakeVector) {
for i, v := range vectors {
h := hashfunc()
h.Write([]byte(v.in))
res := fmt.Sprintf("%x", h.Sum(nil))
if res != v.out {
t.Errorf("%d: expected %q, got %q", i, v.out, res)
}
}
}
func Test256(t *testing.T) {
testVectors(t, New, vectors256)
}
func Test224(t *testing.T) {
testVectors(t, New224, vectors224)
}
var vectors256salt = []struct{ out, in, salt string }{
{"561d6d0cfa3d31d5eedaf2d575f3942539b03522befc2a1196ba0e51af8992a8",
"",
"1234567890123456"},
{"88cc11889bbbee42095337fe2153c591971f94fbf8fe540d3c7e9f1700ab2d0c",
"It's so salty out there!",
"SALTsaltSaltSALT"},
}
func TestSalt(t *testing.T) {
for i, v := range vectors256salt {
h := NewSalt([]byte(v.salt))
h.Write([]byte(v.in))
res := fmt.Sprintf("%x", h.Sum(nil))
if res != v.out {
t.Errorf("%d: expected %q, got %q", i, v.out, res)
}
}
// Check that passing bad salt length panics.
defer func() {
if err := recover(); err == nil {
t.Errorf("expected panic for bad salt length")
}
}()
NewSalt([]byte{1, 2, 3, 4, 5, 6, 7, 8})
}
func TestTwoWrites(t *testing.T) {
b := make([]byte, 65)
for i := range b {
b[i] = byte(i)
}
h1 := New()
h1.Write(b[:1])
h1.Write(b[1:])
sum1 := h1.Sum(nil)
h2 := New()
h2.Write(b)
sum2 := h2.Sum(nil)
if !bytes.Equal(sum1, sum2) {
t.Errorf("Result of two writes differs from a single write with the same bytes")
}
}
var bench = New()
var buf = make([]byte, 8<<10)
func BenchmarkHash1K(b *testing.B) {
b.SetBytes(1024)
for i := 0; i < b.N; i++ {
bench.Write(buf[:1024])
}
}
func BenchmarkHash8K(b *testing.B) {
b.SetBytes(int64(len(buf)))
for i := 0; i < b.N; i++ {
bench.Write(buf)
}
}
func BenchmarkFull64(b *testing.B) {
b.SetBytes(64)
tmp := make([]byte, 32)
b.ResetTimer()
for i := 0; i < b.N; i++ {
bench.Reset()
bench.Write(buf[:64])
bench.Sum(tmp[0:0])
}
}

File diff suppressed because it is too large Load Diff