//     telegram.link
//     Copyright 2014 Enrico Stara 'enrico.stara@gmail.com'
//     Released under the MIT License
//     https://github.com/enricostara/telegram-mt-node

//     AuthKey class
//
// This class represents the Authentication Key

require('requirish')._(module);
var utility = require('lib/utility');
var security = require('lib/security');
var tl = require('telegram-tl-node');

function AuthKey(id, value) {
    this.id = id;
    this.value = value;
}

AuthKey.prototype.toString = function () {
    return tl.utility.toPrintable.apply(this, Array.prototype.slice.call(arguments));
};

AuthKey.prototype.derivateAesKey = function (msgKey, fromServer) {
    var aKey = this.value;
    var x = fromServer ? 8 : 0;
    var sha1A = utility.createSHAHash(Buffer.concat([msgKey, aKey.slice(x, x + 32)], 48));
    var sha1B = utility.createSHAHash(Buffer.concat([aKey.slice(x + 32, x + 48), msgKey, aKey.slice(x + 48, x + 64)], 48));
    var sha1C = utility.createSHAHash(Buffer.concat([aKey.slice(x + 64, x + 96), msgKey], 48));
    var sha1D = utility.createSHAHash(Buffer.concat([msgKey, aKey.slice(x + 96, x + 128)], 48));
    return {
        key: Buffer.concat([sha1A.slice(0, 8), sha1B.slice(8, 20), sha1C.slice(4, 16)], 32),
        iv: Buffer.concat([sha1A.slice(8, 20), sha1B.slice(0, 8), sha1C.slice(16, 20), sha1D.slice(0, 8)], 32),
        toPrintable: tl.utility.toPrintable
    };
};

// Encrypt authKey with the given password
AuthKey.encryptAuthKey = function (authKey, password) {
    var plainKey = Buffer.concat([authKey.id, authKey.value]);
    var passwordHash = utility.createSHAHash(new Buffer(password), 'sha512');
    var aesKey = passwordHash.slice(0, 32);
    var aesIv = passwordHash.slice(32, 64);
    return security.cipher.aesEncrypt(plainKey, aesKey, aesIv).slice(0);
};

// Decrypt authKey with the given password
AuthKey.decryptAuthKey = function (buffer, password) {
    var passwordHash = utility.createSHAHash(new Buffer(password), 'sha512');
    var aesKey = passwordHash.slice(0, 32);
    var aesIv = passwordHash.slice(32, 64);
    var decrypted = security.cipher.aesDecrypt(buffer, aesKey, aesIv);
    return new AuthKey(decrypted.slice(0, 8), decrypted.slice(8, 264));
};

// Export the class
module.exports = exports = AuthKey;

@@ -1401,24 +1401,128 @@
 %0A    %7D;%0A%7D;%0A%0A
+AuthKey.prototype.encrypt = function (password) %7B%0A   return AuthKey.encryptAuthKey(this, password);%0A%7D;%0A%0A
 // Encrypt a

Add the 'encrypt' instance method to AuthKey