如何安全地删除文件?

发布于 2024-12-21 03:25:26 字数 82 浏览 3 评论 0原文

是否有 Gem 或方法可以安全地删除 Ruby 中的文件?我想避免系统上可能不存在的外部程序。

我所说的“安全擦除”是指覆盖文件内容。

Is there a Gem or means of securely erasing a file in Ruby? I'd like to avoid external programs that may not be present on the system.

By "secure erase" I'm referring to overwriting the file contents.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴骨ら 2024-12-28 03:25:26

如果您使用 *nix,一个很好的方法是使用 exec/open3/open4 调用 shred

`shred -fxuz #{filename}`

: .html" rel="nofollow noreferrer">http://www.gnu.org/s/coreutils/manual/html_node/shred-in Vocation.html

检查这个类似的帖子:

用 python 或 ruby​​ 编写文件粉碎机?

If you are on *nix, a pretty good way would be to just call shred using exec/open3/open4:

`shred -fxuz #{filename}`

http://www.gnu.org/s/coreutils/manual/html_node/shred-invocation.html

Check this similar post:

Writing a file shredder in python or ruby?

九八野马 2024-12-28 03:25:26

像这样的事情会帮助你开始:

#!/usr/bin/env ruby

abort "Missing filename" if (ARGV.empty?)

ARGV.each do |filename|
  filesize = File.size(filename)
  [0x00, 0xff].each do |byte|
    File.open(filename, 'wb') do |fo|
      filesize.times { fo.print(byte.chr) }
    end
  end
end

它应该会让你接近。

为了更彻底,您还可以使用 0xaa0x55 来交替字节中的 0 和 1 位。 Random.rand(0xff) 将为您提供 0 到 255 之间的随机值。

Something like this will get you started:

#!/usr/bin/env ruby

abort "Missing filename" if (ARGV.empty?)

ARGV.each do |filename|
  filesize = File.size(filename)
  [0x00, 0xff].each do |byte|
    File.open(filename, 'wb') do |fo|
      filesize.times { fo.print(byte.chr) }
    end
  end
end

It should get you close.

For more thoroughness, you could also use 0xaa and 0x55 for alternating 0 and 1 bits in the byte. Random.rand(0xff) will give you a random value from 0 to 255.

依 靠 2024-12-28 03:25:26

只需

  1. 打开文件
  2. 写入一些垃圾,其数量至少等于当前文件大小
  3. flush()close()
  4. 重复 N 次,将垃圾与零和 0xff 混合 在不同的通行证上

just

  1. open the file
  2. write some garbage at least in amount equal to current file size
  3. flush() and close()
  4. repeat N times, mixing garbage with zeroes and 0xff's on different passes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文