Skip to content

Commit

Permalink
v0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
p7r0x7 committed Aug 21, 2021
1 parent 103ea6f commit a32be18
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 112 deletions.
67 changes: 34 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
. "fmt"
"github.com/p7r0x7/rathash/rathash"
"github.com/p7r0x7/vainpath"
"github.com/spf13/pflag"
. "github.com/spf13/pflag"
"os"
"runtime"
"time"
Expand All @@ -18,11 +18,10 @@ import (

var (
err error
exitCode int
exit int
readErrs int
noFormat bool
quiet bool
message []byte
)

func main() {
Expand All @@ -34,29 +33,29 @@ func main() {
quiet = true
}
}
yell, purp, und, zero := "\033[33m", "\033[35m", "\033[4m", "\033[0m"
yell, purp, und, zero := "\033[33m", "\033[35m", "\033[4m", "\033[0m" /* As in zero formatting */
if runtime.GOOS == "windows" || noFormat || quiet {
yell, purp, und, zero = "", "", "", ""
}
pHelp := pflag.BoolP("help", "h", false, purp+"prints this help menu"+zero+"\n")
pHelp := BoolP("help", "h", false, purp+"prints this help menu"+zero+"\n")

pBase64 := pflag.BoolP("base64", "b", false, purp+"renders digest as base64 string"+zero+" (default hex string)")
pBase64 := BoolP("base64", "b", false, purp+"renders digest as base64 string"+zero+" (default hex string)")

pLength := pflag.IntP("length", "l", 256, purp+"sets output digest length in bits"+zero)
pLength := IntP("length", "l", 256, purp+"sets output digest length in bits"+zero)

pflag.Bool("no-formatting", false, purp+"prints to console without formatting codes"+zero+"\n(always true on windows)")
Bool("no-formatting", false, purp+"prints to console without formatting codes"+zero+"\n(always true on windows)")

pflag.Bool("quiet", false, purp+"prints ONLY digests or breaking errors"+zero+"\n(disables formatting)")
Bool("quiet", false, purp+"prints ONLY digests or breaking errors"+zero+"\n(disables formatting)")

pString := pflag.BoolP("string", "s", false, purp+"process arguments instead as strings to be hashed"+zero)
pString := BoolP("string", "s", false, purp+"process arguments instead as strings to be hashed"+zero)

pTime := pflag.BoolP("time", "t", false, purp+"prints time taken to process each message"+zero)
pTime := BoolP("time", "t", false, purp+"prints time taken to process each message"+zero)
/* Ordered alphabetically except for help, which is hoisted to the top. */
pflag.CommandLine.SortFlags = false
pflag.Parse()
CommandLine.SortFlags = false
Parse()

/* Prints the help menu and exits the program if no other arguments are given. */
if *pHelp || pflag.NArg() == 0 {
if *pHelp || NArg() == 0 {
Println(yell + "The hopefully, eventually, cryptographic hashing algorithm." + zero + "\n\n" +
"Usage:\n" +
" rathash [-h] [--quiet|no-formatting]\n" +
Expand All @@ -65,7 +64,7 @@ func main() {
" [-b] [--quiet|no-formatting] [-l <int>|l=<int>] -s STRING...\n" +
" [-b] [-t] [--no-formatting] [-l <int>|l=<int>] -s STRING...\n\n" +
"Options:")
pflag.PrintDefaults()
PrintDefaults()
Println("\nThanks to spf13's pflag, placement of arguments after `rathash` does not matter\n" +
"unless `--` is specified to signal the end of parsed flag groups. Long-form flag\n" +
"equivalents above. `-` is treated as a reference to STDIN.")
Expand All @@ -77,8 +76,9 @@ func main() {
Println(purp + "Digest length in bits must be at least 256 and evenly divisible by 64." + zero)
os.Exit(22)
}
for i := range pflag.Args() {
path := pflag.Arg(i)
for i := range Args() {
var message []byte
path := Arg(i)
switch stdInfo, _ := os.Stdin.Stat(); {
/* The order of these cases is very important. */
case *pString:
Expand All @@ -93,39 +93,40 @@ func main() {
}
if err != nil {
readErrs++
exitCode = 1
exit = 1
continue
}

t := time.Now()
digest := rathash.Sum(message, nil, *pLength)
delta := time.Since(t).String()
str := Sprintf("%x", digest)
digest := rathash.Sum(message, *pLength)
var str, delta string

if *pTime {
delta = " (" + time.Since(t).String() + ")"
}
if *pBase64 {
str = base64.StdEncoding.EncodeToString(digest)
} else {
str = Sprintf("%x", digest)
}
if *pString {
path = Sprint(zero + "\"" + path + "\"")
path = "\"" + path + "\""
} else {
path = vainpath.Clean(path)
path = und + vainpath.Clean(path) + zero
}
switch {
case quiet:
if quiet {
Println(str)
case *pTime:
Println(yell + str + zero + " " + und + path + zero + ", (" + delta + ")")
default:
Println(yell + str + zero + " " + und + path + zero)
} else {
Println(yell + str + zero + " " + path + delta)
}
}

if quiet != true {
switch {
case readErrs == 1:
if readErrs == 1 {
Println("1 " + purp + "target is a directory or is otherwise inaccessible." + zero)
case readErrs > 1:
} else if readErrs > 1 {
Println(readErrs, purp+"targets are directories or are otherwise inaccessible."+zero)
}
}
os.Exit(exitCode)
os.Exit(exit)
}
23 changes: 11 additions & 12 deletions rathash/prime_gen/prime_gen.go → rathash/cache_gen/cache_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@ import (
)

// Copyright © 2021 Matthew R Bonnette. Openly-licensed under a BSD-3-Clause license.
/* Appends an array of primes to rathash/etc.go. */
// Appends an array of primes to caches.go.

func main() {
str, dex := "", 0
primes := make([]uint64, 10000)
one := big.NewInt(1)
const primeCount = 20000
primes := make([]uint64, primeCount)
primes[0] = 2

for i := big.NewInt(2); dex < 10000; i.Add(i, one) {
if i.ProbablyPrime(1) {
primes[dex] = i.Uint64()
dex++
for i, prime, two := 1, big.NewInt(1), big.NewInt(2); i < primeCount; i++ {
for prime.Add(prime, two).ProbablyPrime(1) == false {
}
primes[i] = prime.Uint64()
}

str = fmt.Sprintln("\nvar primes = [10000]uint64{")
str := fmt.Sprintf("\nvar primes = [%d]uint64{\n", primeCount)
for i := range primes {
switch i++; {
case i%12 == 0:
Expand All @@ -34,15 +33,15 @@ func main() {
}
str += fmt.Sprintln("\n}")

file, err := os.OpenFile("etc.go", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
file, err := os.OpenFile("caches.go", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Println("Failed: etc.go not found and could not be created.")
fmt.Println("Failed: caches.go not found and could not be created.")
os.Exit(1)
}
count, err := file.Write([]byte(str))
if err != nil {
fmt.Println("Failed: could not append array to file.")
os.Exit(1)
}
fmt.Println(count, "bytes appended successfully to etc.go")
fmt.Println(count, "bytes appended successfully to caches.go")
}
131 changes: 67 additions & 64 deletions rathash/rat.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ func (x *xoshiro) seed(a, b uint64) {
const up, charm, top = 30, 27, 31
const down, strange, bottom = 0x9e3779b97f4a7c15, 0xbf58476d1ce4e5b9, 0x94d049bb133111eb

s0 := x.s0 ^ a + down
s0 := x.s0 + a + down
s0 = (s0 ^ s0>>up) * strange
s0 = (s0 ^ s0>>charm) * bottom
x.s0 = s0 ^ s0 >> top
x.s0 = s0 ^ s0>>top

s1 := x.s1 ^ a + down + down
s1 := x.s1 + a + down + down
s1 = (s1 ^ s1>>up) * strange
s1 = (s1 ^ s1>>charm) * bottom
x.s1 = s1 ^ s1 >> top
x.s1 = s1 ^ s1>>top

s2 := x.s2 ^ b + down
s2 := x.s2 + b + down
s2 = (s2 ^ s2>>up) * strange
s2 = (s2 ^ s2>>charm) * bottom
x.s2 = s2 ^ s2 >> top
x.s2 = s2 ^ s2>>top

s3 := x.s3 ^ b + down + down
s3 := x.s3 + b + down + down
s3 = (s3 ^ s3>>up) * strange
s3 = (s3 ^ s3>>charm) * bottom
x.s3 = s3 ^ s3 >> top
x.s3 = s3 ^ s3>>top
}

// Method next updates the internal state of and returns the next value in the deterministic
Expand All @@ -52,38 +52,41 @@ func (x *xoshiro) next() uint64 {
s0, s1, s2, s3 := x.s0, x.s1, x.s2, x.s3
x.s0 ^= s3 ^ s1
x.s1 ^= s2 ^ s0
x.s2 ^= s0 ^ s1 << 17
x.s2 ^= s0 ^ s1<<17
x.s3 = bits.RotateLeft64(s3^s1, 45)
return bits.RotateLeft64(s1*5, 7) * 9
}

func Sum(msg, mac []byte, ln int) []byte {
func KeyedSum(msg, mac []byte, ln int) []byte {
var sum1, sum2 []byte
if mac != nil {
/* MACs called to the function must be at least the size of the output. */
if len(mac) < ln>>3 {
panic("invalid input: MAC length too short")
} else {
var wg sync.WaitGroup
wg.Add(2)
go func() {
sum1 = halfsum(msg, ln)
wg.Done()
}()
go func() {
sum2 = halfsum(mac, ln)
wg.Done()
}()
wg.Wait()
}
/* MACs called to the function must be at least the size of the output. */
if len(mac) < ln>>3 {
panic("invalid input: MAC length too short")
} else {
sum1 = halfsum(msg, ln)
sum2 = halfsum(sum1, ln)
var wg sync.WaitGroup
wg.Add(2)
go func() {
sum1 = halfsum(msg, ln)
wg.Done()
}()
go func() {
sum2 = halfsum(mac, ln)
wg.Done()
}()
wg.Wait()
}
for i := ln>>3 - 1; i >= 0; i-- {
sum1[i] ^= sum2[i]
}
return sum1
}

func Sum(msg []byte, ln int) []byte {
sum1 := halfsum(msg, ln)
sum2 := halfsum(sum1, ln)
for i := ln>>3 - 1; i >= 0; i-- {
sum1[i] ^= sum2[i]
}
return sum1
}

Expand Down Expand Up @@ -147,47 +150,47 @@ func halfsum(msg []byte, ln int) []byte {
if i == ln>>6-1 {
switch rem {
case 7:
prng.seed(sum, bits.ReverseBytes64(
uint64(msg[bSize*(i+1)-7])<<56)^phiE19)
prng.seed(
sum, uint64(msg[bSize*(i+1)-7]))
case 6:
prng.seed(sum, bits.ReverseBytes64(
uint64(msg[bSize*(i+1)-7])<<56|
uint64(msg[bSize*(i+1)-6])<<48)^phiE19)
prng.seed(
sum, uint64(msg[bSize*(i+1)-7])|
uint64(msg[bSize*(i+1)-6])<<8)
case 5:
prng.seed(sum, bits.ReverseBytes64(
uint64(msg[bSize*(i+1)-7])<<56|
uint64(msg[bSize*(i+1)-6])<<48|
uint64(msg[bSize*(i+1)-5])<<40)^phiE19)
prng.seed(
sum, uint64(msg[bSize*(i+1)-7])|
uint64(msg[bSize*(i+1)-6])<<8|
uint64(msg[bSize*(i+1)-5])<<16)
case 4:
prng.seed(sum, bits.ReverseBytes64(
uint64(msg[bSize*(i+1)-7])<<56|
uint64(msg[bSize*(i+1)-6])<<48|
uint64(msg[bSize*(i+1)-5])<<40|
uint64(msg[bSize*(i+1)-4])<<32)^phiE19)
prng.seed(
sum, uint64(msg[bSize*(i+1)-7])|
uint64(msg[bSize*(i+1)-6])<<8|
uint64(msg[bSize*(i+1)-5])<<16|
uint64(msg[bSize*(i+1)-4])<<24)
case 3:
prng.seed(sum, bits.ReverseBytes64(
uint64(msg[bSize*(i+1)-7])<<56|
uint64(msg[bSize*(i+1)-6])<<48|
uint64(msg[bSize*(i+1)-5])<<40|
uint64(msg[bSize*(i+1)-4])<<32|
uint64(msg[bSize*(i+1)-3])<<24)^phiE19)
prng.seed(
sum, uint64(msg[bSize*(i+1)-7])|
uint64(msg[bSize*(i+1)-6])<<8|
uint64(msg[bSize*(i+1)-5])<<16|
uint64(msg[bSize*(i+1)-4])<<24|
uint64(msg[bSize*(i+1)-3])<<32)
case 2:
prng.seed(sum, bits.ReverseBytes64(
uint64(msg[bSize*(i+1)-7])<<56|
uint64(msg[bSize*(i+1)-6])<<48|
uint64(msg[bSize*(i+1)-5])<<40|
uint64(msg[bSize*(i+1)-4])<<32|
uint64(msg[bSize*(i+1)-3])<<24|
uint64(msg[bSize*(i+1)-2])<<16)^phiE19)
prng.seed(
sum, uint64(msg[bSize*(i+1)-7])|
uint64(msg[bSize*(i+1)-6])<<8|
uint64(msg[bSize*(i+1)-5])<<16|
uint64(msg[bSize*(i+1)-4])<<24|
uint64(msg[bSize*(i+1)-3])<<32|
uint64(msg[bSize*(i+1)-2])<<40)
case 1:
prng.seed(sum, bits.ReverseBytes64(
uint64(msg[bSize*(i+1)-7])<<56|
uint64(msg[bSize*(i+1)-6])<<48|
uint64(msg[bSize*(i+1)-5])<<40|
uint64(msg[bSize*(i+1)-4])<<32|
uint64(msg[bSize*(i+1)-3])<<24|
uint64(msg[bSize*(i+1)-2])<<16|
uint64(msg[bSize*(i+1)-1])<<8)^phiE19)
prng.seed(
sum, uint64(msg[bSize*(i+1)-7])|
uint64(msg[bSize*(i+1)-6])<<8|
uint64(msg[bSize*(i+1)-5])<<16|
uint64(msg[bSize*(i+1)-4])<<24|
uint64(msg[bSize*(i+1)-3])<<32|
uint64(msg[bSize*(i+1)-2])<<40|
uint64(msg[bSize*(i+1)-1])<<48)
default:
/* Little-endian byte order */
prng.seed(sum, *(*uint64)(unsafe.Pointer(&msg[bSize*(i+1)-7])))
Expand Down
2 changes: 1 addition & 1 deletion statz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (
b.SetBytes(size)
b.ResetTimer()
for i := b.N; i > 0; i-- {
rathash.Sum(rBytes, nil, length)
rathash.Sum(rBytes, length)
}
},
func(b *testing.B) {
Expand Down
4 changes: 2 additions & 2 deletions statz/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func ratTest() {
const testLength = 256
for i := ints; i > 0; i-- {
binary.BigEndian.PutUint32(iBytes, i)
integers[i] = big.NewInt(0).SetBytes(rathash.Sum(iBytes, nil, testLength))
integers[i] = big.NewInt(0).SetBytes(rathash.Sum(iBytes, testLength))
makeBytes(1024)
random[i] = big.NewInt(0).SetBytes(rathash.Sum(rBytes, nil, testLength))
random[i] = big.NewInt(0).SetBytes(rathash.Sum(rBytes, testLength))
}
fmt.Printf("Integer input Monobit test: %5.3f%%\n", printMeanBias(integers, testLength))
fmt.Printf("Random input Monobit test: %5.3f%%\n", printMeanBias(random, testLength))
Expand Down

0 comments on commit a32be18

Please sign in to comment.