HMAC-MD5 using openSSL - c

I'm trying to study NTLMv2 response, Eric Glass's work.
but stuck at HMAC-MD5 section. (using function like this)
I got the right NTLM hash, and unicode username & domain.
(he only emphasize the "USERDOMAIN" but no username, so I suppose it's "USER")
unsigned char v1hash[16]; // "0xcd06ca7c7e10c99b1d33b7485a2ed808"
unsigned short udata[14];
// concated unicode USER+USERDOMAIN "0x550053004500520044004f004d00410049004e00"
unsigned char v2hash[16];
int iLen;
HMAC(EVP_md5(), v1hash, 16, udata, sizeof(udata), v2hash, &iLen);
but the result is:
v2hash(16): 23 d2 3c a4 dd 1a 20 81 35 cf 3a 42 1c e1 5a 17
which should be "0x04b8e0ba74289cc540826bab1dee63ae"
am I doing something wrong here?

I'm not a C programmer but...
From my reading of that article USERDOMAIN should be just that - NOT USER+USERDOMAIN ie the uppercase username = "USER" concat with the target = "DOMAIN" to give USERDOMAIN which gives the unicode bytes beginning 55005300...
Then it would appear your udata array is the wrong size. You're only processing 20 bytes of information so don't set the array to 28 otherwise the HMAC/MD5 functions will be processing extra random data at the end of the input array.

Related

Is it safe to use the first 22 characters of a NFT pubkey as a primary key for a DB

I am wondering if is safe to only use the first 22 characters instead of the 44 characters of a pubkey of an NFT as a primary key of a MySQL DB. I have a DB with huge data and could save a lot of space thanks to this approach. For instance having the following pubkey:
AQoKYV7tYpTrFZN6P5oUufbQKAUr9mNYGe1TTJC9wajM
Would it be safer to use the first 22 characters:
AQoKYV7tYpTrFZN6P5oUuf
Would it be safer using the first 11chars plus the trailing 11chars, or doesn't make any difference?
AQoKYV7tYpTe1TTJC9wajM
A public key is 32 bytes, so those "44 characters" are actually the base-58 representation of those 32 bytes.
If you're only storing 22 characters, let's simplify things and say that you're storing 16 bytes out of 32 total. The chance of two pubkeys sharing the same 16-byte sequence is 1 / 256^16 = 1 / 2^128 = 2.9 * 10 ^ -39, which is very unlikely, but possible.
Here's another way to approach the problem -- how about storing the full pubkey as 32 bytes instead of as a string? Then you won't ever lose any precision.

Using arrays in cobol to loop through clients and booking type

Trying to solve this using arrays.
Here's the problem:
Problem I'm facing: Has to be an easier way to loop through the booking types. When I view the output, it shows the customer number, customer name, and address several times with the same input which it shouldn't be.
Any help would be appreciated.
Here is the program below:
Process Apost.
Identification Division.
Program-ID. BOOKINGARR.
*
* Page 554 No 3. ARRAYS.
* Data in sequence by Client No. Print the average cost of
* trip for each booking type. Use arrays.
*
Environment Division.
Configuration Section.
Source-Computer. IBM-AS400.
Object-Computer. IBM-AS400.
Input-Output Section.
File-Control.
Select Input-File Assign to Database-Bookingpf.
Select Output-File Assign to Printer-Qsysprt.
Data Division.
File Section.
FD Input-File.
01 Input-File-Rec.
Copy DDS-BookingR of Bookingpf.
FD Output-File.
01 Output-File-Rec Pic x(120).
Working-Storage Section.
01 END-OF-FILE PIC X VALUE 'N'.
01 WS-ARRAY.
05 WS-TABLE-ENTRIES OCCURS 4 TIMES.
10 WS-TOTAL-COST PIC 9(7)V99.
10 WS-TRIP-COUNT PIC 999.
10 WS-AVG-COST PIC 9(7)V99 VALUE ZERO.
10 WS-BOOKING-TYPE PIC 9.
01 ARRAY-INDEX PIC 99.
01 EMPTY-POINTER PIC 99.
01 ARRAY-EMPTY PIC XXX.
01 PROGRAM-HEADER.
05 PIC X(2) VALUE SPACES.
05 PIC X(10) VALUE 'CLIENT NO.'.
05 PIC X(3) VALUE SPACES.
05 PIC X(11) VALUE 'CLIENT NAME'.
05 PIC X(6) VALUE SPACES.
05 PIC X(14) VALUE 'CLIENT ADDRESS'.
05 PIC X(4) VALUE SPACES.
05 PIC X(9) VALUE 'BOOK TYPE'.
05 PIC X(4) VALUE SPACES.
05 PIC X(12) VALUE 'AVERAGE COST'.
01 REPORT-LINE.
05 PIC X(2) VALUE SPACES.
05 CLIENTNO-OUT PIC 999.
05 PIC X(10) VALUE SPACES.
05 CLIENTNA-OUT PIC X(16).
05 PIC X(1) VALUE SPACES.
05 CLIENTADD-OUT PIC X(19).
05 PIC X(3) VALUE SPACES.
05 BOOKTYPE-OUT PIC Z.
05 PIC X(8) VALUE SPACES.
05 AVGCOST-OUT PIC $Z,ZZ9.99.
05 PIC X(12) VALUE SPACES.
Procedure Division.
000-MAIN.
OPEN INPUT INPUT-FILE
OUTPUT OUTPUT-FILE.
PERFORM 100-MOVE.
PERFORM 1000-READ.
PERFORM 300-UPDATE-BOOKINGS
UNTIL END-OF-FILE = 'Y'.
WRITE OUTPUT-FILE-REC FROM PROGRAM-HEADER.
PERFORM 600-WRITE-TO-SCREEN
VARYING ARRAY-INDEX FROM 1 BY 1
UNTIL ARRAY-INDEX > 4.
CLOSE INPUT-FILE, OUTPUT-FILE.
STOP RUN.
100-MOVE.
MOVE 1 TO EMPTY-POINTER.
MOVE 'Y' TO ARRAY-EMPTY.
MOVE SPACES TO UPDATE-DONE.
PERFORM 150-ZERO-OUT-ARRAY
VARYING ARRAY-INDEX FROM 1 BY 1 UNTIL
ARRAY-INDEX > 4.
150-ZERO-OUT-ARRAY.
MOVE ZEROS TO WS-BOOKING-TYPE (ARRAY-INDEX).
MOVE ZEROS TO WS-TOTAL-COST (ARRAY-INDEX).
MOVE ZEROS TO WS-TRIP-COUNT (ARRAY-INDEX).
MOVE ZEROS TO WS-AVG-COST (ARRAY-INDEX).
1000-READ.
READ INPUT-FILE AT END MOVE 'Y' TO END-OF-FILE.
300-UPDATE-BOOKINGS.
IF ARRAY-EMPTY = 'Y'
PERFORM 400-ADD-1-TO-COUNT
MOVE 'N' TO ARRAY-EMPTY
ELSE
MOVE 'N' TO UPDATE-DONE
PERFORM 500-GET-BOOKING-AVERAGE
VARYING ARRAY-INDEX FROM 1 BY 1
UNTIL ARRAY-INDEX = EMPTY-POINTER
OR
UPDATE-DONE = 'Y'.
IF UPDATE-DONE = 'N'
PERFORM 400-ADD-1-TO-COUNT.
PERFORM 1000-READ.
400-ADD-1-TO-COUNT.
MOVE BOOKTYPE TO WS-BOOKING-TYPE (EMPTY-POINTER).
ADD 1 TO WS-TRIP-COUNT (EMPTY-POINTER).
MOVE COSTOFTRIP TO WS-TOTAL-COST (EMPTY-POINTER).
MOVE COSTOFTRIP TO WS-AVG-COST (EMPTY-POINTER).
ADD 1 TO EMPTY-POINTER.
500-GET-BOOKING-AVERAGE.
IF BOOKTYPE = WS-BOOKING-TYPE (ARRAY-INDEX)
ADD 1 TO WS-TRIP-COUNT (ARRAY-INDEX)
ADD COSTOFTRIP TO WS-TOTAL-COST (ARRAY-INDEX)
COMPUTE WS-AVG-COST (ARRAY-INDEX) =
WS-TOTAL-COST (ARRAY-INDEX) /
WS-TRIP-COUNT (ARRAY-INDEX)
MOVE 'Y' TO UPDATE-DONE.
600-WRITE-TO-SCREEN.
MOVE CLIENTNO TO CLIENTNO-OUT.
MOVE CLIENTNA TO CLIENTNA-OUT.
MOVE CLIENTADD TO CLIENTADD-OUT.
MOVE WS-BOOKING-TYPE (ARRAY-INDEX) TO BOOKTYPE-OUT.
MOVE WS-AVG-COST (ARRAY-INDEX) TO AVGCOST-OUT.
WRITE OUTPUT-FILE-REC FROM REPORT-LINE
AFTER ADVANCING 1 LINE.
I see you are using Stern & Stern.
The objective:
Print the average cost of a trip for each booking type. Use arrays.
means the output should contain only two columns and four rows, plus any header. For example,
Booking Type Average Cost
------------ ------------
Cruise ZZZZ9.99
Air-Independent ZZZZ9.99
Air-Tour ZZZZ9.99
Other ZZZZ9.99
To achieve that you will need to place the four descriptions in an array and accumulate the total cost and count, for each booking type, also in an array. After processing all the records, calculate the averages and print the results while looping through the array(s).
You tried to do much more than was requested!
The reason behind numbering COBOL paragraphs is so they are easier to find. In a program several thousand lines long it saves significant effort for the maintainer. You have located paragraph 1000 between paragraphs 150 and 300.
Modern COBOL programs usually have full stops at the end of a paragraph or section name and the end of a paragraph or section. Some people make the last line of a paragraph or section a CONTINUE or EXIT statement with a full stop instead of just a full stop on its own.
Modern COBOL programs use explicit scope terminators, particularly on IF statements, instead of full stops.
COBOL is criticized for being verbose; many COBOL programmers make this into a virtue by naming their paragraphs to indicate what is being done by the code contained therein. For example, 100-MOVE might be better named 100-INITIALIZE.
On Stack Overflow, I suggest you familiarize yourself with the "code sample" button, the "{}" rather than marking your paragraphs in bold.
If the point of the exercise is to compute the average cost of each type of booking, I suggest using the booking type as a subscript into an array. For each record you read, add the COSTOFTRIP to WS-TOTAL-COST(BOOKTYPE) and increment WS-TRIP-COUNT(BOOKTYPE). At end of file compute the average cost of a trip for each BOOKTYPE by using a COMPUTE for each element of the array inside a PERFORM VARYING loop.
For extra credit, verify BOOKTYPE is numeric before using it as a subscript and use an in-line PERFORM to compute the average.

printf not working after executing external function

I have the following c code which calls a cobol program:
#include <stdio.h>
#include "libcob.h"
//#pragma linkage (verkoop, COBOL)
extern void VERKOOP(char *productid, char *aantal, char*resultaat);
main(int argc, char *argv[])
{
int return_status;
COB_RTD = cob_get_rtd();
char *productid = "20 ";
char *aantal = "000020";
char resultaat[30];
cob_init(rtd, 0, NULL);
printf("hallo");//prints
VERKOOP(productid, aantal, resultaat);
printf("hallo");//doesn't print
printf("resultaat:%s", resultaat);// doesn't print
cob_stop_run (rtd, return_status);
}
I'm using the printf to see if resultaat has been assigned correctly. However, both of the lines after VERKOOP don't print for some reason.
This is the COBOL code of VERKOOP(he does fill in LS-RESULTAAT correctly here, I tried it with DISPLAY and this part works):
*************************************************************
* VERKOOP
*************************************************************
IDENTIFICATION DIVISION.
PROGRAM-ID. VERKOOP.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PRODUCTEN ASSIGN TO "BESTANDEN/LIJSTPRODUCTEN"
ACCESS MODE IS RANDOM
ORGANIZATION IS INDEXED
RECORD KEY IS PRODUCTID
FILE STATUS IS WS-FILE-STATUS.
DATA DIVISION.
FILE SECTION.
FD PRODUCTEN BLOCK CONTAINS 10 RECORDS.
01 PRODUCT.
02 PRODUCTID PIC X(6).
02 LEVERANCIERID PIC X(6).
02 AANTAL PIC 9(6).
WORKING-STORAGE SECTION.
77 FOUT PIC X.
88 PRODUCT-NIET-GEVONDEN VALUE 1.
77 WS-PRODUCTID PIC X(6).
77 WS-AANTAL PIC 9(6).
77 WS-FILE-STATUS PIC XX.
77 WS-RESULTAAT PIC X(30).
LINKAGE SECTION.
01 LS-PRODUCTID PIC X(6).
01 LS-AANTAL PIC 9(6).
01 LS-RESULTAAT PIC X(30).
PROCEDURE DIVISION USING LS-PRODUCTID, LS-AANTAL, LS-RESULTAAT.
MAIN.
PERFORM INITIALISEER
PERFORM LEES-PRODUCT-IN
PERFORM LEES-BESTAND
PERFORM SLUIT-BESTAND
STOP RUN.
INITIALISEER.
OPEN I-O PRODUCTEN.
* DISPLAY WS-FILE-STATUS..
LEES-PRODUCT-IN.
MOVE LS-PRODUCTID TO WS-PRODUCTID
MOVE LS-AANTAL TO WS-AANTAL
MOVE 'OK' TO WS-RESULTAAT
* DISPLAY WS-RESULTAAT
MOVE WS-RESULTAAT TO LS-RESULTAAT.
* DISPLAY "GEEF PRODUCTID OP: "
* ACCEPT WS-PRODUCTID
* DISPLAY "GEEF AANTAL OP: "
* ACCEPT WS-AANTAL.
LEES-BESTAND.
* DISPLAY "LEES-BESTAND"
MOVE WS-PRODUCTID TO PRODUCTID
* DISPLAY PRODUCTID
* DISPLAY WS-FILE-STATUS
READ PRODUCTEN INVALID KEY SET PRODUCT-NIET-GEVONDEN TO TRUE
END-READ
IF PRODUCT-NIET-GEVONDEN PERFORM FOUTJE
ELSE
* MOVE WS-PRODUCTID TO PRODUCTID
SUBTRACT WS-AANTAL FROM AANTAL
PERFORM UPDATE-PRODUCT
END-IF.
UPDATE-PRODUCT.
REWRITE PRODUCT INVALID KEY PERFORM FOUTJE.
SLUIT-BESTAND.
* DISPLAY "SLUIT-BESTAND"
CLOSE PRODUCTEN.
FOUTJE.
DISPLAY "ER IS EEN FOUT OPGETREDEN"
DISPLAY WS-FILE-STATUS
STOP RUN.
UPDATE: I tried removing both "STOP RUN's", however now for some reason he substracts 40 instead of 20 and prints "ER IS EEN FOUT OPGETREDEN". So he is running the COBOL Program twice for some reason.
UPDATE: After replacing STOP RUN by GOBACK it works perfectly
Your STOP RUN is returning here, cob_stop_run (rtd, return_status), so your prior code after the invocation of the COBOL program does not run.
If EXIT PROGRAM is in a "main" program (at least in the case of the pragma-usage it seems that is what you have) then it is treated the same as STOP RUN.
GOBACK is returning control to where you expect it to.
If you use the COBOL-IT API, as has already been suggested, then perhaps the EXIT PROGRAM will behave differently. Perhaps not.
You are using an undocumented way to call a COBOL-IT program. Exactly how it behaves is not known, and cannot be known, to someone without COBOL-IT and the same operating system as you have, and the patience to do something in a non-obvious way.
If things are suggested which you then ignore, it is difficult to keep answering your questions.
Again, you have an Assignment. The Assignment expects you to use the API. You should use the API and get your programs working. If you have time later, you can look at the pragma-usage to effect the interoperability.

What are some good ways to compress data across time?

I have an array of objects with time and value property. Looks something like this.
UPDATE: dataset with epoch times rather than time strings
[{datetime:1383661634, value: 43},{datetime:1383661856, value: 40}, {datetime:1383662133, value: 23}, {datetime:1383662944, value: 23}]
The array is far larger than this. Possibly a 6 digit length. I intend to build a graph to represent this array. Due to obvious reasons, I cannot use every bit of the data to build this graph (value vs time); so I need to normalize it across time.
So here's the main problem - There is no trend in the timestamp for these objects; so I need to dynamically choose slots of time in which I either average out the values or show counts of objects in that slot.
How can I calculate slots that user friendly. i.e per minute, hour, day, eight hours or so. I am looking at having a maximum of 25 slots done out of the array, which I show up on the graph.
I hope this helps get my point through.
You can convert the date/time into epoch and use numpy.histogram to get the ranges:
import random, numpy
l = [ random.randint(0, 1000) for x in range(1000) ]
num_items_bins, bin_ranges = numpy.histogram(l, 25)
print num_items_bins
print bin_ranges
Gives:
[34 38 42 41 43 50 34 29 37 46 31 47 43 29 30 42 38 52 42 44 42 42 51 34 39]
[ 1. 40.96 80.92 120.88 160.84 200.8 240.76 280.72
320.68 360.64 400.6 440.56 480.52 520.48 560.44 600.4
640.36 680.32 720.28 760.24 800.2 840.16 880.12 920.08
960.04 1000. ]
Hard to say without knowing the nature of your values, compressing values for display is a matter of what you can afford to discard and what you can't. Some ideas though:
histogram
candlestick chart
Is this JSON and the DateTimes transmitted as text?
Why not transmit the Date as a long (Int64), and use a method to convert to/from DateTime? Depending on which language you could use these implementations:
DateTime to Long in C#
Date to long using Unix timestamp in Java
That alone would save you a considerable amount of space, since strings are 16-bits per character and the long TimeStamp would be just 64 bits.

protobuf c string decode

Experts, I need your help in decoding the string value in the protobuf-c message. Say the message
m { optional string id =0 }
the string is stored with a value "test", by using the C api m.id = "test" and pack it using m__pack(&m,buf); the message stream will look like 0a 04 74 65 73 74, where my string value is 74 65 73 74 in ascii format. I try to get my string value back by using api m__unpack(NULL,length,buf) where buf contains the stream. Now when I try to print the string printf("%s\n",msg->id) I get seg fault. Shouldn't it print the ascii values(74 65 73 74)? Can you please help in getting the string value. Thanks for the help.
solved..
its my mistake, i was printing the msg->id after the free_unpack api, hence the issue.
thanks for your time.

Resources