How convert this python hw rnd number generator
import sys
import time
from ctypes import *
def MMSYSERR_NOERROR(value):
if value != 0 :
raise Exception("Error while running winmm.dll function", value)
return value
for funcname in ["waveInOpen", "waveInPrepareHeader",
"waveInAddBuffer", "waveInStart",
"waveInStop", "waveInReset",
"waveInUnprepareHeader", "waveInClose"]:
vars()[funcname] = windll.winmm[funcname]
vars()[funcname].restype = MMSYSERR_NOERROR
class WAVEFORMATEX(Structure):
WAVE_FORMAT_PCM = 1
_fields_ = [("wFormatTag", c_ushort),
("nChannels", c_ushort),
("nSamplesPerSec", c_uint),
("nAvgBytesPerSec", c_uint),
("nBlockAlign", c_ushort),
("wBitsPerSample", c_ushort),
("cbSize", c_ushort)]
def __init__(self, samples=48000, bits=16, channels=1):
self.wFormatTag = WAVEFORMATEX.WAVE_FORMAT_PCM
self.nSamplesPerSec = samples
self.wBitsPerSample = bits
self.nChannels = channels
self.nBlockAlign = self.nChannels*self.wBitsPerSample/8
self.nAvgBytesPerSec = self.nBlockAlign*self.nSamplesPerSec
self.cbSize = 0
class WAVEHDR(Structure):
_fields_ = [("lpData", POINTER(c_char)),
("dwBufferLength", c_uint),
("dwBytesRecorded", c_uint),
("dwUser", c_uint), # User data dword or pointer
("dwFlags", c_uint),
("dwLoops", c_uint),
("lpNext", c_uint), # pointer reserved
("reserved", c_uint)] # pointer reserved
def __init__(self, waveformat):
self.dwBufferLength = waveformat.nAvgBytesPerSec
self.lpData = create_string_buffer('\000' * self.dwBufferLength)
self.dwFlags = 0
waveFormat = WAVEFORMATEX(samples=48000,bits=16)
waveBufferArray = [WAVEHDR(waveFormat) for i in range(3)]
WRITECALLBACK = WINFUNCTYPE(None, c_uint, c_uint, POINTER(c_uint), POINTER(WAVEHDR), c_uint)
def pythonWriteCallBack(HandleWaveIn, uMsg, dwInstance, dwParam1, dwParam2):
MM_WIM_CLOSE = 0x3BF
MM_WIM_DATA = 0x3C0
MM_WIM_OPEN = 0x3BE
if uMsg == MM_WIM_OPEN:
print "Open handle =", HandleWaveIn
elif uMsg == MM_WIM_CLOSE:
print "Close handle =", HandleWaveIn
elif uMsg == MM_WIM_DATA:
#print "Data handle =", HandleWaveIn
wavBuf = dwParam1.contents
if wavBuf.dwBytesRecorded > 0 :
bits = [ord(wavBuf.lpData[i]) & 1 for i in range( 0 ,wavBuf.dwBytesRecorded,2)]
bias = [bits[i] for i in range( 0 ,len(bits),2) if bits[i] != bits[i+1]]
bytes = [chr(reduce(lambda v,b:v<<1|b,bias[i-8:i], 0 )) for i in range(8,len(bias),8)]
rndstr = ''.join(bytes)
#print bytes,
sys.stdout.write(rndstr)
if wavBuf.dwBytesRecorded == wavBuf.dwBufferLength:
waveInAddBuffer(HandleWaveIn, dwParam1, sizeof(waveBuf))
else:
print "Releasing one buffer from", dwInstance[ 0 ]
dwInstance[ 0 ]-=1
else:
raise "Unknown message"
writeCallBack=WRITECALLBACK(pythonWriteCallBack)
try:
ExitFlag = c_uint(3)
HandleWaveIn = c_uint( 0 )
WAVE_MAPPER = c_int(-1)
WAVE_FORMAT_QUERY = c_int(1)
CALLBACK_FUNCTION = c_int(0x30000)
waveInOpen( 0 , WAVE_MAPPER, byref(waveFormat), 0 , 0 , WAVE_FORMAT_QUERY)
waveInOpen(byref(HandleWaveIn), WAVE_MAPPER, byref(waveFormat), writeCallBack, byref(ExitFlag), CALLBACK_FUNCTION)
for waveBuf in waveBufferArray:
waveInPrepareHeader(HandleWaveIn, byref(waveBuf), sizeof(waveBuf))
waveInAddBuffer(HandleWaveIn, byref(waveBuf), sizeof(waveBuf))
waveInStart(HandleWaveIn)
while 1:
time.sleep(1)
except KeyboardInterrupt:
waveInReset(HandleWaveIn)
while 1:
time.sleep(1)
if ExitFlag.value == 0 :
break
for waveBuf in waveBufferArray:
waveInUnprepareHeader(HandleWaveIn, byref(waveBuf), sizeof(waveBuf))
waveInClose(HandleWaveIn)
to this goland program go random seed generator
package main
import (
"http://ift.tt/1xMdqN7"
"http://ift.tt/1Nzq1jS"
"bytes"
"crypto/sha256"
"fmt"
"time"
"os"
"math/rand"
"http://ift.tt/2t3PHhp"
)
const letterBytes = "98765431"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
func RandStringBytesMaskImpr(n int) string {
b := make([]byte, n)
// A rand.Int63() generates 63 random bits, enough for letterIdxMax letters!
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = rand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
// end source
func main () {
rand.Seed(time.Now().UTC().UnixNano()) // seed rand
var address string
saltValue := ""
if len(os.Args) >= 2 {
address = os.Args[1]
if len(os.Args) == 3 {
saltValue = os.Args[2]
} else {
saltValue = "";
}
} else {
fmt.Printf("Usage: %s [Address] [Salt - optional]\n\n", os.Args[0])
os.Exit(0)
}
fmt.Printf("Using address \"%s\" and salt \"%s\"\n", address, saltValue)
tries := 0
start := time.Now()
for {
passphraseValue := RandStringBytesMaskImpr(8)
bruteforce(passphraseValue, saltValue, address);
tries += 1
if tries % 60 == 1 { // update every xx attempts
fmt.Printf("\rTried %d passphrases in %s [last passphrase: %s]", tries, time.Since(start), passphraseValue)
}
}
}
func bruteforce(passphraseValue string, saltValue string, address string) {
var priv btckey.PrivateKey
var err error
pass := fmt.Sprint(passphraseValue, "\x01")
salt := fmt.Sprint(saltValue, "\x01")
key, _ := scrypt.Key([]byte(pass), []byte(salt), 262144, 8, 1, 32)
pass = fmt.Sprint(passphraseValue, "\x02")
salt = fmt.Sprint(saltValue, "\x02")
key2 := pbkdf2.Key([]byte(pass), []byte(salt), 65536, 32, sha256.New)
var result bytes.Buffer
for i := 0; i < len(key); i++ {
result.WriteByte(key[i] ^ key2[i])
}
err = priv.FromBytes(result.Bytes())
if err != nil {
fmt.Printf("Error importing private key: %s [%s]\n", err, passphraseValue)
os.Exit(0)
}
address_uncompressed := priv.ToAddressUncompressed()
if (address_uncompressed == address) {
wif := priv.ToWIF()
fmt.Printf("Found! Passphrase %s\n", passphraseValue)
fmt.Printf("Bitcoin Address (Uncompressed) %s\n", address_uncompressed)
fmt.Printf("Private Key WIF (Uncompressed) %s\n", wif)
}
}
i try find solution , but find noting. Need add code generation how seed for this random generator const letterBytes = "98765431"
Aucun commentaire:
Enregistrer un commentaire