<commit_before>var cipherSafe = angular.module("ciphersafe", ["ionic", "firebase"]);
var fb = new Firebase("https://amber-inferno-4704.firebaseio.com/");;

cipherSafe.run(function($ionicPlatform, $state) {
    $ionicPlatform.ready(function() {
        if(window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if(window.StatusBar) {
            StatusBar.styleDefault();
        }
    });
});

cipherSafe.config(function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state("locked", {
            url: "/locked",
            templateUrl: "templates/locked.html",
            controller: "VaultController",
            cache: false
        })
        .state("createvault", {
            url: "/createvault",
            templateUrl: "templates/create_vault.html",
            controller: "VaultController"
        })
        .state("firebase", {
            url: "/firebase",
            templateUrl: "templates/firebase.html",
            controller: "FirebaseController"
        })
        .state("categories", {
            url: "/categories/:masterPassword",
            templateUrl: "templates/categories.html",
            controller: "CategoryController"
        })
        .state("passwords", {
            url: "/passwords/:categoryId/:masterPassword",
            templateUrl: "templates/password_list.html",
            controller: "PasswordController",
            cache: false
        })
        .state("newpassword", {
            url: "/newpassword/:categoryId/:masterPassword",
            templateUrl: "templates/password_new.html",
            controller: "PasswordController"
        })
        .state("viewpassword", {
            url: "/viewpassword/:categoryId/:masterPassword/:passwordId",
            templateUrl: "templates/password_view.html",
            controller: "PasswordController"
        });
    $urlRouterProvider.otherwise('/locked');
});


cipherSafe.controller("VaultController", function($scope, $state, $ionicHistory, $firebaseObject, $firebaseAuth, $cipherFactory) {

    $ionicHistory.nextViewOptions({
        disableAnimate: true,
        disableBack: true
    });

    var fbAuth = fb.getAuth();
    if(fbAuth) {
        var userReference = fb.child("users/" + fbAuth.uid);
        var syncObject = $firebaseObject(userReference);
        syncObject.$bindTo($scope, "data");
    } else {
        $state.go("firebase");
    }

    $scope.unlock = function(masterPassword) {
        syncObject.$loaded().then(function() {
            var decipherPhrase = $cipherFactory.decrypt($scope.data.masterPassword.cipher_text, masterPassword, $scope.data.masterPassword.salt, $scope.data.masterPassword.iv, {output: "hex"});
            if(decipherPhrase === "Authenticated".toHex()) {
                $state.go("categories", {masterPassword: masterPassword});
            }
        });
    }

    $scope.create = function(masterPassword) {
        syncObject.$loaded().then(function() {
            userReference.child("masterPassword").set($cipherFactory.encrypt("Authenticated", masterPassword), function(error) {
                $state.go("locked");
            });
        });
    }

    $scope.reset = function() {
        userReference.remove(function(error) {
            if(error) {
                console.error("ERROR: " + error);
            } else {
                $state.go("createvault");
            }
        });
    }

});

cipherSafe.controller("FirebaseController", function($scope, $state, $ionicHistory, $firebaseAuth) {

    $ionicHistory.nextViewOptions({
        disableAnimate: true,
        disableBack: true
    });

    var fbAuth = $firebaseAuth(fb);

    $scope.login = function(username, password) {
        fbAuth.$authWithPassword({
            email: username,
            password: password
        }).then(function(authData) {
            $state.go("locked");
        }).catch(function(error) {
            console.error("ERROR: " + error);
        });
    }

    $scope.register = function(username, password) {
        fbAuth.$createUser({email: username, password: password}).then(function(userData) {
            return fbAuth.$authWithPassword({
                email: username,
                password: password
            });
        }).then(function(authData) {
            $state.go("createvault");
        }).catch(function(error) {
            console.error("ERROR: " + error);
        });
    }

});

