/// <summary> /// Constructor of Session class for a new User /// </summary> /// <param name="username">Username of the user</param> /// <param name="password">Password of the user</param> /// <param name="localObjectStorage">LocalObjectStorage</param> internal Session(string username, string password, LocalObjectStorage localObjectStorage) { LocalObjectStorage = localObjectStorage; rsaKeyPair = Crypto.DeriveRsaKey(Encoding.UTF8.GetBytes(username), Encoding.UTF8.GetBytes(password)); KademliaId id = Crypto.Hash(rsaKeyPair.Public); Username = username; User = localObjectStorage.GetObject(id) as User; if (User == null) { User = new User(rsaKeyPair.Public) {ObjectId = id}; localObjectStorage.StoreObject(User, true); } onUserUpdated = UpdateUser; localObjectStorage.OnUserUpdated += onUserUpdated; }
/// <summary> /// Generates a signature for the data /// </summary> /// <param name="data">Data to sign</param> /// <returns>The signature of the hashed data</returns> internal static byte[] Sign(byte[] data, RsaKeyPair keyPair) { return DecryptRsa(Hash(data), keyPair); }
/// <summary> /// Decrypts data with an RSA key pair /// </summary> /// <param name="data">Data to decrypt</param> /// <param name="keyPair">RSA Key pair to use for decryption</param> /// <returns>The decrypted data</returns> internal static byte[] DecryptRsa(byte[] data, RsaKeyPair keyPair) { var c = new BigInt(data); var d = new BigInt(keyPair.Private); var n = new BigInt(keyPair.Public); return c.PowerMod(d, n).ToByteArray(); }