Skip to content

Instantly share code, notes, and snippets.

@lolo32
Last active August 9, 2018 21:43
Show Gist options
  • Save lolo32/8743fc74739887aa391f710036716cbb to your computer and use it in GitHub Desktop.
Save lolo32/8743fc74739887aa391f710036716cbb to your computer and use it in GitHub Desktop.
Fast (>100MBps) CSPRNG to randomize hard drive before encryption. Compile one time, use without any dependency everywhere with static compilation.
// Based on https://gist.github.com/aeris/2a0f9beeed94102fd0cb2a8caad964d0
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/binary"
"os"
)
func main() {
BLOCK_SIZE := 1024 * 1024 // 1M
CIPHER_BLOCK_SIZE := 128
rndKey := make([]byte, CIPHER_BLOCK_SIZE/8)
rndIv := make([]byte, CIPHER_BLOCK_SIZE/8)
rndInput := make([]byte, BLOCK_SIZE)
for {
n := 0
key := rndKey[:16]
rand.Read(key)
iv := rndIv[:16]
rand.Read(iv)
block, _ := aes.NewCipher(key)
mode := cipher.NewCBCEncrypter(block, iv)
var input []byte
for {
if n%(100*1024*1024/BLOCK_SIZE) == 0 {
// Change input each 100M
input = rndInput[:BLOCK_SIZE]
rand.Read(input)
}
n = n + 1
mode.CryptBlocks(input, input)
binary.Write(os.Stdout, binary.BigEndian, input)
if n%(10*1024*1024*1024/BLOCK_SIZE) == 0 {
// Change cipher each 10G
break
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment