作者:vtolsto
项目:cloudmet
func (v *VirStorageVol) Download(stream *VirStream, offset, length uint64, flags uint32) error {
if C.virStorageVolDownload(v.ptr, stream.ptr, C.ulonglong(offset),
C.ulonglong(length), C.uint(flags)) == -1 {
return GetLastError()
}
return nil
}
作者:4cd
项目:srndv
// sign data with secret key sk
// return detached sig
// this uses crypto_sign instead pf crypto_sign_detached
func CryptoSignFucky(msg, sk []byte) []byte {
msgbuff := NewBuffer(msg)
defer msgbuff.Free()
skbuff := NewBuffer(sk)
defer skbuff.Free()
if skbuff.size != C.crypto_sign_bytes() {
log.Println("nacl.CryptoSign() invalid secret key size", len(sk))
return nil
}
// allocate the signed message buffer
sig := malloc(C.crypto_sign_bytes() + msgbuff.size)
defer sig.Free()
// compute signature
siglen := C.ulonglong(0)
res := C.crypto_sign(sig.uchar(), &siglen, msgbuff.uchar(), C.ulonglong(msgbuff.size), skbuff.uchar())
if res == 0 {
// return copy of signature inside the signed message
offset := int(C.crypto_sign_bytes())
return sig.Bytes()[:offset]
}
// failure to sign
log.Println("nacl.CryptoSign() failed")
return nil
}
作者:hopkings200
项目:catkeepe
func StorageVolUpload(vol VirStorageVol, s VirStream, offset uint64, length uint64) error {
result := C.virStorageVolUpload(vol.ptr, s.ptr, C.ulonglong(offset), C.ulonglong(length), 0)
if result < 0 {
return errors.New(GetLastError())
}
return nil
}
作者:ProjectNiw
项目:sagir
func (ctx *natrAEAD) Seal(dst, nonce, plaintext, data []byte) []byte {
out := make([]byte, len(plaintext)+_AEADOverheadBytes)
rv := C.crypto_aead_chacha20poly1305_encrypt(g2cbt(out), nil,
g2cbt(plaintext), C.ulonglong(len(plaintext)), g2cbt(data), C.ulonglong(len(data)),
nil, g2cbt(nonce), g2cbt(ctx.key[:]))
if rv != 0 {
panic("crypto_secretbox_easy returned non-zero")
}
return append(dst, out...)
}
作者:ProjectNiw
项目:sagir
func (ctx *natrAEAD) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
out := make([]byte, len(ciphertext)-_AEADOverheadBytes)
rv := C.crypto_aead_chacha20poly1305_decrypt(g2cbt(out), nil, nil,
g2cbt(ciphertext), C.ulonglong(len(ciphertext)), g2cbt(data), C.ulonglong(len(data)),
g2cbt(nonce), g2cbt(ctx.key[:]))
if rv != 0 {
return nil, errors.New("MAC error")
}
return append(dst, out...), nil
}
作者:Jenil
项目:golang-bb
func lowerWindow(c1 chan string, cycles int64) {
C.init_perfcounters(1, 0)
timeStart := C.ulonglong(C.get_cyclecount())
for {
timeElapsed := C.ulonglong(C.get_cyclecount()) - timeStart
if timeElapsed > C.ulonglong(cycles) {
c1 <- "ENTER WINDOW\n"
break
}
}
}
作者:asimshanka
项目:bn25
func big2scalar(out *[4]C.ulonglong, in *big.Int) error {
b := in.Bits()
if len(b) > 8 {
return fmt.Errorf("big.Int needs %d words, cannot be converted to scalar_t", len(b))
}
max := len(b) >> 1
for i := 0; i < max; i++ {
out[i] = C.ulonglong(b[i<<1]) | (C.ulonglong(b[i<<1+1]) << 32)
}
if len(b)&0x1 == 1 {
out[max] = C.ulonglong(b[len(b)-1])
}
return nil
}
作者:glycerin
项目:rm
//export ReadMsgpackFrame
//
// ReadMsgpackFrame reads the msgpack frame at byteOffset in rawStream, decodes the
// 2-5 bytes of a msgpack binary array (either bin8, bin16, or bin32), and returns
// and the decoded-into-R object and the next byteOffset to use.
//
func ReadMsgpackFrame(rawStream C.SEXP, byteOffset C.SEXP) C.SEXP {
var start int
if C.TYPEOF(byteOffset) == C.REALSXP {
start = int(C.get_real_elt(byteOffset, 0))
} else if C.TYPEOF(byteOffset) == C.INTSXP {
start = int(C.get_int_elt(byteOffset, 0))
} else {
C.ReportErrorToR_NoReturn(C.CString("read.msgpack.frame(x, byteOffset) requires byteOffset to be a numeric byte-offset number."))
}
// rawStream must be a RAWSXP
if C.TYPEOF(rawStream) != C.RAWSXP {
C.ReportErrorToR_NoReturn(C.CString("read.msgpack.frame(x, byteOffset) requires x be a RAW vector of bytes."))
}
n := int(C.Rf_xlength(rawStream))
if n == 0 {
return C.R_NilValue
}
if start >= n {
C.ReportErrorToR_NoReturn(C.CString(fmt.Sprintf("read.msgpack.frame(x, byteOffset) error: byteOffset(%d) is beyond the length of x (x has len %d).", start, n)))
}
var decoder [5]byte
C.memcpy(unsafe.Pointer(&decoder[0]), unsafe.Pointer(C.get_raw_elt_ptr(rawStream, C.ulonglong(start))), C.size_t(5))
headerSz, _, totalSz, err := DecodeMsgpackBinArrayHeader(decoder[:])
if err != nil {
C.ReportErrorToR_NoReturn(C.CString(fmt.Sprintf("ReadMsgpackFrame error trying to decode msgpack frame: %s", err)))
}
if start+totalSz > n {
C.ReportErrorToR_NoReturn(C.CString(fmt.Sprintf("read.msgpack.frame(x, byteOffset) error: byteOffset(%d) plus the frames size(%d) goes beyond the length of x (x has len %d).", start, totalSz, n)))
}
bytes := make([]byte, totalSz)
C.memcpy(unsafe.Pointer(&bytes[0]), unsafe.Pointer(C.get_raw_elt_ptr(rawStream, C.ulonglong(start))), C.size_t(totalSz))
rObject := decodeMsgpackToR(bytes[headerSz:])
C.Rf_protect(rObject)
returnList := C.allocVector(C.VECSXP, C.R_xlen_t(2))
C.Rf_protect(returnList)
C.SET_VECTOR_ELT(returnList, C.R_xlen_t(0), C.Rf_ScalarReal(C.double(float64(start+totalSz))))
C.SET_VECTOR_ELT(returnList, C.R_xlen_t(1), rObject)
C.Rf_unprotect_ptr(rObject)
C.Rf_unprotect_ptr(returnList)
return returnList
}
作者:ZiRo
项目:srndv
// verify a signed message
func CryptoVerify(smsg, pk []byte) bool {
smsg_buff := NewBuffer(smsg)
defer smsg_buff.Free()
pk_buff := NewBuffer(pk)
defer pk_buff.Free()
if pk_buff.size != C.crypto_sign_publickeybytes() {
return false
}
mlen := C.ulonglong(0)
msg := malloc(C.size_t(len(smsg)))
defer msg.Free()
smlen := C.ulonglong(smsg_buff.size)
return C.crypto_sign_open(msg.uchar(), &mlen, smsg_buff.uchar(), smlen, pk_buff.uchar()) != -1
}
作者:Jenil
项目:golang-bb
func waitTimer(cycles int64, f *os.File) int {
// init counters:
C.init_perfcounters(1, 0)
//fmt.Printf("cyles to wait: %d\n", cycles)
timeStart := C.ulonglong(C.get_cyclecount())
timeElapsed := C.ulonglong(0)
for {
timeElapsed = C.ulonglong(C.get_cyclecount()) - timeStart
if timeElapsed > C.ulonglong(cycles) {
writeMessage(f, fmt.Sprintf("%27s", "COMPLETE"))
break
}
}
return int(timeElapsed)
}
作者:4cd
项目:srndv
// encrypts a message to a user given their public key is known
// returns an encrypted box
func CryptoBox(msg, nounce, pk, sk []byte) []byte {
msgbuff := NewBuffer(msg)
defer msgbuff.Free()
// check sizes
if len(pk) != int(C.crypto_box_publickeybytes()) {
log.Println("len(pk) != crypto_box_publickey_bytes")
return nil
}
if len(sk) != int(C.crypto_box_secretkeybytes()) {
log.Println("len(sk) != crypto_box_secretkey_bytes")
return nil
}
if len(nounce) != int(C.crypto_box_macbytes()) {
log.Println("len(nounce) != crypto_box_macbytes()")
return nil
}
pkbuff := NewBuffer(pk)
defer pkbuff.Free()
skbuff := NewBuffer(sk)
defer skbuff.Free()
nouncebuff := NewBuffer(nounce)
defer nouncebuff.Free()
resultbuff := malloc(msgbuff.size + nouncebuff.size)
defer resultbuff.Free()
res := C.crypto_box_easy(resultbuff.uchar(), msgbuff.uchar(), C.ulonglong(msgbuff.size), nouncebuff.uchar(), pkbuff.uchar(), skbuff.uchar())
if res != 0 {
log.Println("crypto_box_easy failed:", res)
return nil
}
return resultbuff.Bytes()
}
作者:sacad
项目:pigosa
// New returns a new Pigosat instance, ready to have literals added to it. The
// error return value need only be checked if the OutputFile option is non-nil.
func New(options *Options) (*Pigosat, error) {
// PicoSAT * picosat_init (void);
p := C.picosat_init()
if options != nil {
// void picosat_set_propagation_limit (PicoSAT *, unsigned long long limit);
C.picosat_set_propagation_limit(p, C.ulonglong(options.PropagationLimit))
if options.OutputFile != nil {
cfile, err := cfdopen(options.OutputFile, "a")
if err != nil {
C.picosat_reset(p)
return nil, &os.PathError{Op: "fdopen",
Path: options.OutputFile.Name(), Err: err}
}
// void picosat_set_output (PicoSAT *, FILE *);
C.picosat_set_output(p, cfile)
}
// void picosat_set_verbosity (PicoSAT *, int new_verbosity_level);
C.picosat_set_verbosity(p, C.int(options.Verbosity))
if options.Prefix != "" {
// void picosat_set_prefix (PicoSAT *, const char *);
prefix := C.CString(options.Prefix)
defer C.free(unsafe.Pointer(prefix))
C.picosat_set_prefix(p, prefix)
}
if options.MeasureAllCalls {
// void picosat_measure_all_calls (PicoSAT *);
C.picosat_measure_all_calls(p)
}
}
pgo := &Pigosat{p: p, lock: sync.RWMutex{}}
runtime.SetFinalizer(pgo, (*Pigosat).delete)
return pgo, nil
}
作者:nimbus-networ
项目:nimbus-deskto
func Random(n int) []byte {
buf := make([]byte, n)
C.randombytes(array(buf), C.ulonglong(n))
return buf
}
作者:ZiRo
项目:srndv
// open an encrypted box
func CryptoBoxOpen(box, nounce, sk, pk []byte) ([]byte, error) {
boxbuff := NewBuffer(box)
defer boxbuff.Free()
// check sizes
if len(pk) != int(C.crypto_box_publickeybytes()) {
err := errors.New("len(pk) != crypto_box_publickey_bytes")
return nil, err
}
if len(sk) != int(C.crypto_box_secretkeybytes()) {
err := errors.New("len(sk) != crypto_box_secretkey_bytes")
return nil, err
}
if len(nounce) != int(C.crypto_box_macbytes()) {
err := errors.New("len(nounce) != crypto_box_macbytes()")
return nil, err
}
pkbuff := NewBuffer(pk)
defer pkbuff.Free()
skbuff := NewBuffer(sk)
defer skbuff.Free()
nouncebuff := NewBuffer(nounce)
defer nouncebuff.Free()
resultbuff := malloc(boxbuff.size - nouncebuff.size)
defer resultbuff.Free()
// decrypt
res := C.crypto_box_open_easy(resultbuff.uchar(), boxbuff.uchar(), C.ulonglong(boxbuff.size), nouncebuff.uchar(), pkbuff.uchar(), skbuff.uchar())
if res != 0 {
return nil, errors.New("crypto_box_open_easy failed")
}
// return result
return resultbuff.Bytes(), nil
}
作者:nimbus-networ
项目:nimbus-deskto
func Hash(message []byte) []byte {
hash := make([]byte, HashSize)
C.crypto_hash(array(hash), array(message), C.ulonglong(len(message)))
return hash
}
作者:jaked12
项目:Si
// takes as input a public key and a signed message
// returns whether the signature is valid or not
func Verify(verificationKey PublicKey, signedMessage SignedMessage) (verified bool, err error) {
// points to unsigned message after verifying
var messagePointer *C.uchar
messageBytes := make([]byte, len(signedMessage.Message)+1)
if len(signedMessage.Message) == 0 {
// must point somewhere valid, but the data won't be altered
// can't point to [0] because the slice is empty
messagePointer = (*C.uchar)(unsafe.Pointer(&messageBytes))
} else {
messagePointer = (*C.uchar)(unsafe.Pointer(&messageBytes[0]))
}
// points to an int so the C function can return the message length after verifying
var messageLen uint64
lenPointer := (*C.ulonglong)(unsafe.Pointer(&messageLen))
// points to the signed message as input for verification
signedMessageBytes := []byte(signedMessage.CombinedMessage())
signedMessagePointer := (*C.uchar)(unsafe.Pointer(&signedMessageBytes[0]))
// length of signed message, but as a C object
signedMessageLen := C.ulonglong(len(signedMessageBytes))
// pointer to the public key used to sign the message
verificationKeyPointer := (*C.uchar)(unsafe.Pointer(&verificationKey[0]))
// verify signature
success := C.crypto_sign_open(messagePointer, lenPointer, signedMessagePointer, signedMessageLen, verificationKeyPointer)
verified = success == 0
return
}
作者:ericcapricor
项目:flyn
func (v *VirStorageVol) Resize(capacity uint64, flags uint32) error {
result := C.virStorageVolResize(v.ptr, C.ulonglong(capacity), C.uint(flags))
if result == -1 {
return GetLastError()
}
return nil
}
作者:reuse
项目:lg
func (lua *Lua) PushGoValue(value reflect.Value) {
switch t := value.Type(); t.Kind() {
case reflect.Bool:
if value.Bool() {
C.lua_pushboolean(lua.State, C.int(1))
} else {
C.lua_pushboolean(lua.State, C.int(0))
}
case reflect.String:
C.lua_pushstring(lua.State, C.CString(value.String()))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
C.lua_pushnumber(lua.State, C.lua_Number(C.longlong(value.Int())))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
C.lua_pushnumber(lua.State, C.lua_Number(C.ulonglong(value.Uint())))
case reflect.Float32, reflect.Float64:
C.lua_pushnumber(lua.State, C.lua_Number(C.double(value.Float())))
case reflect.Slice:
length := value.Len()
C.lua_createtable(lua.State, C.int(length), 0)
for i := 0; i < length; i++ {
C.lua_pushnumber(lua.State, C.lua_Number(i+1))
lua.PushGoValue(value.Index(i))
C.lua_settable(lua.State, -3)
}
case reflect.Interface:
lua.PushGoValue(value.Elem())
case reflect.Ptr, reflect.UnsafePointer:
C.lua_pushlightuserdata(lua.State, unsafe.Pointer(value.Pointer()))
default:
lua.Panic("wrong return value %v %v", value, t.Kind())
}
}
作者:ash
项目:jqrep
// Start will compile `program` and return a three channels: input, output and
// error. Sending a jq.Jv* to input cause the program to be run to it and
// one-or-more results returned as jq.Jv* on the output channel, or one or more
// error values sent to the error channel. When you are done sending values
// close the input channel.
//
// args is a list of key/value pairs to bind as variables into the program, and
// must be an array type even if empty. Each element of the array should be an
// object with a "name" and "value" properties. Name should exclude the "$"
// sign. For example this is `[ {"name": "n", "value": 1 } ]` would then be
// `$n` in the programm.
//
// This function is not reentereant -- in that you cannot and should not call
// Start again until you have closed the previous input channel.
//
// If there is a problem compiling the JQ program then the errors will be
// reported on error channel before any input is read so makle sure you account
// for this case.
//
// Any jq.Jv* values passed to the input channel will be owned by the channel.
// If you want to keep them afterwards ensure you Copy() them before passing to
// the channel
func (jq *Jq) Start(program string, args *Jv) (in chan<- *Jv, out <-chan *Jv, errs <-chan error) {
// Create out two way copy of the channels. We need to be able to recv from
// input, so need to store the original channel
cIn := make(chan *Jv)
cOut := make(chan *Jv)
cErr := make(chan error)
// And assign the read/write only versions to the output fars
in = cIn
out = cOut
errs = cErr
// Before setting up any of the global error handling state, lets check that
// args is of the right type!
if args.Kind() != JV_KIND_ARRAY {
go func() {
// Take ownership of the inputs
for jv := range cIn {
jv.Free()
}
cErr <- fmt.Errorf("`args` parameter is of type %s not array!", args.Kind().String())
args.Free()
close(cOut)
close(cErr)
}()
return
}
jq.errorStoreId = globalErrorChannels.Add(cErr)
// Because we can't pass a function pointer to an exported Go func we have to
// call a C function which uses the exported fund for us.
// https://github.com/golang/go/wiki/cgo#function-variables
C.install_jq_error_cb(jq._state, C.ulonglong(jq.errorStoreId))
go func() {
if jq._Compile(program, args) == false {
// Even if compile failed follow the contract. Read any inputs and take
// ownership of them (aka free them)
//
// Errors from compile will be sent to the error channel
for jv := range cIn {
jv.Free()
}
} else {
for jv := range cIn {
jq._Execute(jv, cOut, cErr)
}
}
// Once we've read all the inputs close the output to signal to caller that
// we are done.
close(cOut)
close(cErr)
C.install_jq_error_cb(jq._state, 0)
}()
return
}
作者:flie
项目:goh
func newPlatformInfo(tune TuneFlag, cpu CpuFeature) *hsPlatformInfo {
var platform C.struct_hs_platform_info
platform.tune = C.uint(tune)
platform.cpu_features = C.ulonglong(cpu)
return &hsPlatformInfo{platform}
}