@@ -479,4 +479,76 @@ namespace core {
479
479
splitLinesWorker ( text , lineStarts , /*lines*/ undefined , /*removeEmptyElements*/ false ) ;
480
480
return lineStarts ;
481
481
}
482
+
483
+ //
484
+ // Cryptography
485
+ //
486
+
487
+ const H = new Uint32Array ( 5 ) ;
488
+ const W = new Uint8Array ( 80 ) ;
489
+ const B = new Uint8Array ( 64 ) ;
490
+ const BLOCK_SIZE = 64 ;
491
+
492
+ export function sha1 ( message : string ) : string {
493
+ let buffer = B ;
494
+ const textSize = message . length ;
495
+ const messageSize = textSize * 2 ;
496
+ const finalBlockSize = messageSize % BLOCK_SIZE ;
497
+ const padSize = ( finalBlockSize < BLOCK_SIZE - 8 - 1 ? BLOCK_SIZE : BLOCK_SIZE * 2 ) - finalBlockSize ;
498
+ const byteLength = messageSize + padSize ;
499
+ if ( byteLength > BLOCK_SIZE ) {
500
+ buffer = new Uint8Array ( byteLength ) ;
501
+ }
502
+
503
+ const bufferView = new DataView ( buffer . buffer ) ;
504
+ for ( let i = 0 ; i < textSize ; ++ i ) {
505
+ bufferView . setUint16 ( i * 2 , message . charCodeAt ( i ) ) ;
506
+ }
507
+
508
+ buffer [ messageSize ] = 0x80 ;
509
+ bufferView . setUint32 ( byteLength - 4 , messageSize * 8 ) ;
510
+ H [ 0 ] = 0x67452301 , H [ 1 ] = 0xefcdab89 , H [ 2 ] = 0x98badcfe , H [ 3 ] = 0x10325476 , H [ 4 ] = 0xc3d2e1f0 ;
511
+ for ( let offset = 0 ; offset < byteLength ; offset += BLOCK_SIZE ) {
512
+ let a = H [ 0 ] , b = H [ 1 ] , c = H [ 2 ] , d = H [ 3 ] , e = H [ 4 ] ;
513
+ for ( let i = 0 ; i < 80 ; ++ i ) {
514
+ if ( i < 16 ) {
515
+ const x = offset + i * 4 ;
516
+ W [ i ] = buffer [ x ] << 24 | buffer [ x + 1 ] << 16 | buffer [ x + 2 ] << 8 | buffer [ x + 3 ] ;
517
+ }
518
+ else {
519
+ const x = W [ i - 3 ] ^ W [ i - 8 ] ^ W [ i - 14 ] ^ W [ i - 16 ] ;
520
+ W [ i ] = ( x << 1 | x >>> 31 ) >>> 0 ;
521
+ }
522
+
523
+ let t = ( a << 5 | a >>> 27 ) >>> 0 + e + W [ i ] ;
524
+ if ( i < 20 ) {
525
+ t += ( ( b & c ) | ( ~ b & d ) ) + 0x5A827999 ;
526
+ }
527
+ else if ( i < 40 ) {
528
+ t += ( b ^ c ^ d ) + 0x6ED9EBA1 ;
529
+ }
530
+ else if ( i < 60 ) {
531
+ t += ( ( b & c ) | ( b & d ) | ( c & d ) ) + 0x8F1BBCDC ;
532
+ }
533
+ else {
534
+ t += ( b ^ c ^ d ) + 0xCA62C1D6 ;
535
+ }
536
+
537
+ e = d , d = c , c = ( b << 30 | b >>> 2 ) >>> 0 , b = a , a = t ;
538
+ }
539
+
540
+ H [ 0 ] += a , H [ 1 ] += b , H [ 2 ] += c , H [ 3 ] += d , H [ 4 ] += e ;
541
+ }
542
+
543
+ for ( let i = 0 ; i < 5 ; ++ i ) {
544
+ bufferView . setUint32 ( i * 4 , H [ i ] ) ;
545
+ }
546
+
547
+ let result = "" ;
548
+ for ( let i = 0 ; i < 20 ; ++ i ) {
549
+ result += ( buffer [ i ] < 16 ? "0" : "" ) + buffer [ i ] . toString ( 16 ) ;
550
+ }
551
+
552
+ return result ;
553
+ }
482
554
}
0 commit comments