cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

ThingWorx Navigate is now Windchill Navigate Learn More

Translate the entire conversation x

SHA256 hashing in ThingWorx service

mneumann
16-Pearl

SHA256 hashing in ThingWorx service

For a service I need to compare a SHA256 hashed value with a value of a ThingWorx property.

As ThingWorx only knows the actual value, I need to SHA256 it to be able to compare it to the value I receive.

 

Is there a built-in service that can do this?


I have only seen EncryptionServices which don't really help, as I am not interested in Encryption but only Hashing. Ideally I wouldn't want to move this out to an external library (or Java-SDK service).

ACCEPTED SOLUTION

Accepted Solutions
mneumann
16-Pearl
(To:Rocko)

Thank you!
As mentioned an extension is nothing I am aiming for right now.

I was following the (corrected) solution in https://stackoverflow.com/questions/59777670/how-can-i-hash-a-string-with-sha256

This allows me to have a custom service that can handle the hashing directly, without going with an extension.

View solution in original post

3 REPLIES 3


@mneumann wrote:

Is there a built-in service that can do this?


Not that I know of. You could try to find a JS implementation and put that into a service.

The alternative: Thingworx already comes with commons-codec, so you would just have to expose their services, specifically

DigestUtils.sha256Hex(originalString);

That would mean writing an extension with a thing that calls this method and exposes it as a @ThingworxService. 

mneumann
16-Pearl
(To:Rocko)

Thank you!
As mentioned an extension is nothing I am aiming for right now.

I was following the (corrected) solution in https://stackoverflow.com/questions/59777670/how-can-i-hash-a-string-with-sha256

This allows me to have a custom service that can handle the hashing directly, without going with an extension.

Hello,

 I had a same usecase in the past but i could not find any built in Thingworx implementation for SHA256. As you have mentioned we have Encrpytion services in thingworx but i was also interested in hashing. For that i have used this built in crypto.subtle API which supports SHA 256 in browsers

 

async function sha256(input) {
  const encoder = new TextEncoder();
  const data = encoder.encode(input);
  const hashBuffer = await crypto.subtle.digest("SHA-256", data);
  const hashArray = Array.from(new Uint8Array(hashBuffer));
  const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
  return hashHex;
}

sha256("HelloWorld").then(console.log); 
// -> e.g. "872e4e50ce9990d8b041330c47c9ddd11bec6b503ae9386a99da8584e9bb12c4"

 

Other then that Encrpytion in thingworx is not recommend for migration like if you have saved encrpted values in the datatables then you would not be able to take that data with export. like we had a situation where we have changed our thingworx version/server and then i had this road block and my basetype of that field was password with encrpted value saved in it and now i have removed the encrption and using SHA 256 for comparison.

 

Thank you

Announcements


Top Tags