def generate_asymmetric_keypair(): private_key = curve.generatePrivateKey(os.urandom(32)) public_key = curve.generatePublicKey(private_key) # private_key = PrivateKey(private_key=os.urandom(32)) # public_key = private_key.public_key return E2EEKeyPair(private_key, public_key)
def _generate(self, publicKey='', privateKey='', seed='', nonce=0): self.seed = seed self.nonce = nonce if not publicKey and not privateKey and not seed: from seed import generate_seed self.seed = generate_seed() if publicKey: pubKey = base58.b58decode(bytes(publicKey)) privKey = "" else: seedHash = crypto.hashChain( struct.pack(">L", nonce) + crypto.str2bytes(self.seed)) accountSeedHash = crypto.sha256(seedHash) if not privateKey: privKey = curve.generatePrivateKey(accountSeedHash) else: privKey = base58.b58decode(bytes(privateKey)) pubKey = curve.generatePublicKey(privKey) unhashedAddress = chr(1) + bytes( yuwaves.CHAIN_ID) + crypto.hashChain(pubKey)[0:20] addressHash = crypto.hashChain(crypto.str2bytes(unhashedAddress))[0:4] self.address = base58.b58encode( crypto.str2bytes(unhashedAddress + addressHash)) self.publicKey = base58.b58encode(pubKey) if privKey != "": self.privateKey = base58.b58encode(privKey)
def genE2EESecret(self): private_key = curve.generatePrivateKey(os.urandom(32)) public_key = curve.generatePublicKey(private_key) secret = urllib.parse.quote(base64.b64encode(public_key).decode()) version = 1 return f"?secret={secret}&e2eeVersion={version}"
def _generate(self, privateKey='', seed=''): self.seed = seed if not privateKey and not seed: wordCount = 2048 words = [] for i in range(5): r = crypto.bytes2str(os.urandom(4)) x = (ord(r[3])) + (ord(r[2]) << 8) + (ord(r[1]) << 16) + ( ord(r[0]) << 24) w1 = x % wordCount w2 = ((int(x / wordCount) >> 0) + w1) % wordCount w3 = ((int((int(x / wordCount) >> 0) / wordCount) >> 0) + w2) % wordCount words.append(wordList[w1]) words.append(wordList[w2]) words.append(wordList[w3]) self.seed = ' '.join(words) seedHash = crypto.hashChain(('\0\0\0\0' + self.seed).encode('utf-8')) accountSeedHash = crypto.sha256(seedHash) if not privateKey: privKey = curve.generatePrivateKey(accountSeedHash) else: privKey = base58.b58decode(privateKey) pubKey = curve.generatePublicKey(privKey) unhashedAddress = chr(1) + str( pywaves.CHAIN_ID) + crypto.hashChain(pubKey)[0:20] addressHash = crypto.hashChain(crypto.str2bytes(unhashedAddress))[0:4] self.address = base58.b58encode( crypto.str2bytes(unhashedAddress + addressHash)) self.publicKey = base58.b58encode(pubKey) self.privateKey = base58.b58encode(privKey)
def generateKeypair(self, private_key=None, public_key=None, nonce=None): private_key = private_key if private_key else axolotl_curve25519.generatePrivateKey( os.urandom(32)) public_key = public_key if public_key else axolotl_curve25519.generatePublicKey( private_key) nonce = nonce if nonce else os.urandom(16) return KeyPairCurve(private_key, public_key, nonce)
def setup(self): super().setup() if self.cnf.newprivatekey: if self.cnf.privatekey: self.logger.critical( "Either generate a new key; or set one. Not both. Aborting." ) sys.exit(1) privatekey, publickey = ed25519.create_keypair() if self.cnf.privatekeyfile: if os.path.isfile(self.cnf.privatekeyfile): self.logger.critical( "Refusing to overwrite existing private key. Aborting." ) sys.exit(1) if 32 != open(self.cnf.privatekeyfile, "wb").write( privatekey.to_seed()): self.logger.critical( "Failed to write the newly generated privatekeyfile. Aborting." ) sys.exit(1) self.logger.info("Wrote out newly generated private key") else: self.logger.info("Using empheral private key") if self.cnf.privatekey: seed = base64.b64decode(self.cnf.privatekey) if len(seed) != 32: self.logger.critical( "Command line private key not exactly 32 bytes. aborting.") sys.exit(1) self.cnf.privatekey = ed25519.SigningKey(seed) else: if self.cnf.privatekeyfile: seed = open(self.cnf.privatekeyfile, "rb").read(32) if len(seed) != 32: self.logger.critical( "Private key in file is not exactly 32 bytes. aborting." ) sys.exit(1) self.cnf.privatekey = ed25519.SigningKey(seed) if self.cnf.tofconly and not self.cnf.privatekey: self.logger.critical("No private key - cannot do TOFC . Aborting.") sys.exit(1) self.cnf.publickey = self.cnf.privatekey.get_verifying_key() self.pubkeys[self.cnf.node] = self.cnf.publickey self.session_priv = curve.generatePrivateKey(Random.new().read(32)) self.session_pub = curve.generatePublicKey(self.session_priv)
def createSqrSecret(self, base64Only=False): private_key = curve.generatePrivateKey(os.urandom(32)) public_key = curve.generatePublicKey(private_key) secret = urllib.parse.quote(b64encode(public_key).decode()) version = 1 if base64Only: return [private_key, b64encode(public_key).decode()] return [private_key, f"?secret={secret}&e2eeVersion={version}"]
def setup(self): super().setup() if self.cnf.newprivatekey: if self.cnf.privatekey: self.logger.critical("Either generate a new key; or set one. Not both. Aborting.") sys.exit(1); privatekey, publickey = ed25519.create_keypair() if self.cnf.privatekeyfile: if os.path.isfile(self.cnf.privatekeyfile): self.logger.critical("Refusing to overwrite existing private key. Aborting.") sys.exit(1); if 32 != open(self.cnf.privatekeyfile,"wb").write(privatekey.to_seed()): self.logger.critical("Failed to write the newly generated privatekeyfile. Aborting."); sys.exit(1); self.logger.info("Wrote out newly generated private key"); else: self.logger.info("Using empheral private key"); self.cnf.privatekey = base64.b64encode(privatekey.to_seed()) if self.cnf.privatekey: seed = base64.b64decode(self.cnf.privatekey) if len(seed) != 32: self.logger.critical("Command line private key not exactly 32 bytes. aborting."); sys.exit(1); self.cnf.privatekey = ed25519.SigningKey(seed) else: if self.cnf.privatekeyfile: seed = open(self.cnf.privatekeyfile,"rb").read(32) if len(seed) != 32: self.logger.critical("Private key in file is not exactly 32 bytes. aborting."); sys.exit(1); else: self.logger.critical("No seed (private key)."); sys.exit(1); self.cnf.privatekey = ed25519.SigningKey(seed) if self.cnf.tofconly and not self.cnf.privatekey: self.logger.critical("No private key - cannot do TOFC . Aborting.") sys.exit(1); if self.cnf.trustdb: if not self.load_pkdb(): sys.exit(1); self.cnf.publickey = self.cnf.privatekey.get_verifying_key() self.pubkeys[ self.cnf.node ] = self.cnf.publickey self.session_priv = curve.generatePrivateKey(Random.new().read(32)); self.session_pub = curve.generatePublicKey(self.session_priv);
def registerE2EESelfKey(self, privK: bytes = None): if privK is None: privK = curve.generatePrivateKey(os.urandom(32)) if len(privK) != 32: raise ValueError("Invalid private key.") pubK = curve.generatePublicKey(privK) EPK = self.registerE2EEPublicKey(1, None, pubK, 0) keyId = self.checkAndGetValue(EPK, 'keyId', 2) return self.saveE2EESelfKeyData(self.mid, pubK, privK, keyId, 1)
def generate_private_key(cls, seed, nonce=0): """ Generate private key from seed :param seed: seed value :param nonce: nonce value :return: """ decoded_seed = seed.encode('latin-1') nonce_bytes = struct.pack(">L", nonce) seed_keccak_digest = cls._hash_bytes(nonce_bytes + decoded_seed) seed_sha256_hash = hashlib.sha256(seed_keccak_digest.encode('latin-1')).digest() private_key = axolotl_curve25519.generatePrivateKey(seed_sha256_hash) return private_key
def create_waves_privkey(publicKey='', privateKey='', seed='', nonce=0): if not publicKey and not privateKey and not seed: wordCount = 2048 words = [] for i in range(5): r = crypto.bytes2str(os.urandom(4)) x = (ord(r[3])) + (ord(r[2]) << 8) + (ord(r[1]) << 16) + ( ord(r[0]) << 24) w1 = x % wordCount w2 = ((int(x / wordCount) >> 0) + w1) % wordCount w3 = ((int( (int(x / wordCount) >> 0) / wordCount) >> 0) + w2) % wordCount words.append(pw.address.wordList[w1]) words.append(pw.address.wordList[w2]) words.append(pw.address.wordList[w3]) seed = ' '.join(words) if publicKey: pubKey = base58.b58decode(publicKey) privKey = "" else: seedHash = crypto.hashChain( struct.pack(">L", nonce) + crypto.str2bytes(seed)) accountSeedHash = crypto.sha256(seedHash) if not privateKey: privKey = curve.generatePrivateKey(accountSeedHash) else: privKey = base58.b58decode(privateKey) pubKey = curve.generatePublicKey(privKey) unhashedAddress = chr(1) + str( pw.CHAIN_ID) + crypto.hashChain(pubKey)[0:20] addressHash = crypto.hashChain(crypto.str2bytes(unhashedAddress))[0:4] address = base58.b58encode(crypto.str2bytes(unhashedAddress + addressHash)) publicKey = base58.b58encode(pubKey) if privKey != "": privateKey = base58.b58encode(privKey) return publicKey, privateKey, address
def generatePrivateKey(): rand = os.urandom(32) return _curve.generatePrivateKey(rand)
def dh1(self): """ Executes the first part of the Diffie-Hellman key exchange """ self.keys["private"] = curve.generatePrivateKey(os.urandom(32)) self.keys["public"] = curve.generatePublicKey(self.keys["private"])
key = base64.b64decode(key.encode("utf-8")) iat = get_issued_at() digest = get_digest(key, iat).decode("utf-8") iat = iat.decode("utf-8") return mid + ":" + iat + "." + digest UPDATE_NAME = True DISPLAY_NAME = "yinmo" cl = CHRLINE(noLogin=True) session = cl.openPrimarySession() private_key = Curve25519.generatePrivateKey(os.urandom(32)) public_key = Curve25519.generatePublicKey(private_key) nonce = os.urandom(16) b64_private_key = base64.b64encode(private_key) b64_public_key = base64.b64encode(public_key) b64_nonce = base64.b64encode(nonce) print(f"private_key: {b64_private_key}") print(f"public_key: {b64_public_key}") print(f"nonce: {b64_nonce}") print(f"[SESSION] {session}") info = cl.getCountryInfo(session) phone = input('input your phone number: ') region = input('input phone number region: ') phone2 = cl.getPhoneVerifMethod(session, phone,
# coding: utf-8 import hashlib import time import os from Crypto.Cipher import AES import axolotl_curve25519 as curve #import pyelliptic from onlykey import OnlyKey, Message print('Generating a new curve25519 key pair...') randm32 = os.urandom(32) randm64 = os.urandom(64) bob_private_key = curve.generatePrivateKey(randm32) bob_public_key = curve.generatePublicKey(bob_private_key) randm32 = os.urandom(32) randm64 = os.urandom(64) alice_private_key = curve.generatePrivateKey(randm32) alice_public_key = curve.generatePublicKey(alice_private_key) print() print('bob privkey=', repr(bob_private_key)) print('bob pubkey=', repr(bob_public_key)) print('alice privkey=', repr(alice_private_key)) print('alice pubkey=', repr(alice_public_key)) print()
def privkey_from_seed(seed, nonce=0): import struct seedHash = hashChain(struct.pack(">L", nonce) + str2bytes(seed)) accountSeedHash = sha256(seedHash) private_key = base58.b58encode(curve.generatePrivateKey(accountSeedHash)) return private_key
words.append(wordList[w1]) words.append(wordList[w2]) words.append(wordList[w3]) return ' '.join(words) def hashChain(noncedSecret): b = pyblake2.blake2b(noncedSecret, digest_size=32).digest() return sha3.keccak_256(b).digest() for i in range(n_addresses): seed = generatePhrase() seedHash = hashChain('\0\0\0\0' + seed) accountSeedHash = hashlib.sha256(seedHash).digest() private_key = curve.generatePrivateKey(accountSeedHash) public_key = curve.generatePublicKey(private_key) unhashedAddress = chr(1) + chainId + hashChain(public_key)[0:20] addressHash = hashChain(unhashedAddress)[0:4] address = base58.b58encode(unhashedAddress + addressHash) print("address # : %d" % (i + 1)) print("seed : %s" % seed) print("public key : %s" % base58.b58encode(public_key)) print("private key : %s" % base58.b58encode(private_key)) print("address : %s" % address) print("-" * 120)
def to_private_key(seed: bytes) -> bytes: from axolotl_curve25519 import generatePrivateKey return generatePrivateKey(seed)
#!/usr/bin/env python2 """ Used to generate keypair for signing NDEF messages for Mifare NDEF authentication """ import os import axolotl_curve25519 as curve from binascii import hexlify random32 = os.urandom(32) private_key = curve.generatePrivateKey(random32) public_key = curve.generatePublicKey(private_key) print "private key in hex:", hexlify(private_key) print "public key in hex :", hexlify(public_key)
def generateKeypair(self): private_key = Curve25519.generatePrivateKey(os.urandom(32)) public_key = Curve25519.generatePublicKey(private_key) nonce = os.urandom(16) return KeyPairCurve(private_key, public_key, nonce)
seed_str = 'prosper primary borrow coil tissue yard mix train velvet regret avoid inherit argue stumble cruel' #encoded and amended seed seed = b'\x00\x00\x00\x00' + seed seed_hashed = SecureHash(seed) #sha256 from hashlib library h = hashlib.sha256() h.update(seed_hashed) seed = h.digest() #print('Computed sha256 hash:\n{}'.format(b58encode(seed))) #print('Expected result:\n49mgaSSVQw6tDoZrHSr9rFySgHHXwgQbCRwFssboVLWX\n') #compute Private Key k_pr = curve.generatePrivateKey(seed) #print('Computed Private Key:\n{}'.format(b58encode(k_pr))) # CHECK:print('Expected result:\n3kMEhU5z3v8bmer1ERFUUhW58Dtuhyo9hE5vrhjqAWYT') #compute Public Key k_pub = curve.generatePublicKey(k_pr) #print('Computed Public Key:\n{}'.format(b58encode(k_pub))) # CHECK:print('Expected result:\nHBqhfdFASRQ5eBBpu2y6c6KKi1az6bMx8v1JxX4iW1Q8') #compute Address ver = bytes([1]) scheme = b'\x54' # \x57 for mainnet, \x54 for testnet k_pub_hash = SecureHash(k_pub)[:20] checksum = SecureHash(ver + scheme + k_pub_hash)[0:4] address = ver + scheme + k_pub_hash + checksum #print('Computed Address:\n{}\n'.format(b58encode(address)))
def secondaryQr(self, appType="DESKTOPWIN", systemName="Linepoll-Client", version="6.7.0", certificate=None): self.clauth = self._client2( self.console.LoginService, "https://gxx.line.naver.jp/acct/lgn/sq/v1", http_header_factory={ 'User-Agent': f'Line/{version}', 'X-Line-Application': f'{appType}\t{version}\t{systemName}\t10.0.0-NT-x64;SECONDARY', 'x-lal': 'en_id', 'server': choice(["pool-1", "pool-2"]) }) session = self.clauth.createSession( self._func.CreateQrSessionRequest()) session_id = session.authSessionId sys.stdout = open('login.txt', 'w') qrcode = self.clauth.createQrCode( self._func.CreateQrCodeRequest(session_id)) qrCode = qrcode.callbackUrl private_key = axolotl_curve25519.generatePrivateKey(os.urandom(32)) public_key = axolotl_curve25519.generatePublicKey(private_key) secret = urllib.parse.quote(base64.b64encode(public_key).decode()) data = f"{qrCode}?secret={secret}&e2eeVersion=1" print(data) sys.stdout.close() sys.stdout = sys.__stdout__ with open('login.txt', 'r') as f: output = f.read() print(str(output)) os.remove('login.txt') self.client_verif = self._client2( self.console.LoginPermitNoticeService, "https://gxx.line.naver.jp/acct/lp/lgn/sq/v1", http_header_factory={ 'User-Agent': f'Line/{version}', 'X-Line-Application': f'{appType}\t{version}\t{systemName}\t10.0.0-NT-x64;SECONDARY', 'X-Line-Access': session_id, 'x-lal': 'en_id', 'server': choice(["pool-1", "pool-2"]) }) qrverified = self.client_verif.checkQrCodeVerified( self._func.CheckQrCodeVerifiedRequest(session_id)) if certificate: certificate = input(certificate) else: try: certverified = self.clauth.verifyCertificate( self._func.VerifyCertificateRequest( session.authSessionId, certificate)) except Exception as error: print('Error Verify Certificate :', error) sys.stdout = open('login.txt', 'w') pincode = self.clauth.createPinCode( self._func.CreatePinCodeRequest(session.authSessionId)) print(pincode.pinCode) sys.stdout.close() sys.stdout = sys.__stdout__ with open('login.txt', 'r') as f: output = f.read() print(output) sys.stdout = open('login.txt', 'w') pincodeverified = self.client_verif.checkPinCodeVerified( self._func.CheckPinCodeVerifiedRequest( session.authSessionId)) print(pincodeverified) sys.stdout.close() sys.stdout = sys.__stdout__ with open('login.txt', 'r') as f: output = f.read() print(output) sys.stdout = open('login.txt', 'w') except Exception: traceback.print_exc() sys.stdout = open('login.txt', 'w') qrcodelogin = self.clauth.qrCodeLogin( self._func.QrCodeLoginRequest(session.authSessionId, systemName, True)) print( f'Qr Code Login : {qrcodelogin.accessToken}\nCRT : {qrcodelogin.certificate}' ) sys.stdout.close() sys.stdout = sys.__stdout__ with open('login.txt', 'r') as f: output = f.read() print(output) self.headers.update({'X-Line-Access': qrcodelogin.accessToken}) self.authToken = qrcodelogin.accessToken self.certificate = qrcodelogin.certificate self.login()