判断base64加密后的字符串是否是图片

http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html

http://en.wikipedia.org/wiki/List_of_file_signatures

Image files

File typeTypical
extension
Hex digits
xx = variable
Ascii digits
. = not an ascii char
Bitmap format.bmp42 4dBM
FITS format.fits53 49 4d 50 4c 45SIMPLE
GIF format.gif47 49 46 38GIF8
Graphics Kernel System.gks47 4b 53 4dGKSM
IRIS rgb format.rgb01 da..
ITC (CMU WM) format.itcf1 00 40 bb....
JPEG File Interchange Format.jpgff d8 ff e0....
NIFF (Navy TIFF).nif49 49 4e 31IIN1
PM format.pm56 49 45 57VIEW
PNG format.png89 50 4e 47.PNG
Postscript format.[e]ps25 21%!
Sun Rasterfile.ras59 a6 6a 95Y.j.
Targa format.tgaxx xx xx...
TIFF format (Motorola - big endian) .tif4d 4d 00 2aMM.*
TIFF format (Intel - little endian) .tif49 49 2a 00II*.
X11 Bitmap format.xbmxx xx
XCF Gimp file structure.xcf67 69 6d 70 20 78 63 66 20 76gimp xcf
Xfig format.fig23 46 49 47#FIG
XPM format.xpm2f 2a 20 58 50 4d 20 2a 2f/* XPM */

Compressed files

File typeTypical
extension
Hex digits
xx = variable
Ascii digits
. = not an ascii char
Bzip.bz42 5aBZ
Compress.Z1f 9d..
gzip format.gz1f 8b..
pkzip format.zip50 4b 03 04PK..

Archive files

File typeTypical
extension
Hex digits
xx = variable
Ascii digits
. = not an ascii char
TAR (pre-POSIX).tarxx xx(a filename)
TAR (POSIX).tar75 73 74 61 72ustar (offset by 257 bytes)

Excecutable files

File typeTypical
extension
Hex digits
xx = variable
Ascii digits
. = not an ascii char
MS-DOS, OS/2 or MS Windows4d 5aMZ
Unix elf7f 45 4c 46.ELF

Miscellaneous files

File typeTypical
extension
Hex digits
xx = variable
Ascii digits
. = not an ascii char
pgp public ring99 00..
pgp security ring95 01..
pgp security ring95 00..
pgp encrypted dataa6 00¦.

 

由于二进制文件有自己的magic number,只需要根据magic number则可以判断是否为image。

def is_a_media_resource body
    data = Base64.decode64 body
    bytes = data.bytes.to_a
    bytes = bytes[0...9]
    hex_bytes = []
    bytes.each{|e| hex_bytes << e.to_s(16)}
    return is_media? hex_bytes
  end

 经过base64解密后,提取前9个字节进行比对,因为magic number最长是9个字节。

 

module ReceiverHelper
  MAGIC_NUMBERS ={
    "PNG"=>["89","50","4e","47"],
    "GIF"=>["47","49","46","38"],
    "FITS"=>["53","49","4d","50","4c", "45"],
    "GKS"=>["47","4b","53","4d"],
    "RGB"=>["01","da"],
    "ITC"=>["f1","00" ,"40","bb"],
    "JPG"=>["ff","d8","ff","e0"],
    "NIF"=>["49","49","4e","31"],
    "PM"=>["56","49","45","57"],
    "EPS"=>["25","21"],
    "RAS"=>["59", "a6", "6a", "95"],
    "TIF_BIGENDIAN"=>["4d", "4d", "00", "2a"],
    "TIF_LITTLEEDIAN"=>["49" ,"49", "2a", "00"],
    "XCF"=>["67", "69", "6d", "70", "20", "78", "63", "66", "20", "76"],
    "FIG"=>["23", "46", "49", "47"],
    "XPM"=>["2f", "2a", "20", "58", "50", "4d", "20", "2a", "2f"]
    }
  def is_media? data 
    MAGIC_NUMBERS.each_pair{|type,numbers|
      if equals(data,numbers)
        return true
      end 
    }
    false
  end
  private 
  def equals data,png_magic_numbers
    data[0...png_magic_numbers.length] == png_magic_numbers
  end
end
 

 

 

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值