Bitcoin Forum
May 09, 2024, 11:52:10 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Casascius python upgrade  (Read 354 times)
stalker00075 (OP)
Newbie
*
Offline Offline

Activity: 54
Merit: 0


View Profile
February 18, 2019, 05:52:15 PM
 #1

help please, who is strong in python
need get address of the private key

wiki code python
Code:
import random
import hashlib

BASE58 = '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

def Candidate():
    """
    Generate a random, well-formed mini private key.
    """
    return('%s%s' % ('S', ''.join(
        [BASE58[ random.randrange(0,len(BASE58)) ] for i in range(29)])))

def GenerateKeys(numKeys = 10):
    """
    Generate mini private keys and output the mini key as well as the full
    private key. numKeys is The number of keys to generate, and
    """
    keysGenerated = 0
    totalCandidates = 0
    while keysGenerated < numKeys:
        try:
            cand = Candidate()
            # Do typo check
            t = '%s?' % cand
            # Take one round of SHA256
            candHash = hashlib.sha256(t).digest()
            # Check if the first eight bits of the hash are 0
            if candHash[0] == '\x00':
                privateKey = GetPrivateKey(cand)
                print('\n%s\nSHA256( ): %s\nsha256(?): %s' %
                      (cand, privateKey, candHash.encode('hex_codec')))
                if CheckShortKey(cand):
                    print('Validated.')
                else:
                    print('Invalid!')
                keysGenerated += 1
            totalCandidates += 1
        except KeyboardInterrupt:
            break
    print('\n%s: %i\n%s: %i\n%s: %.1f' %
          ('Keys Generated', keysGenerated,
           'Total Candidates', totalCandidates,
           'Reject Percentage',
           100*(1.0-keysGenerated/float(totalCandidates))))

def GetPrivateKey(shortKey):
    """
    Returns the hexadecimal representation of the private key corresponding
    to the given short key.
    """
    if CheckShortKey(shortKey):
        return hashlib.sha256(shortKey).hexdigest()
    else:
        print('Typo detected in private key!')
        return None

def CheckShortKey(shortKey):
    """
    Checks for typos in the short key.
    """
    if len(shortKey) != 30:
        return False
    t = '%s?' % shortKey
    tHash = hashlib.sha256(t).digest()
    # Check to see that first byte is \x00
    if tHash[0] == '\x00':
        return True
    return False
    GenerateKeys (1)

now
SKhHHQLDkHsAniFW2MRyVw9jwDDkKx
SHA256( ): ae172028e80ef37d3e01906ccd05441946c3efa9e4532ab20f5a6e25ce293840
sha256(?): 0057497a02482464d757500773d3d2c26badbcd94d5081d1cff9ea7c16fd2175
Validated.

need
SKhHHQLDkHsAniFW2MRyVw9jwDDkKx
SHA256( ): ae172028e80ef37d3e01906ccd05441946c3efa9e4532ab20f5a6e25ce293840
sha256(?): 0057497a02482464d757500773d3d2c26badbcd94d5081d1cff9ea7c16fd2175
Address: 17bYqJpPz3huoXuz6Dx6iLejuAHA2k2q3H
Validated.
1715298730
Hero Member
*
Offline Offline

Posts: 1715298730

View Profile Personal Message (Offline)

Ignore
1715298730
Reply with quote  #2

1715298730
Report to moderator
1715298730
Hero Member
*
Offline Offline

Posts: 1715298730

View Profile Personal Message (Offline)

Ignore
1715298730
Reply with quote  #2

1715298730
Report to moderator
The block chain is the main innovation of Bitcoin. It is the first distributed timestamping system.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715298730
Hero Member
*
Offline Offline

Posts: 1715298730

View Profile Personal Message (Offline)

Ignore
1715298730
Reply with quote  #2

1715298730
Report to moderator
darosior
Sr. Member
****
Offline Offline

Activity: 279
Merit: 435


View Profile
February 18, 2019, 08:28:35 PM
 #2

help please, who is strong in python
need get address of the private key

wiki code python
Code:
import random
import hashlib

BASE58 = '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

def Candidate():
    """
    Generate a random, well-formed mini private key.
    """
    return('%s%s' % ('S', ''.join(
        [BASE58[ random.randrange(0,len(BASE58)) ] for i in range(29)])))

def GenerateKeys(numKeys = 10):
    """
    Generate mini private keys and output the mini key as well as the full
    private key. numKeys is The number of keys to generate, and
    """
    keysGenerated = 0
    totalCandidates = 0
    while keysGenerated < numKeys:
        try:
            cand = Candidate()
            # Do typo check
            t = '%s?' % cand
            # Take one round of SHA256
            candHash = hashlib.sha256(t).digest()
            # Check if the first eight bits of the hash are 0
            if candHash[0] == '\x00':
                privateKey = GetPrivateKey(cand)
                print('\n%s\nSHA256( ): %s\nsha256(?): %s' %
                      (cand, privateKey, candHash.encode('hex_codec')))
                if CheckShortKey(cand):
                    print('Validated.')
                else:
                    print('Invalid!')
                keysGenerated += 1
            totalCandidates += 1
        except KeyboardInterrupt:
            break
    print('\n%s: %i\n%s: %i\n%s: %.1f' %
          ('Keys Generated', keysGenerated,
           'Total Candidates', totalCandidates,
           'Reject Percentage',
           100*(1.0-keysGenerated/float(totalCandidates))))

