Hashing session key - session

I'm attempting to hash a session key but I'm getting an error of cannot convert from string to byte[]
I'm assuming that the hash stores it in a byte array but why is it throwing errors on a session key variable.
var Sha1Hash = System.Security.Cryptography.SHA1.Create();
var hash = Sha1Hash.ComputeHash(HttpContext.Session.GetString(SessionKeyName));

You need to convert string (HttpContext.Session.GetString(SessionKeyName)) into byte array
var hash = Sha1Hash.ComputeHash(Encoding.ASCII.GetBytes(HttpContext.Session.GetString(SessionKeyName)));

Related

Web3j: Get value of transaction token transfered using Transaction Hash

With Web3j (not Web3js), how do I get the value of token transferred using transaction hash?
I found this example for web3j:
String transactionHash = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
TransactionReceipt transReceipt = web3j.ethGetTransactionReceipt(transactionHash).send().getTransactionReceipt();
Transaction transaction = ethTransaction.getResult();
int size = tokenImpl.getTransferEvents(transReceipt).size();
size = size -1;
BigInteger valueTransaction = tokenImpl.getTransferEvents(transReceipt).get(size).value;
The value will be recieved in BigInteger

Query regarding merging values of a Map to the first key of the Map

I've a hashmap with few key value pairs.
My requirement is to iterate over the all the key and value pairs but merge all the value to the first key of the hashset.
For example,
Map<String,Integer> resultMap = new HashMap();
Map<String,Integer> map = new HashMap();
map.put("abc",1);
map.put("def",2);
map.put("efg",3);
map.put("uvw",4);
map.put("xyz",5);
I want to do something similar to this:
map.foreach((k,v)->resultMap.merge(k,v,(v1,v2)->v1+v2)
the resultant map will have only one key i.e. "abc" and value as (1+2+3+4+5)=15
How can I do this efficiently using java8?
You can do like this:
String firstKey = "";
for (Map.Entry<String,Integer> entry:map.entrySet()) {
if (resultMap.isEmpty()) firstKey = entry.getKey();
resultMap.merge(firstKey, entry.getValue(), Integer::sum);
}
stream version:
resultMap = map.entrySet().stream()
.collect(HashMap::new, (m, e) ->
{
if (m.isEmpty()) m.put(e.getKey(), e.getValue());
else m.merge(m.entrySet().stream()
.findFirst().get().getKey(),e.getValue(),Integer::sum);
}
, HashMap::putAll);
You can sum all the values of the map first and then add it resultMap with first key.
int sum = map.values().stream().reduce(0, Integer::sum);
Map.Entry<String,Integer> entry = map.entrySet().iterator().next();
String key = entry.getKey();
resultMap.put(key,sum);
Using Merge function, first sum all the values with "randomKey" not present in the original Map. Then replace random key with original map's first key
map.forEach((k,v)->resultMap.merge("randomKey",v,Integer::sum));
Map.Entry<String,Integer> entry = map.entrySet().iterator().next();
Object obj = resultMap.remove("randomKey");
resultMap.put(entry.getKey(), (Integer) obj);

Cant get data from sql array with JDBC and HSQLDB

This code:
Array a=rs.getArray("curAccs");
ResultSet rs1=a.getResultSet();
int rs1size=rs1.getFetchSize();
return empty rs1, but a is ARRAY[221,222]
I also cant convert sql array to java
(Integer[])a2.getArray()
printing
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
How to fix it?
The array contents can be retrieved with these calls:
java.sql.Array a = rs.getArray("curAccs");
Object[] values = (Object[]) a.getArray();
Integer intValA = (Integer) values[0];
Integer intValB = (Integer) values[1];

How to store large values (10MB) using key-value store database?

Consider a system that requires the store of mostly small values (1-1000 bytes), but in some cases, it needs to store a large value (10MB). It contains a list of hashes (think a list of public keys for EDDSA signature)
In Ethereum, the storage is done via PATRICIA TIRE, but I dont think that is necessary. Does anyone know of a good way to handle such large values with a key/store database?
Here is some code to store large binary object (aka. blobs) with foundationdb:
from uuid import uuid4
from uuid import UUID
from collections import namedtuple
from hashlib import blake2b as hasher
from more_itertools import sliced
import found
class BStoreException(found.BaseFoundException):
pass
BSTORE_SUFFIX_HASH = [b'\x01']
BSTORE_SUFFIX_BLOB = [b'\x02']
BStore = namedtuple('BStore', ('name', 'prefix_hash', 'prefix_blob',))
def make(name, prefix):
prefix = list(prefix)
out = BStore(name, tuple(prefix + BSTORE_SUFFIX_HASH), tuple(prefix + BSTORE_SUFFIX_BLOB))
return out
async def get_or_create(tx, bstore, blob):
hash = hasher(blob).digest()
key = found.pack((bstore.prefix_hash, hash))
maybe_uid = await found.get(tx, key)
if maybe_uid is not None:
return UUID(bytes=maybe_uid)
# Otherwise create the hash entry and store the blob with a new uid
# TODO: Use a counter and implement a garbage collector, and implement
# bstore.delete
uid = uuid4()
found.set(tx, key, uid.bytes)
for index, slice in enumerate(sliced(blob, found.MAX_SIZE_VALUE)):
found.set(tx, found.pack((bstore.prefix_blob, uid, index)), bytes(slice))
return uid
async def get(tx, bstore, uid):
key = found.pack((bstore.prefix_blob, uid))
out = b''
async for _, value in found.query(tx, key, found.next_prefix(key)):
out += value
if out == b'':
raise BStoreException('BLOB should be in database: uid={}'.format(uid))
return out
Taken from https://github.com/amirouche/asyncio-foundationdb/blob/main/found/bstore.py

RadSpreadsheet Cell Values for DateTime returning Number values (Telerik)

I have a CSV file with Dates ("MM/DD/YYYY"). I wish to parse these values within the active worksheet as string values, but they're converted to number format (i.e. "22532"). I'm unable to get these values in the original string format.
CellSelection selection = sheet.Cells[0, 0];
ICellValue value = selection.GetValue().Value;
Is it possible to ensure all cell values are in String format, representing exactly how they are in a CSV file?
You could use the following code:
CellSelection selection = sheet.Cells[0, 0];
string value = selection.GetValue().Value.GetResultValueAsString(new CellValueFormat("mm/d/yyyy"));
This will return a string formatted as a date. In the case with "22532" the result will be "09/8/1961".
This is not the exact answer you are looking for, but you can convert the int value to a datetime with the following code:
public static DateTime FromExcelSerialDate(this int serialDate)
{
if (serialDate > 59) serialDate -= 1; //Excel/Lotus 2/29/1900 bug
return new DateTime(1899, 12, 31).AddDays(serialDate);
}

Resources