小编leh*_*ins的帖子

Haskell 或 OCAML 可以处理敏感数据而不会通过垃圾收集泄漏吗?

我会做这样的事情(伪代码):

1. load sensitive encrypted data from file
2. decrypt the data
3. do something with the unencrypted data
4. override the data safely / securely (for example with random data)
Run Code Online (Sandbox Code Playgroud)

敏感数据在内存中保持原样(未加密)的时间应尽可能短。

不得以任何方式泄露明文数据。

A. 这样的程序可以用 Haskell 或 OCAML 编写吗?

B. 是否可以防止数据泄露,即被垃圾收集器在后台静默复制?

C. 能否在内存中正确覆盖纯数据?

据我所知,垃圾收集器 (GC) 可以在后台静默复制数据。我猜这是由分代 GC 算法完成的,但我不确定。

我知道如果攻击者设法在正确的时间/状态获取程序的内存,攻击者仍然有可能获得纯数据。我只是考虑这样做以提高安全性,因为我没有控制上下文(即操作系统、交换等)。

ocaml garbage-collection haskell memory-management

16
推荐指数
2
解决办法
257
查看次数

在NF之前的最后一个论点?

我之前尝试打印过这个论点$NF.然而$NF--,不做的伎俩.这是什么解决方案?问题是,我不知道有多少args,所以我总是需要$NF和arg之前.

亲切的问候

克林斯曼

host -t ptr 1.1.1.1 | awk '/pointer/ {num=split($0,a, "."); print a[num-2] "." a[num-1] ;}'

foo.tld
Run Code Online (Sandbox Code Playgroud)

awk

14
推荐指数
1
解决办法
1万
查看次数

是否有可能在Haskell中加速快速排名?

我有这个看似琐碎的并行快速实现,代码如下:

import System.Random
import Control.Parallel
import Data.List

quicksort :: Ord a => [a] -> [a]
quicksort xs = pQuicksort 16 xs -- 16 is the number of sparks used to sort

-- pQuicksort, parallelQuicksort  
-- As long as n > 0 evaluates the lower and upper part of the list in parallel,
-- when we have recursed deep enough, n==0, this turns into a serial quicksort.
pQuicksort :: Ord a => Int -> [a] -> [a]
pQuicksort _ [] = …
Run Code Online (Sandbox Code Playgroud)

parallel-processing profiling haskell quicksort

14
推荐指数
3
解决办法
1544
查看次数

如何在Python中使用Google Shortener API

我想写一个应用程序来缩短网址.这是我的代码:

import urllib, urllib2
import json
def goo_shorten_url(url):
    post_url = 'https://www.googleapis.com/urlshortener/v1/url'
    postdata = urllib.urlencode({'longUrl':url})
    headers = {'Content-Type':'application/json'}
    req = urllib2.Request(
        post_url,
        postdata,
        headers
        )
    ret = urllib2.urlopen(req).read()
    return json.loads(ret)['id']
Run Code Online (Sandbox Code Playgroud)

当我运行代码获取一个小url时,它抛出一个异常:urllib2.HTTPError: HTTP Error 400: Bad Requests.这段代码有什么问题?

python google-api google-url-shortener

11
推荐指数
3
解决办法
7243
查看次数

类型类函数的显式forall

从ghc-8.0开始,我们有一个很好的扩展名TypeApplications.这允许我们而不是:

?> show (5 :: Int)
"5"
Run Code Online (Sandbox Code Playgroud)

这样做是这样的:

?> :set -XTypeApplications
?> show @Int 5
"5"
Run Code Online (Sandbox Code Playgroud)

这真的很酷.当我们添加更多类型变量时,它会变得更加复杂,但是有一些规则可以用来确定确切的顺序,并且它们有很好的记录:

showFooBar :: (Show a, Show b) => a -> b -> String
showFooBar a b = show a ++ " and " ++ show b
Run Code Online (Sandbox Code Playgroud)

所以在上面的函数中我们首先提供a然后b:

?> showFooBar @Int @Double 3 4
"3 and 4.0"
Run Code Online (Sandbox Code Playgroud)

这很好,但是如果我想改变订单怎么办?没问题,我们可以使用ExplicitForAll扩展(或其他一些暗示它)来指定它:

{-# LANGUAGE ExplicitForAll #-}

showFooBar :: forall b a . (Show a, Show b) => a -> b -> …
Run Code Online (Sandbox Code Playgroud)

haskell typeclass forall

11
推荐指数
1
解决办法
148
查看次数

使用Haskell检索图像的像素值

是否有可用的方法或库可以加载图像(jpeg,png等)并将该图像的像素值分配到列表或矩阵中?我想做一些图像和模式识别的实验.

在正确的方向稍微推动将不胜感激.

haskell image-processing image-recognition libraries

10
推荐指数
1
解决办法
1668
查看次数

交错手动的realWorld#状态传递与任意Monad是否安全

考虑这个生成任意列表的函数Monad:

generateListM :: Monad m => Int -> (Int -> m a) -> m [a]
generateListM sz f = go 0
  where go i | i < sz = do x <- f i
                           xs <- go (i + 1)
                           return (x:xs)
             | otherwise = pure []
Run Code Online (Sandbox Code Playgroud)

实现可能并不完美,但这里仅用于演示所需的效果,这非常简单.例如,如果monad是列表,那么获取列表列表:

?> generateListM 3 (\i -> [0 :: Int64 .. fromIntegral i])
[[0,0,0],[0,0,1],[0,0,2],[0,1,0],[0,1,1],[0,1,2]]
Run Code Online (Sandbox Code Playgroud)

我想要做的是实现相同的效果,但ByteArray代替List.事实证明,当我第一次偶然发现这个问题时,这比我想象的要复杂得多.最终目标是使用该生成器mapMmassiv中实现,但这是重点.

需要最少努力的方法是使用一个函数generateM向量而做了一些手动转换的包.但事实证明,有一种方法可以通过手动处理状态令牌并将其与monad交错的这个巧妙的小技巧来实现至少x2性能增益的因素:

{-# LANGUAGE MagicHash           #-}
{-# LANGUAGE …
Run Code Online (Sandbox Code Playgroud)

arrays primitive haskell vector massiv

7
推荐指数
1
解决办法
145
查看次数

为什么`readIORef` 是一个阻塞操作

这对我来说完全是个惊喜。有人可以解释一下在飞行中readIORef阻塞的原因是atomicModifyIORef什么吗?我知道假设是提供给后一个函数的修改函数应该非常快,但这不是重点。

这是一段示例代码,它重现了我所说的内容:

{-# LANGUAGE NumericUnderscores #-}
module Main where

import Control.Concurrent
import Control.Concurrent.Async
import Control.Monad
import Data.IORef
import Say (sayString)
import Data.Time.Clock
import System.IO.Unsafe

main :: IO ()
main = do
  ref <- newIORef (10 :: Int)
  before <- getCurrentTime
  race_ (threadBusy ref 10_000_000) (threadBlock ref)
  after <- getCurrentTime
  sayString $ "Elapsed: " ++ show (diffUTCTime after before)


threadBlock :: IORef Int -> IO ()
threadBlock ref = do
  sayString "Below threads are totally blocked on …
Run Code Online (Sandbox Code Playgroud)

concurrency haskell ioref

6
推荐指数
1
解决办法
114
查看次数

为什么Random类没有最小完整定义

本页指出 Random 类的最小完整定义是“Nothing”

\n

但如果我不提供它们,它们就不起作用:

\n
data Color = Red | Blue deriving (Bounded, Show)\n\ninstance Random Color\n
Run Code Online (Sandbox Code Playgroud)\n

产生编译器警告:

\n
test.hs:5:10: warning: [-Wmissing-methods]\n    \xe2\x80\xa2 No explicit implementation for\n        \xe2\x80\x98randomR\xe2\x80\x99 and \xe2\x80\x98random\xe2\x80\x99\n    \xe2\x80\xa2 In the instance declaration for \xe2\x80\x98Random Color\xe2\x80\x99\n  |\n5 | instance Random Color\n
Run Code Online (Sandbox Code Playgroud)\n

当我运行代码时出现错误:

\n
*Main> let g = mkStdGen 100\n*Main> (fst $ random g) :: Color\n*** Exception: test.hs:5:10-21: No instance nor default method for class operation random\n
Run Code Online (Sandbox Code Playgroud)\n

random为什么和没有randomR列在最小完整定义下?

\n

random haskell

6
推荐指数
1
解决办法
162
查看次数

Haskell cabal包括静态库

我有一个令人尴尬的简单问题.

我正在尝试使用mylib.a一个大型C项目的归档库(称之为)(使用GHC的MinGW副本编译).

从顶层我有:

./project.cabal
./src/...haskell..code...
./cbits/interface.c (simplifies access to `lib.a`)
./include/mylib.h
./lib/mylib.a      <<<<<<<<<<<<<<< not sure where to put this or how to reference it
Run Code Online (Sandbox Code Playgroud)

project.cabal既有

c-sources:           cbits/interface.c
include-dirs:        include
Run Code Online (Sandbox Code Playgroud)

extra-lib-dirs似乎想绝对路径(目录).

如何解决这个问题?

haskell ffi cabal

5
推荐指数
1
解决办法
584
查看次数