cipherSafe.controller("CategoryController", function($scope, $ionicPopup, $firebaseObject, $stateParams, $cipherFactory) {

    $scope.masterPassword = $stateParams.masterPassword;
    $scope.categories = [];

    var fbAuth = fb.getAuth();
    if(fbAuth) {
        var categoriesReference = fb.child("users/" + fbAuth.uid);
        var syncObject = $firebaseObject(categoriesReference);
        syncObject.$bindTo($scope, "data");
    } else {
        $state.go("firebase");
    }

    $scope.list = function() {
        syncObject.$loaded().then(function() {
            for(var key in $scope.data.categories) {
                if($scope.data.categories.hasOwnProperty(key)) {
                    $scope.categories.push({
                        id: key,
                        category: $cipherFactory.decrypt($scope.data.categories[key].category.cipher_text, $stateParams.masterPassword, $scope.data.categories[key].category.salt, $scope.data.categories[key].category.iv)
                    });
                }
            }
        });
    }

    $scope.add = function() {
        $ionicPopup.prompt({
            title: 'Enter a new category',
            inputType: 'text'
        })
        .then(function(result) {
            if(result !== undefined) {
                if($scope.data.categories === undefined) {
                    $scope.data.categories = {};
                }
                if($scope.data.categories[result.toSHA1()] === undefined) {
                    $scope.data.categories[result.toSHA1()] = {
                        category: $cipherFactory.encrypt(result, $stateParams.masterPassword),
                        passwords: {}
                    };
                    $scope.categories.push({
                        id: result.toSHA1(),
                        category: result
                    });
                }
            } else {
                console.log("Action not completed");
            }
        });
    }

});

