So doing a small amount of playing in SQL Server.
select '214163915155286000' as s,
hashbytes('sha1', '214163915155286000') as h,
cast(hashbytes('sha1', '214163915155286000') as int) as i
;
Gives:
s, 214163915155286000
h, 0x374543AC5A9890AF8CD863EB7C98E7467FBCAF2B
i, 2143072043
so my SQL is the same as your, as good start.
converting 2143072043
to hex we get 7FBCAF2B
which is the last 4 bytes of the hash.
Thus the you want to get the result of hash and truncate, which I will BITAND to achieve, but last time I used the snowflake BIT functions they did allow hex input, so in stead of type a rather clear 0xFFffFFff
we will use the decimal of that 4294967295
, thus this should work for you:
select bitand(sha1('214163915155286000'), 4294967295);
Right so thanks to Gokhan's insights, and logging into snowflake, and reading the manual for BITSHIFTLEFT/RIGHT. we can use the shifts, but the output is a 128 bit number, not a 64 bit, as I had assumed, to extend the sign bit correctly we have to shift by 96 bits, which this code shows working:
SELECT
column1 as input,
sha1(input) as sha1,
right(sha1,8) as right8,
to_number(right8,'XXXXXXXX') as int32,
BITSHIFTRIGHT(BITSHIFTLEFT(int32,96),96) as result
FROM VALUES
('214163915155286001'),
('214163915155286000')
ORDER BY 1;
which gives the output:
INPUT |
SHA1 |
RIGHT8 |
INT32 |
RESULT |
214163915155286000 |
374543ac5a9890af8cd863eb7c98e7467fbcaf2b |
7fbcaf2b |
2143072043 |
2143072043 |
214163915155286001 |
1911d3df794846fbc74e0e4cf29133459466e0e7 |
9466e0e7 |
2489770215 |
-1805197081 |
so a more compact and final block of SQL can be:
BITSHIFTRIGHT(BITSHIFTLEFT(to_number(right(sha1(input),8),'XXXXXXXX'),96),96)