From: "NAKAMURA, Hiroshi" Date: 2005-07-29T10:46:38+09:00 Subject: Re: Ruby libraries --------------enig864F2775BF31181C968CAABB Content-Type: multipart/mixed; boundary="------------010001060308060406090600" This is a multi-part message in MIME format. --------------010001060308060406090600 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hi, Andrew Falanga wrote: > Outstanding! I forgot to ask in the original post, however, that I also > need a library for creating CRC's with a divisor of 32bits. Does > something like this exist? You can use Zlib.crc32 if you need CRC32-Reversed. And attached is my port of CRC base functions in Classless.hasher at http://www.classless.net/projects/hasher/ . Use this if you need CRC32 - not Reversed. Regards, // NaHi --------------010001060308060406090600 Content-Type: text/plain; name="crc.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="crc.rb" # crc.rb - Ruby port of CRC.cs in Classless.Hasher - C#/.NET Hash and Checksum Algorithm Library. (http://www.classless.net/projects/hasher/) =begin following is an excerpt of CRC.cs in Classless.Hasher. /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Classless.Hasher - C#/.NET Hash and Checksum Algorithm Library. * * The Initial Developer of the Original Code is Classless.net. * Portions created by the Initial Developer are Copyright (C) 2004 the Initial * Developer. All Rights Reserved. * * Contributor(s): * Jason Simeone (jay@classless.net) * * ***** END LICENSE BLOCK ***** */ =end class CRC def initialize(order, poly, iv, xor, reflect) @order = order @poly = poly @iv = iv @xor = xor @reflect = reflect if @reflect @mask = (2 ** (@order - 8)) - 1 end @lookup = build_lookup @crc = init end def init crc = @iv if @reflect crc = reflect(crc, @order) end crc end def update(str) topbit = 1 << (@order - 1) widthmask = (topbit << 1) - 1 str.each_byte do |byte| if @reflect @crc = ((@crc >> 8) & @mask) ^ @lookup[(@crc ^ byte) & 0xFF] else @crc = (@crc << 8) ^ @lookup[((@crc >> (@order - 8)) ^ byte) & 0xFF] end @crc &= widthmask end end def final @crc ^= @xor bytes = (@order + 7) >> 3 @crc end private def build_lookup lookup = [] topbit = 1 << (@order - 1) widthmask = (topbit << 1) - 1 for idx in 0..255 v = idx v = reflect(v, 8) if @reflect v <<= (@order - 8) 8.times do if (v & topbit).nonzero? v = (v << 1) ^ @poly else v <<= 1 end end v = reflect(v, @order) if @reflect v &= widthmask lookup << v end lookup end def reflect(crc, bits) base = crc for idx in 0...bits bitmask = 1 << ((bits - 1) - idx) if (base & 1).nonzero? crc |= bitmask; else crc &= ~bitmask; end base >>= 1 end crc end end if __FILE__ == $0 crc32 = CRC.new(32, 0xEDB88320, 0xFFFFFFFF, 0xFFFFFFFF, false) crc32rev = CRC.new(32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true) crc32.update("12345") p crc32.final crc32rev.update("12345") p crc32rev.final require 'zlib' p Zlib.crc32("12345") crc8 = CRC.new(8, 0xE0, 0x00, 0x00, false) msg = ARGV.shift msg = "\1\2\3\4" crc8.update(msg) printf("%x\n", crc8.final) require 'pgp/util' crc24 = CRC.new(24, 0x01864cfb, 0x00b704ce, 0, false) msg = ARGV.shift crc24.update(msg) p crc24.final p PGP::Util.crc24(msg) end --------------010001060308060406090600-- --------------enig864F2775BF31181C968CAABB Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (Cygwin) iD8DBQFC6Yp5f6b33ts2dPkRAjxeAKCQRZMSG2NwE1lrl3BG6gyFcX3T/ACgypyW TdXCG+Uf80nwW/77tU9Ahfw= =kRCL -----END PGP SIGNATURE----- --------------enig864F2775BF31181C968CAABB--