cipherSafe.controller("PasswordController", function($scope, $stateParams, $firebaseObject, $state, $cipherFactory, $timeout, $ionicHistory) {

    $scope.masterPassword = $stateParams.masterPassword;
    $scope.categoryId = $stateParams.categoryId;
    $scope.passwords = [];

    var fbAuth = fb.getAuth();
    if(fbAuth) {
        var categoryReference = fb.child("users/" + fbAuth.uid + "/categories/" + $stateParams.categoryId);
        var passwordsReference = fb.child("users/" + fbAuth.uid + "/categories/" + $stateParams.categoryId + "/passwords");
        var syncObject = $firebaseObject(categoryReference);
        syncObject.$bindTo($scope, "data");
    } else {
        $state.go("firebase");
    }

    $scope.list = function() {
        syncObject.$loaded().then(function() {
            var encryptedPasswords = $scope.data.passwords;
            for(var key in encryptedPasswords) {
                if(encryptedPasswords.hasOwnProperty(key)) {
                    $scope.passwords.push({
                        id: key,
                        password: JSON.parse($cipherFactory.decrypt(encryptedPasswords[key].cipher_text, $stateParams.masterPassword, encryptedPasswords[key].salt, encryptedPasswords[key].iv))
                    });<commit_msg>some code cleanup<commit_after>var cipherSafe = angular.module("ciphersafe", ["ionic", "firebase"]);
var fb = new Firebase("https://amber-inferno-4704.firebaseio.com/");

cipherSafe.run(function($ionicPlatform, $state) {
    $ionicPlatform.ready(function() {
        if(window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if(window.StatusBar) {
            StatusBar.styleDefault();
        }
    });
});

cipherSafe.config(function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state("locked", {
            url: "/locked",
            templateUrl: "templates/locked.html",
            controller: "VaultController",
            cache: false
        })
        .state("createvault", {
            url: "/createvault",
            templateUrl: "templates/create_vault.html",
            controller: "VaultController"
        })
        .state("firebase", {
            url: "/firebase",
            templateUrl: "templates/firebase.html",
            controller: "FirebaseController"
        })
        .state("categories", {
            url: "/categories/:masterPassword",
            templateUrl: "templates/categories.html",
            controller: "CategoryController"
        })
        .state("passwords", {
            url: "/passwords/:categoryId/:masterPassword",
            templateUrl: "templates/password_list.html",
            controller: "PasswordController",
            cache: false
        })
        .state("newpassword", {
            url: "/newpassword/:categoryId/:masterPassword",
            templateUrl: "templates/password_new.html",
            controller: "PasswordController"
        })
        .state("viewpassword", {
            url: "/viewpassword/:categoryId/:masterPassword/:passwordId",
            templateUrl: "templates/password_view.html",
            controller: "PasswordController"
        });
    $urlRouterProvider.otherwise('/locked');
});


cipherSafe.controller("VaultController", function($scope, $state, $ionicHistory, $firebaseObject, $cipherFactory) {

    $ionicHistory.nextViewOptions({
        disableAnimate: true,
        disableBack: true
    });

    var fbAuth = fb.getAuth();
    if(fbAuth) {
        var userReference = fb.child("users/" + fbAuth.uid);
        var syncObject = $firebaseObject(userReference);
        syncObject.$bindTo($scope, "data");
    } else {
        $state.go("firebase");
    }

    $scope.unlock = function(masterPassword) {
        syncObject.$loaded().then(function() {
            var decipherPhrase = $cipherFactory.decrypt($scope.data.masterPassword.cipher_text, masterPassword, $scope.data.masterPassword.salt, $scope.data.masterPassword.iv, {output: "hex"});
            if(decipherPhrase === "Authenticated".toHex()) {
                $state.go("categories", {masterPassword: masterPassword});
            }
        });
    }

    $scope.create = function(masterPassword) {
        syncObject.$loaded().then(function() {
            userReference.child("masterPassword").set($cipherFactory.encrypt("Authenticated", masterPassword), function(error) {
                $state.go("locked");
            });
        });
    }

    $scope.reset = function() {
        userReference.remove(function(error) {
            if(error) {
                console.error("ERROR: " + error);
            } else {
                $state.go("createvault");
            }
        });
    }

});

cipherSafe.controller("FirebaseController", function($scope, $state, $ionicHistory, $firebaseAuth) {

    $ionicHistory.nextViewOptions({
        disableAnimate: true,
        disableBack: true
    });

    var fbAuth = $firebaseAuth(fb);

    $scope.login = function(username, password) {
        fbAuth.$authWithPassword({
            email: username,
            password: password
        }).then(function(authData) {
            $state.go("locked");
        }).catch(function(error) {
            console.error("ERROR: " + error);
        });
    }

    $scope.register = function(username, password) {
        fbAuth.$createUser({email: username, password: password}).then(function(userData) {
            return fbAuth.$authWithPassword({
                email: username,
                password: password
            });
        }).then(function(authData) {
            $state.go("createvault");
        }).catch(function(error) {
            console.error("ERROR: " + error);
        });
    }

});

cipherSafe.controller("CategoryController", function($scope, $ionicPopup, $firebaseObject, $stateParams, $cipherFactory) {

    $scope.masterPassword = $stateParams.masterPassword;
    $scope.categories = [];

    var fbAuth = fb.getAuth();
    if(fbAuth) {
        var categoriesReference = fb.child("users/" + fbAuth.uid);
        var syncObject = $firebaseObject(categoriesReference);
        syncObject.$bindTo($scope, "data");
    } else {
        $state.go("firebase");
    }

    $scope.list = function() {
        syncObject.$loaded().then(function() {
            for(var key in $scope.data.categories) {
                if($scope.data.categories.hasOwnProperty(key)) {
                    $scope.categories.push({
                        id: key,
                        category: $cipherFactory.decrypt($scope.data.categories[key].category.cipher_text, $stateParams.masterPassword, $scope.data.categories[key].category.salt, $scope.data.categories[key].category.iv)
                    });
                }
            }
        });
    }

    $scope.add = function() {
        $ionicPopup.prompt({
            title: 'Enter a new category',
            inputType: 'text'
        })
        .then(function(result) {
            if(result !== undefined) {
                if($scope.data.categories === undefined) {
                    $scope.data.categories = {};
                }
                if($scope.data.categories[result.toSHA1()] === undefined) {
                    $scope.data.categories[result.toSHA1()] = {
                        category: $cipherFactory.encrypt(result, $stateParams.masterPassword),
                        passwords: {}
                    };
                    $scope.categories.push({
                        id: result.toSHA1(),
                        category: result
                    });
                }
            } else {
                console.log("Action not completed");
            }
        });
    }

});

cipherSafe.controller("PasswordController", function($scope, $stateParams, $firebaseObject, $state, $cipherFactory, $ionicHistory) {

    $scope.masterPassword = $stateParams.masterPassword;
    $scope.categoryId = $stateParams.categoryId;
    $scope.passwords = [];

    var fbAuth = fb.getAuth();
    if(fbAuth) {
        var categoryReference = fb.child("users/" + fbAuth.uid + "/categories/" + $stateParams.categoryId);
        var passwordsReference = fb.child("users/" + fbAuth.uid + "/categories/" + $stateParams.categoryId + "/passwords");
        var syncObject = $firebaseObject(categoryReference);
        syncObject.$bindTo($scope, "data");
    } else {
        $state.go("firebase");
    }

    $scope.list = function() {
        syncObject.$loaded().then(function() {
            var encryptedPasswords = $scope.data.passwords;
            for(var key in encryptedPasswords) {
                if(encryptedPasswords.hasOwnProperty(key)) {
                    $scope.passwords.push({
                        id: key,
                        password: JSON.parse($cipherFactory.decrypt(encryptedPasswords[key].cipher_text, $stateParams.masterPassword, encryptedPasswords[key].salt, encryptedPasswords[key].iv))
                    });
{'fraction_non_alphanumeric': 0.13940688940688942, 'fraction_numerical': 0.0009065009065009065, 'mean_word_length': 2.0945702264075337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4959392', 'n_tokens_mistral': 4224, 'n_tokens_neox': 4031, 'n_words': 608}