def GetPrivateKey(shortKey):
    """
    Returns the hexadecimal representation of the private key corresponding
    to the given short key.
    """
    if CheckShortKey(shortKey):
        return hashlib.sha256(shortKey).hexdigest()
    else:
        print('Typo detected in private key!')
        return None

def CheckShortKey(shortKey):
    """
    Checks for typos in the short key.
    """
    if len(shortKey) != 30:
        return False
    t = '%s?' % shortKey
    tHash = hashlib.sha256(t).digest()
    # Check to see that first byte is \x00
    if tHash[0] == '\x00':
        return True
    return False
    GenerateKeys (1)

now
SKhHHQLDkHsAniFW2MRyVw9jwDDkKx
SHA256( ): ae172028e80ef37d3e01906ccd05441946c3efa9e4532ab20f5a6e25ce293840
sha256(?): 0057497a02482464d757500773d3d2c26badbcd94d5081d1cff9ea7c16fd2175
Validated.

need
SKhHHQLDkHsAniFW2MRyVw9jwDDkKx
SHA256( ): ae172028e80ef37d3e01906ccd05441946c3efa9e4532ab20f5a6e25ce293840
sha256(?): 0057497a02482464d757500773d3d2c26badbcd94d5081d1cff9ea7c16fd2175
Address: 17bYqJpPz3huoXuz6Dx6iLejuAHA2k2q3H
Validated.
Hi,

why do you generate your private key in base58 ? If you are looking for an example I made a tutorial here and (kind of) a Python library here.
stalker00075 (OP)
Newbie
*
Offline Offline

Activity: 54
Merit: 0


View Profile
February 18, 2019, 09:11:52 PM
 #3

hi, can you please try to change the code

Code:
import random
import hashlib

BASE58 = '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

def Candidate():
    """
    Generate a random, well-formed mini private key.
    """
    return('%s%s' % ('S', ''.join(
        [BASE58[ random.randrange(0,len(BASE58)) ] for i in range(29)])))

def GenerateKeys(numKeys = 10):
    """
    Generate mini private keys and output the mini key as well as the full
    private key. numKeys is The number of keys to generate, and
    """
    keysGenerated = 0
    totalCandidates = 0
    while keysGenerated < numKeys:
        try:
            cand = Candidate()
            # Do typo check
            t = '%s?' % cand
            # Take one round of SHA256
            candHash = hashlib.sha256(t).digest()
            # Check if the first eight bits of the hash are 0
            if candHash[0] == '\x00':
                privateKey = GetPrivateKey(cand)
                print('\n%s\nSHA256( ): %s\nsha256(?): %s' %
                      (cand, privateKey, candHash.encode('hex_codec')))
                if CheckShortKey(cand):
                    print('Validated.')
                else:
                    print('Invalid!')
                keysGenerated += 1
            totalCandidates += 1
        except KeyboardInterrupt:
            break
    print('\n%s: %i\n%s: %i\n%s: %.1f' %
          ('Keys Generated', keysGenerated,
           'Total Candidates', totalCandidates,
           'Reject Percentage',
           100*(1.0-keysGenerated/float(totalCandidates))))

def GetPrivateKey(shortKey):
    """
    Returns the hexadecimal representation of the private key corresponding
    to the given short key.
    """
    if CheckShortKey(shortKey):
        return hashlib.sha256(shortKey).hexdigest()
    else:
        print('Typo detected in private key!')
        return None

def CheckShortKey(shortKey):
    """
    Checks for typos in the short key.
    """
    if len(shortKey) != 30:
        return False
    t = '%s?' % shortKey
    tHash = hashlib.sha256(t).digest()
    # Check to see that first byte is \x00
    if tHash[0] == '\x00':
        return True
    return False
    GenerateKeys (1)
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
February 19, 2019, 05:22:58 AM
Merited by TheBeardedBaby (1)
 #4

a Python library here.

why are you rounding N? That is not the correct value.

I am not really familiar with python but you may need to change sizeof(x) and sizeof(y) here to a hardcoded value '32' instead since x and y can be less than 32 bytes but you must return them with zeros appended.
Here is a unit test that must pass:
Code:
Private key: 128b87b572c379e92d8dd9f2f89118ec0bd8fe7ad6214312486d572d8718f8c7
public key: 04C7F4A880E496EE7335424E9613E7B29D37CEF9151DE1751D44E9159C699E07A20038BF72B3FF4FC35F1A6FF2E43F56DC4D5743BE3AFF67F1A7C5F6A9F227B47C

To OP:
You need to use another library on top of this one. You call your GetPrivateKey(shortKey) function with your mini private key and it will return an actual private key in hexadecimal formal. Then pass that into a key function in that library. Check out https://github.com/warner/python-ecdsa for example.

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!