sha 256 Encrypt code (from document)
function hmacEncrypt(r) {
let str = r.uri.split("/", 2)
return require('crypto').createHash('sha256', 'key')
.update(str)
.digest('base64url');
}
I want to use njs to decode aes256, base64 encoded string.
However, the official njs document only shows the encrypt method.
Is there a way to decode or encode aes256? Can I help you?
For the aes256, you can do something like:
const crypto = require("crypto");
const initVector = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
]);
function encryptString(string, securityKey) {
const cipher = crypto.createCipheriv(
'aes-256-cbc',
securityKey,
initVector
);
cipher.update(string, 'utf-8', 'hex');
return cipher.final('hex');
}
function decryptString(string, securityKey) {
const decipher = crypto.createDecipheriv(
'aes-256-cbc',
securityKey,
initVector
);
decipher.update(string, 'hex', 'utf-8');
return decipher.final('utf-8');
}
The initVector is to provide the initial state of the algorithm, you can change it to whatever you want but it should be an array of exactly 16 bytes, then just simply use those functions:
const securityKey = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32
]);
const text = 'example string';
const encrypted = encryptString(text, securityKey);
const decrypted = decryptString(encrypted, securityKey);
console.log(encrypted);
console.log(decrypted);
The securityKey key is the password that will be used to encrypt and decrypt the string, it should be an array of exactly 32 bytes, change it to whatever you want!
Related
I have tried this exercise for a few sets e.g. {2, 3, 5}, {5, 11} where xor of elements is not 0. My intuition suggests that it will always be non-zero but I am unable to prove it. I searched on the net but didn't find anything. Any help will be appreciated.
Condensed from comments to your question:
For two distinct primes, bit-wise XOR will not be zero. In general, XOR of two numbers is zero if and only if the two numbers are equal.
For three distinct primes, from considering the least significant bit only, we see that not all three primes can be odd numbers. But there exists only one even prime, namely 2. Then the remaining primes, both odd, must be identical on every bit except the second-least significant bit where they must be different. They are therefore twin primes of the form 4k+1 and 4k+3 (note that twin primes of the form 4k-1 and 4k+1 do not satisfy the requirement). So the solutions for three primes are { 2, 5, 7 }; { 2, 17, 19 }; { 2, 29, 31 }; { 2, 41, 43 }; ....
For four primes, there are simply so many ways it can happen. From a simple program listing all occurrences with all primes under 50, I get: { 3, 5, 11, 13 }; { 5, 7, 17, 19 }; { 3, 5, 17, 23 }; { 11, 13, 17, 23 }; { 3, 7, 19, 23 }; { 7, 11, 17, 29 }; { 5, 11, 19, 29 }; { 3, 13, 19, 29 }; { 7, 13, 23, 29 }; { 5, 11, 17, 31 }; { 3, 13, 17, 31 }; { 7, 11, 19, 31 }; { 3, 11, 23, 31 }; { 5, 13, 23, 31 }; { 5, 7, 29, 31 }; { 17, 19, 29, 31 }; { 7, 11, 37, 41 }; { 17, 29, 37, 41 }; { 19, 31, 37, 41 }; { 5, 11, 37, 43 }; { 3, 13, 37, 43 }; { 19, 29, 37, 43 }; { 17, 31, 37, 43 }; { 5, 7, 41, 43 }; { 17, 19, 41, 43 }; { 29, 31, 41, 43 }; { 7, 13, 37, 47 }; { 23, 29, 37, 47 }; { 3, 5, 41, 47 }; { 11, 13, 41, 47 }; { 17, 23, 41, 47 }; { 3, 7, 43, 47 }; { 19, 23, 43, 47 }; ....
For five primes, again not all can be odd, so one has to be 2. But there are still a lot of ways it can happen (based on brute force search).
Not sure if this helps your intuition.
For example in for this highchart:
https://www.highcharts.com/stock/demo/basic-line
I am trying to web-scrape this information
Each dropdown has information that i need to collect for analysis. Currently i am trying to use the Requests package in python without much success
Would love to hear any advice!
Ok, use requests.get to make a get request, then use .json method to parse response as a json, then optionally convert timestamps (given in ms, divide by 1000 to get seconds) into datetime objects like this:
import requests
from datetime import datetime
from pprint import pprint
def get_stock_prices(symbol: str) -> list:
symbol = symbol.lower()
url = f'https://www.highcharts.com/samples/data/{symbol}-c.json'
res = requests.get(url)
res.raise_for_status()
prices_raw = res.json()
return [[datetime.fromtimestamp(t / 1000), price]
for t, price in prices_raw]
symbol = 'AAPL'
stocks = get_stock_prices(symbol)
pprint(stocks)
output:
[[datetime.datetime(2017, 7, 17, 16, 30), 149.56],
[datetime.datetime(2017, 7, 18, 16, 30), 150.08],
[datetime.datetime(2017, 7, 19, 16, 30), 151.02],
[datetime.datetime(2017, 7, 20, 16, 30), 150.34],
[datetime.datetime(2017, 7, 21, 16, 30), 150.27],
[datetime.datetime(2017, 7, 24, 16, 30), 152.09],
[datetime.datetime(2017, 7, 25, 16, 30), 152.74],
[datetime.datetime(2017, 7, 26, 16, 30), 153.46],
In python3.4, if I create a timezone aware datetime objet, how do I determine whether the given date is summer (dst) or winter time?
Example:
local_time = pytz.timezone('Europe/Berlin')
time_winter = datetime.datetime(2014, 11, 26, 10, tzinfo=local_time)
time_summer = datetime.datetime(2014, 7, 26, 10, tzinfo=local_time)
In both cases .dst() returns off:
>>> datetime.timedelta(0)
Also .tzname() and .tzinfo() are always the same.
In principle, the object is aware of the timezone and dst, but only sometimes:
cet_winter = pytz.timezone('CET') # CET is without dst
datetime.datetime(2014,7 , 26, 10, tzinfo=local_time).astimezone(cet_winter)
>>> datetime.datetime(2014, 7, 26, 11, 0, tzinfo=<DstTzInfo 'CET' CEST+2:00:00 DST>)
datetime.datetime(2014,11, 26, 10, tzinfo=local_time).astimezone(cet_winter)
>>> datetime.datetime(2014, 11, 26, 10, 0, tzinfo=<DstTzInfo 'CET' CET+1:00:00 STD>)
Here it shows a difference between summer and winter time...
Doing the same to UTC, it won't work...
datetime.datetime(2014,11, 26, 10, tzinfo=local_time).astimezone(pytz.timezone('UTC'))
>>> datetime.datetime(2014, 11, 26, 9, 0, tzinfo=<UTC>)
datetime.datetime(2014,11, 26, 10, tzinfo=local_time).astimezone(pytz.timezone('UTC'))
>>> datetime.datetime(2014, 11, 26, 9, 0, tzinfo=<UTC>)
Do I miss something fundamentally or do I need to make the timezone object time dependent?
You need to use localize on the timezone object:
>>> local_time.localize(datetime.datetime(2014, 11, 26, 10)).dst()
datetime.timedelta(0)
>>> local_time.localize(datetime.datetime(2014, 7, 26, 10)).dst()
datetime.timedelta(0, 3600)
Both .localize() and .normalize() are used to ensure conversion is done correctly and takes DST into account (see examples).
I am using ASP.NET MVC 4 with aspx.
I want from my controller to produce code at my view.
Therefore I have a line at my controller which is
ViewBag.Chart = "var c = r.barchart(10, 10, 600, 440, [[55, 20, 13, 32, 5, 1, 2, 10], [10, 2, 1, 5, 32, 13, 20, 55]], { stacked: true, type: \"soft\" }).hoverColumn(fin2, fout2);";
However, when it shows at my code, it does not shows the character (") but instead it replaces it with a & quot;
What I can do in order to produce the character " at the code?
You need to use %= instead of %:
<%= ViewBag.Chart %>
if you are using %: it is automatically HTML encodes your string while the %= does not.
Sidenote: it is not a good practice to emit JavaScript from your controller into your view...
Hi try placing \ in front of the quote and before closing the quotes. It works fine for me
\"var c = r.barchart(10, 10, 600, 440, [[55, 20, 13, 32, 5, 1, 2, 10], [10, 2, 1, 5, 32, 13, 20, 55]], { stacked: true, type: \"soft\" }).hoverColumn(fin2, fout2);\";
I am using the graphaeljs library to display charts, at an ASP.NET MVC 4
I have copied this code from the graphaeljs website
<script src="scripts/raphael.js"></script>
<script src="scripts/g.raphael-min.js"></script>
<script src="scripts/g.bar-min.js"></script>
<script>
window.onload = function () {
var r = Raphael("holder"),
fin = function () {
this.flag = r.popup(this.bar.x, this.bar.y, this.bar.value || "0").insertBefore(this);
},
fout = function () {
this.flag.animate({ opacity: 0 }, 300, function () { this.remove(); });
},
fin2 = function () {
var y = [], res = [];
for (var i = this.bars.length; i--;) {
y.push(this.bars[i].y);
res.push(this.bars[i].value || "0");
}
this.flag = r.popup(this.bars[0].x, Math.min.apply(Math, y), res.join(", ")).insertBefore(this);
},
fout2 = function () {
this.flag.animate({ opacity: 0 }, 300, function () { this.remove(); });
},
txtattr = { font: "12px sans-serif" };
r.text(160, 10, "Single Series Chart").attr(txtattr);
r.text(480, 10, "Multiline Series Stacked Chart").attr(txtattr);
r.text(160, 250, "Multiple Series Chart").attr(txtattr);
r.text(480, 250, "Multiline Series Stacked Chart\nColumn Hover").attr(txtattr);
r.barchart(10, 10, 300, 220, [[55, 20, 13, 32, 5, 1, 2, 10]]).hover(fin, fout);
r.hbarchart(330, 10, 300, 220, [[55, 20, 13, 32, 5, 1, 2, 10], [10, 2, 1, 5, 32, 13, 20, 55]], { stacked: true }).hover(fin, fout);
r.hbarchart(10, 250, 300, 220, [[55, 20, 13, 32, 5, 1, 2, 10], [10, 2, 1, 5, 32, 13, 20, 55]]).hover(fin, fout);
var c = r.barchart(330, 250, 300, 220, [[55, 20, 13, 32, 5, 1, 2, 10], [10, 2, 1, 5, 32, 13, 20, 55]], { stacked: true, type: "soft" }).hoverColumn(fin2, fout2);
};
</script>
But when I run at my browser, this message appears
Uncaught TypeError: Cannot read property 'x' of undefined raphael.js:11
c._engine.create raphael.js:11
c raphael.js:9
window.onload (index):92
I do not know what I do wrong, and I used the libraries exactly as they are. Can you please tell me, what might be my mistake?
Sorry, I don't see anything wrong with the code you posted.
I made a fiddle for you.
It is obviously an issue with the raphael.js file.
Be sure to include class="raphael" on your body tag, and a div with id="holder".
If that fails, try re-downloading raphael.js.