Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _2user():
# 1st user
exp1 = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
public1 = nacl.crypto_scalarmult_curve25519_base(exp1)
#print "public1: \t%s\nexp1: \t%s" % (b85encode(public1), b85encode(exp1))
print
# 2nd user
exp2 = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
public2 = nacl.crypto_scalarmult_curve25519_base(exp2)
key = nacl.crypto_scalarmult_curve25519(exp2, public1)
print "key: \t%s" % (b85encode(key))
#print "public2: \t%s\nkey: \t%s" % (b85encode(public2), b85encode(key))
print
# 1st user completing DH
key = nacl.crypto_scalarmult_curve25519(exp1, public2)
print "key: \t%s" % (b85encode(key))
if clargs.file is None:
message = sys.stdin.read().encode()
else:
with open(clargs.file, 'r') as msgfile:
message=msgfile.read().encode()
if (message is None) or (len(message) == 0):
sys.exit('Error: Plaintext length 0, aborting.')
# generate a random (ephemeral) private key
eprivkey = FieldElement.urandom(curve.p)
SharedPt = Pubkey * eprivkey
sbytes = SharedPt.compressed()
key = sha256(sbytes).digest()
nonce = pysodium.randombytes(pysodium.crypto_stream_NONCEBYTES)
assert pysodium.crypto_stream_NONCEBYTES == 24
assert pysodium.crypto_stream_KEYBYTES == 32
ctext = pysodium.crypto_stream_xor(message, len(message), nonce, key)
# public key point for ephemeral key
Gpt = ECPoint(curve, curve.G)
ePubkey = Gpt * eprivkey
DERmsg = der_encode_message(ePubkey, nonce, ctext)
print(pem_wrap(DERmsg, 'ECDHE_XSALSA20 ENCRYPTED MESSAGE'))
def do_create_channel(self, line):
"""
Create a random channel name (hex number) and set the new channel in
the configuration.
"""
channel = base64.b16encode(pysodium.randombytes(16)).lower()
self.config[b'channel'] = channel
save_data(CONFIG, self.config)
print('[+] Channel ID {0} added to configuration.'.format(channel))
def getsalt(datadir):
datadir = os.path.expanduser(datadir)
try:
fd = open(datadir+'salt', 'rb')
salt = fd.read()
fd.close()
return salt
except FileNotFoundError:
salt = pysodium.randombytes(32)
with open(datadir+'salt','wb') as fd:
os.fchmod(fd.fileno(),0o600)
fd.write(salt)
return salt
def savesecretekey(self, ext, key):
fname = get_sk_filename(self.basedir, self.name, ext)
k = pbp.getkey(nacl.crypto_secretbox_KEYBYTES,
empty=True,
text='Master' if ext == 'mk' else 'Subkey')
nonce = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES)
with open(fname,'w') as fd:
fd.write(nonce)
fd.write(nacl.crypto_secretbox(key, nonce, k))
def random_branch():
return base64.b32encode(pysodium.randombytes(10))\
.decode('utf-8').lower()
# outgoing keys.
# only do this directly after receiving a packet, not on later sends
# without receiving any acks before, we reset peer_pub to signal, that
# an incoming request has been already once processed like this.
self.e_in = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
self.in_prev = self.in_k
self.in_k = nacl.crypto_scalarmult_curve25519(self.e_in, self.peer_pub)
self.peer_pub = ('\0' * nacl.crypto_scalarmult_curve25519_BYTES)
# generate e_out
self.e_out = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
elif self.out_k == ('\0' * nacl.crypto_secretbox_KEYBYTES):
# only for the very first packet necessary
# we explicitly need to generate e_out
self.e_out = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
#else: # axolotlize
# print 'axolotl!'
# self.out_k = nacl.crypto_generichash(self.out_k,
# nacl.crypto_scalarmult_curve25519(self.me_id.cs, self.peer_id.cp),
# nacl.crypto_scalarmult_curve25519_BYTES)
# compose packet
dh1 = nacl.crypto_scalarmult_curve25519_base(self.e_out)
dh2 = (nacl.crypto_scalarmult_curve25519_base(self.e_in)
if self.e_in != ('\0' * nacl.crypto_scalarmult_curve25519_BYTES)
else ('\0' * nacl.crypto_scalarmult_curve25519_BYTES))
plain = ''.join((dh1, dh2, plain))
# encrypt the whole packet
return self.encrypt(plain)