<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
<script src="script.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha512.js"></script>
</head>
<body ng-controller="PruebaController">
Contraseña:<input ng-model="password" />
<br>
El hash de la contraseña es:
<br>
{{getHash()}}
</body>
</html>
var app=angular.module("app",[]);
app.value("algoritmo","SHA-1");
app.factory("hash",['algoritmo',function(algoritmo) {
var hashFunction;
if (algoritmo==="MD5") {
hashFunction=CryptoJS.MD5;
} else if (algoritmo==="SHA-1") {
hashFunction=CryptoJS.SHA1;
} else if (algoritmo==="SHA-2-256") {
hashFunction=CryptoJS.SHA256;
} else if (algoritmo==="SHA-2-512") {
hashFunction=CryptoJS.SHA512;
} else {
throw Error("El tipo de algoritmo no es vĂ¡lido:"+algoritmo);
}
var hash=function(message) {
var objHashResult=hashFunction(message);
var strHashResult=objHashResult.toString(CryptoJS.enc.Base64);
return strHashResult;
}
return hash;
}]);
app.controller("PruebaController",["$scope","hash",function($scope,hash) {
$scope.password="s3cret";
$scope.getHash=function() {
var hashResult=hash($scope.password);
return hashResult;
}
}]);
Ejemplo complejo de factory