Skip to content

Instantly share code, notes, and snippets.

@skeeto
Created June 20, 2021 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skeeto/5c901b880fe8871b14fc5ab84c76a4d1 to your computer and use it in GitHub Desktop.
Save skeeto/5c901b880fe8871b14fc5ab84c76a4d1 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/binary"
"fmt"
"io"
"os"
)
// Mix32 is a quick-and-dirty 256-bit hash from 32-bit operations.
type Mix32 struct {
s [8]uint32
buf [28]byte
n int
}
func (m *Mix32) mix() {
m.buf[27] = byte(m.n)
m.n = 0
m.s[0] ^= binary.LittleEndian.Uint32(m.buf[0:])
m.s[1] ^= binary.LittleEndian.Uint32(m.buf[4:])
m.s[2] ^= binary.LittleEndian.Uint32(m.buf[8:])
m.s[3] ^= binary.LittleEndian.Uint32(m.buf[12:])
m.s[4] ^= binary.LittleEndian.Uint32(m.buf[16:])
m.s[5] ^= binary.LittleEndian.Uint32(m.buf[20:])
m.s[6] ^= binary.LittleEndian.Uint32(m.buf[24:])
for r := 0; r < 2; r++ {
for i := uint32(0); i < 8; i++ {
j := (i + 7) % 8
m.s[i] ^= i - m.s[j]
m.s[i] *= 0x885a308d
m.s[i] ^= m.s[i] >> 16
}
}
}
func (m *Mix32) Write(p []byte) (n int, err error) {
n = len(p)
if m.n > 0 {
if m.n+len(p) < 27 {
m.n += copy(m.buf[m.n:], p)
return
} else if m.n+len(p) >= 27 {
v := copy(m.buf[m.n:], p)
m.n = 27
m.mix()
p = p[v:]
}
}
for len(p) >= 27 {
copy(m.buf[0:], p)
m.n = 27
m.mix()
p = p[27:]
}
m.n = copy(m.buf[0:], p)
return
}
func (m *Mix32) Sum(b []byte) []byte {
c := *m
c.mix()
var buf [32]byte
binary.LittleEndian.PutUint32(buf[0:], c.s[0])
binary.LittleEndian.PutUint32(buf[4:], c.s[1])
binary.LittleEndian.PutUint32(buf[8:], c.s[2])
binary.LittleEndian.PutUint32(buf[12:], c.s[3])
binary.LittleEndian.PutUint32(buf[16:], c.s[4])
binary.LittleEndian.PutUint32(buf[20:], c.s[5])
binary.LittleEndian.PutUint32(buf[24:], c.s[6])
binary.LittleEndian.PutUint32(buf[28:], c.s[7])
return append(b, buf[:]...)
}
func (m *Mix32) Reset() {
*m = Mix32{}
}
func (m *Mix32) Size() int {
return 32
}
func (m *Mix32) BlockSize() int {
return 27
}
func main() {
var h Mix32
io.Copy(&h, os.Stdin)
s := h.Sum(nil)
const scale = 32
fmt.Printf("P1\n%d %d\n", 16*scale, 16*scale)
for y := 0; y < 16*scale; y++ {
by := y / scale
for x := 0; x < 16*scale; x++ {
bx := x / scale
b := by*16 + bx
fmt.Println(s[b/8] >> (b % 8) & 1)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment