Translating JS crypto.createHmac to Xojo Crypto.HMAC - javascript

I am trying to translate this block in Javascript:
const crypto = require('crypto'),
shared_key = 'kw4qSnpSwXzgiv5yxYpZZmFEd9QAeiKTQ6OuyMja',
signing_string = 'licenseSpring\ndate: Tue, 07 Jun 2011 20:51:35 GMT';
let signature = crypto.createHmac('sha256', shared_key).update(signing_string).digest('base64');
console.log(signature);
// UDysfR6MndUZReo07Y9r+vErn8vSxrnQ5ulit18iJ/Q=
Into Xojo:
Var shared_key as String = "kw4qSnpSwXzgiv5yxYpZZmFEd9QAeiKTQ6OuyMja"
Var signing_string as String = "licenseSpring\ndate: Tue, 07 Jun 2011 20:51:35 GMT"
Var hash As String
hash = EncodeBase64(Crypto.HMAC(shared_key, signing_string, Crypto.HashAlgorithms.SHA256))
MessageBox(hash)
//Q4BAhsu1Xw3LsBZ+BCLShWQDbmJ2j/eFXzvF9T6n9tU=
I am getting two different hashed strings, but expect they should be the same. Are these algorithms equivalent?

It turned out to be this:
Var signing_string as String = "licenseSpring" + EndOfLine.UNIX + "date: Tue, 07 Jun 2011 20:51:35 GMT"

Related

converting data format (Feb 01 2021 10:53:00am KST => 2021-02-01 10:53:00am)

I need to filter datas by it's dates.
In order to do that I have to convert date to Number.
but I am getting this NaN error.
How can I convert
Feb 01 2021 10:53:00am KST
into
2021-02-01 10:53:00
or Is there any way to convert
Feb 01 2021 10:53:00am KST
to Number directly?
Having a look at Date.parse
This one parses in Chrome "Feb 01 2021 10:53:00"
const d = new Date("Feb 01 2021 10:53:00")
console.log(d)
So let's try more specific
// We want YYYY-MO-DDTHH:MM:SS.MSC+hh:mm
const months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
const pad = num => ("0"+num).slice(-2);
const dString = "Feb 01 2021 10:53:00am KST"; // assuming mmm dd yyyy hh:mm:ssxx KST where xx is am or pm
let [mmm,dd,yyyy,tString,tz] = dString.split(" ");
let [_,hh,mm,ss,ampm] = tString.match(/(\d{2}):(\d{2}):(\d{2})(am|pm)/)
hh = pad(+hh + (ampm==="pm" ? 12 : 0));
const month = pad(months.indexOf(mmm)+1)
const newDString = `${yyyy}-${month}-${dd}T${hh}:${mm}:${ss}.000-09:00`
const date = new Date(newDString)
console.log(newDString,date)

Date not parsing correct in Javascript

I'm using timestamp that is inserted into my PostreSQL database & trying to parse it to become user friendly, however I'm getting the wrong year?
function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum/1000;
}
let timestamp = toTimestamp('Sun Jan 19 2020 21:19:40 GMT+0000 (Coordinated Universal Time)');
var d = new Date();
d.setTime(timestamp);
console.log(d.toGMTString()); //Mon, 19 Jan 1970 06:44:28 GMT
I'm expecting a result of Sun, 19 Jan 2020 21:19:40 GMT
Don't divide datum by 1000
see here
function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum;
}
let timestamp = toTimestamp('Sun Jan 19 2020 21:19:40 GMT+0000 (Coordinated Universal Time)');
var d = new Date();
d.setTime(timestamp);
console.log(d.toGMTString()); // Sun, 19 Jan 2020 21:19:40 GMT
It's just a unit of measurement error. Date expects epoch in milliseconds but you are dividing the datum variable by 1000, turning it into seconds. This is resulting in the discrepancy and can be fixed by removing the divide by 1000 step.
toTimestamp then becomes:
function toTimestamp(strDate){
return Date.parse(strDate);
}
use only datum instead of datum/1000 except this your code is working fine
function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum;
//return Date.parse(strDate);
}
let timestamp = toTimestamp('Sun Jan 19 2020 21:19:40 GMT+0000 (Coordinated Universal Time)');
var d = new Date();
d.setTime(timestamp);
console.log(d.toGMTString()); //Mon, 19 Jan 1970 06:44:28 GMT

How to convert the Full UTC time to yyyymmdd format using javascript

here how can i convert the
[Sun Jul 15 2018 17:48:13 GMT+0530 (India Standard Time), Sun Jul 22 2018 17:48:13 GMT+0530 (India Standard Time)]
to 20180715 using javascript
here a variable named DateData is storing the above two dates
DateData:Date[];
After selecting the from the datetime picker i storing the data in the DateData
now i am trying to convert the variable using DateData.toISOstring or DateData.toDate() also not working displaying as unable to convert the dataData to Date Format
Perhaps something like this
function sqlDate(dat)
{
return dat.toISOString().substr(0,10).replace('-','');
}
There you go. :)
function convertDates(dateArr) {
var newDateArray = [],
dateObj;
for (var i=0; i<dateArr.length; i++) {
dateObj = new Date(dateArr[i]);
newDateArray.push(dateObj.getFullYear() + '' + dateObj.getMonth() + '' + dateObj.getDate());
}
return newDateArray;
}
var dateArr = ['Sun Jul 15 2018 17:48:13 GMT+0530 (India Standard Time)', 'Sun Jul 22 2018 17:48:13 GMT+0530 (India Standard Time)'];
console.log(convertDates(dateArr));
function formatDate(date){
if(date instanceof Date) date = new Date(date);
var fullZero = s => s.toString().length ===1 ?'0'+s :s.toString();
return `${date.getFullYear()}${fullZero(date.getMonth()+1)}${fullZero(date.getDate())}`;
}
formatDate(new Date) // -> "20180722"
If you have an array of Date, use dateData.map(formatDate) to format it.
Try splitting on the "T" like this snippet:
var d = new Date();
// => d.toISOString() show 2019-02-01T06:38:21.990Z
console.log(d.toISOString().split("T")[0].replace(/-/g, ''));
or you can use a JavaScript library called date-and-time for the purpose.
let now = date.format(new Date(), 'YYYYMMDD');
console.log(now);
<script src="https://cdn.jsdelivr.net/npm/date-and-time/date-and-time.min.js"></script>

Timestamp Splitting incorrect - Javascript

I've a Timestamp loaded from MSSQL into a Webclient.
The Timestamp is: Thu Jan 01 18:00:00 CET 1970,
but i need only the: 18:00 or 18:00:00 (first one is the better one..)
<script>
var timestamp = "{{SYSTIMESTAMP}}";
var time1 = timestamp.split("T")[1].split(".")[0];
</script>
The HTMLcode for the function is:
<span class="systime"><script type="text/javascript">document.write(time1)</script></span>
The output is:
hu Jan 01 18:00:00 CE
--> I need 18:00 or 18:00:00
The Timestamp is: Thu Jan 01 18:00:00 CET 1970
Assuming you have that as a string, client side:
var str = "Thu Jan 01 18:00:00 CET 1970";
The simplest way to get "18:00" from it is a simple regular expression:
var m = /(\d{2}:\d{2}):\d{2}/.exec(str);
var result = m && m[1];
You said just "18:00" was better, but if you wanted "18:00:00", just move the closing ) to the end, just before the / that ends the regular expression.
Live example:
var str = "Thu Jan 01 18:00:00 CET 1970";
var m = /(\d{2}:\d{2}):\d{2}/.exec(str);
var result = m && m[1];
snippet.log("Result: '" + result + "'");
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Extract specific string and change string format (jquery/ javascript)

I want to extract "10:30" from mystring and convert it to "10, 30".
var mystring = "Sat Dec 17 2011 10:30:00 GMT+0530 (India Standard Time)";
Output will be:
Time: 10:30
Changed Format: 10, 30
Provide code solution only.
You can use this to extract the time:
var mystring = "Sat Dec 17 2011 10:30:00 GMT+0530 (India Standard Time)";
var match = mystring.match(/(\d+:\d+):\d+/);
if (match) {
var output = "Time: " + match[1].replace(":", ", ");
}

Categories

Resources