Remove crypto.subtle requirement
This commit is contained in:
		
							parent
							
								
									4a6aca4c07
								
							
						
					
					
						commit
						3699464947
					
				
					 2 changed files with 13 additions and 9 deletions
				
			
		| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
import Connection from "./Connection";
 | 
			
		||||
import {sha256} from "./utils";
 | 
			
		||||
import {hashCode} from "./utils";
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * The connection manager keeps track of active connections (WebSocket connections, see Connection).
 | 
			
		||||
| 
						 | 
				
			
			@ -108,10 +108,9 @@ class ConnectionManager {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
const makeConnectionId = async (subscription, user) => {
 | 
			
		||||
    const hash = (user)
 | 
			
		||||
        ? await sha256(`${subscription.id}|${user.username}|${user.password}`)
 | 
			
		||||
        : await sha256(`${subscription.id}`);
 | 
			
		||||
    return hash.substring(0, 10);
 | 
			
		||||
    return (user)
 | 
			
		||||
        ? hashCode(`${subscription.id}|${user.username}|${user.password}`)
 | 
			
		||||
        : hashCode(`${subscription.id}`);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const connectionManager = new ConnectionManager();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -115,10 +115,15 @@ export const shuffle = (arr) => {
 | 
			
		|||
    return arr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// https://jameshfisher.com/2017/10/30/web-cryptography-api-hello-world/
 | 
			
		||||
export const sha256 = async (s) => {
 | 
			
		||||
    const buf = await crypto.subtle.digest("SHA-256", new TextEncoder("utf-8").encode(s));
 | 
			
		||||
    return Array.prototype.map.call(new Uint8Array(buf), x=>(('00'+x.toString(16)).slice(-2))).join('');
 | 
			
		||||
/** Non-cryptographic hash function, see https://stackoverflow.com/a/8831937/1440785 */
 | 
			
		||||
export const hashCode = async (s) => {
 | 
			
		||||
    let hash = 0;
 | 
			
		||||
    for (let i = 0; i < s.length; i++) {
 | 
			
		||||
        const char = s.charCodeAt(i);
 | 
			
		||||
        hash = ((hash<<5)-hash)+char;
 | 
			
		||||
        hash = hash & hash; // Convert to 32bit integer
 | 
			
		||||
    }
 | 
			
		||||
    return hash;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const formatShortDateTime = (timestamp) => {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue