kopia lustrzana https://github.com/c9/core
Revert "Revert another rotate ssso"
rodzic
1dad6c0950
commit
497893335a
|
@ -1,22 +1,35 @@
|
|||
var crypto = require("crypto");
|
||||
|
||||
exports.crypt = function(sessionId, secret) {
|
||||
secret = [].concat(secret);
|
||||
secret = secret[1] || secret[0];
|
||||
|
||||
var cipher = crypto.createCipher("aes256", secret);
|
||||
|
||||
return (
|
||||
cipher.update(sessionId, "ascii", "base64") +
|
||||
cipher.update(sessionId, "ascii", "base64") +
|
||||
cipher.final("base64")
|
||||
).replace(/\=+$/, "");
|
||||
};
|
||||
|
||||
exports.decrypt = function(encrypted, secret) {
|
||||
var cipher = crypto.createDecipher("aes256", secret);
|
||||
|
||||
var data = [
|
||||
cipher.update(encrypted, "base64", "ascii"),
|
||||
cipher.final("ascii")
|
||||
];
|
||||
var secrets = [].concat(secret);
|
||||
|
||||
return data.join("").replace(/\=+$/, "");
|
||||
var data;
|
||||
|
||||
for (var i = 0; i < secrets.length; i++) {
|
||||
secret = secrets[i];
|
||||
var cipher = crypto.createDecipher("aes256", secret);
|
||||
|
||||
try {
|
||||
data = [
|
||||
cipher.update(encrypted, "base64", "ascii"),
|
||||
cipher.final("ascii")
|
||||
];
|
||||
}
|
||||
catch (err) { /** ignore failed decrypt **/ }
|
||||
if (data) return data.join("").replace(/\=+$/, "");
|
||||
}
|
||||
};
|
||||
|
||||
exports.uid = function(length) {
|
||||
|
|
|
@ -1,25 +1,53 @@
|
|||
"use strict";
|
||||
|
||||
"use server";
|
||||
|
||||
require("c9/inline-mocha")(module);
|
||||
|
||||
var assert = require("assert");
|
||||
var crypt = require("./crypt");
|
||||
|
||||
module.exports = {
|
||||
|
||||
name: "cookie crypt",
|
||||
|
||||
"test encrpyt and decrypt should return input" : function() {
|
||||
describe("c9/crypt", function() {
|
||||
it("encrpyt and decrypt should return input", function() {
|
||||
var sessionId = "vOcRVvhaBBauiYexVvWyJpPb.AqmabaXkhpmlR8AUkORJHu%2FB7WA57EsDqzled0VoKAg";
|
||||
var secret = "geheim";
|
||||
|
||||
|
||||
assert.equal(crypt.crypt(sessionId, secret), "C+kRJ4UWhmjgqo7DVv31cJLfZ9LIPLZB7OuMdN8i07ZdZHKlusCClMKaqBEwHDiKH3uFKf8IUZOxoVHU6+eNrkLArr32HrBDLr8qfnKfAgY");
|
||||
|
||||
|
||||
assert.equal(
|
||||
crypt.decrypt(crypt.crypt(sessionId, secret), secret),
|
||||
sessionId
|
||||
);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
!module.parent && require("asyncjs").test.testcase(module.exports).exec();
|
||||
|
||||
it("Should accept an array for decrypt", function() {
|
||||
var message = Math.random().toString(36);
|
||||
var secret = "swordfish";
|
||||
|
||||
var crypted = crypt.crypt(message, secret);
|
||||
var plaintext = crypt.decrypt(crypted, [Math.random().toString(36), Math.random().toString(36), secret]);
|
||||
|
||||
assert.equal(plaintext, message, "Got message back");
|
||||
});
|
||||
|
||||
it("Should accept an array for crypt", function() {
|
||||
var message = Math.random().toString(36);
|
||||
var secret = ["letmein", "swordfish"];
|
||||
|
||||
var crypted = crypt.crypt(message, secret);
|
||||
var plaintext = crypt.decrypt(crypted, secret);
|
||||
|
||||
assert.equal(plaintext, message, "Got message back");
|
||||
});
|
||||
|
||||
it("Should prefer the #1st secret", function() {
|
||||
var message = Math.random().toString(36);
|
||||
var secret = ["letmein", "swordfish", "princess"];
|
||||
|
||||
var crypted = crypt.crypt(message, secret);
|
||||
|
||||
assert.ok(!crypt.decrypt(crypted, secret[0]), "String was not encrypted with the #1st secret");
|
||||
assert.ok(!crypt.decrypt(crypted, secret[2]), "String was not encrypted with the #3rd secret");
|
||||
assert.equal(crypt.decrypt(crypted, secret[1]), message, "Got message back");
|
||||
});
|
||||
});
|
||||
|
|
Ładowanie…
Reference in New Issue