import * as crypto from "crypto" ; import { ICryptoKeyIV } from "./ICryptoKeyIV" ; export default class CryptoTools { public static generateKeyIVByPassword ( passwordStr , keyLength , ivLength ) { var password = new Buffer ( passwordStr , "binary" ) ; var hashBuffers = [ ] ; for ( var dataCount = 0 , loopCount = 0 ; dataCount < keyLength + ivLength ; loopCount ++ ) { var data = password ; if ( loopCount > 0 ) { data = Buffer . concat ( [ hashBuffers [ loopCount - 1 ] , password ] ) ; } var md5 = crypto . createHash ( "md5" ) ; var md5Buffer = md5 . update ( data ) . digest ( ) ; hashBuffers . push ( md5Buffer ) ; dataCount += md5Buffer . length ; } var hashBuffer = Buffer . concat ( hashBuffers ) ; var key = hashBuffer . slice ( 0 , keyLength ) ; var iv = hashBuffer . slice ( keyLength , keyLength + ivLength ) ; return { key : key , iv : iv } ; } public static generateRc4Md5KeyByKV ( kv ) { var md5 = crypto . createHash ( "md5" ) ; md5 . update ( kv . key ) ; md5 . update ( kv . iv ) ; var hash = md5 . digest ( ) ; return hash ; } }