/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/crypto/symmetric/PKCS5.as
ActionScript | 44 lines | 30 code | 5 blank | 9 comment | 4 complexity | 517ed29bc41e2d86e5794320e98d4c99 MD5 | raw file
1/** 2 * PKCS5 3 * 4 * A padding implementation of PKCS5. 5 * Copyright (c) 2007 Henri Torgemane 6 * 7 * See LICENSE.txt for full license information. 8 */ 9package com.hurlant.crypto.symmetric 10{ 11 import flash.utils.ByteArray; 12 13 public class PKCS5 implements IPad 14 { 15 private var blockSize:uint; 16 17 public function PKCS5(blockSize:uint=0) { 18 this.blockSize = blockSize; 19 } 20 21 public function pad(a:ByteArray):void { 22 var c:uint = blockSize-a.length%blockSize; 23 for (var i:uint=0;i<c;i++){ 24 a[a.length] = c; 25 } 26 } 27 public function unpad(a:ByteArray):void { 28 var c:uint = a.length%blockSize; 29 if (c!=0) throw new Error("PKCS#5::unpad: ByteArray.length isn't a multiple of the blockSize"); 30 c = a[a.length-1]; 31 for (var i:uint=c;i>0;i--) { 32 var v:uint = a[a.length-1]; 33 a.length--; 34 if (c!=v) throw new Error("PKCS#5:unpad: Invalid padding value. expected ["+c+"], found ["+v+"]"); 35 } 36 // that is all. 37 } 38 39 public function setBlockSize(bs:uint):void { 40 blockSize = bs; 41 } 42 43 } 44}