github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/ext/hash/sha2.go (about)

     1  package hash
     2  
     3  import (
     4  	"crypto"
     5  
     6  	"github.com/ncruces/go-sqlite3"
     7  	"github.com/ncruces/go-sqlite3/internal/util"
     8  )
     9  
    10  func sha224Func(ctx sqlite3.Context, arg ...sqlite3.Value) {
    11  	hashFunc(ctx, arg[0], crypto.SHA224)
    12  }
    13  
    14  func sha384Func(ctx sqlite3.Context, arg ...sqlite3.Value) {
    15  	hashFunc(ctx, arg[0], crypto.SHA384)
    16  }
    17  
    18  func sha256Func(ctx sqlite3.Context, arg ...sqlite3.Value) {
    19  	size := 256
    20  	if len(arg) > 1 {
    21  		size = arg[1].Int()
    22  	}
    23  
    24  	switch size {
    25  	case 224:
    26  		hashFunc(ctx, arg[0], crypto.SHA224)
    27  	case 256:
    28  		hashFunc(ctx, arg[0], crypto.SHA256)
    29  	default:
    30  		ctx.ResultError(util.ErrorString("sha256: size must be 224, 256"))
    31  	}
    32  }
    33  
    34  func sha512Func(ctx sqlite3.Context, arg ...sqlite3.Value) {
    35  	size := 512
    36  	if len(arg) > 1 {
    37  		size = arg[1].Int()
    38  	}
    39  
    40  	switch size {
    41  	case 224:
    42  		hashFunc(ctx, arg[0], crypto.SHA512_224)
    43  	case 256:
    44  		hashFunc(ctx, arg[0], crypto.SHA512_256)
    45  	case 384:
    46  		hashFunc(ctx, arg[0], crypto.SHA384)
    47  	case 512:
    48  		hashFunc(ctx, arg[0], crypto.SHA512)
    49  	default:
    50  		ctx.ResultError(util.ErrorString("sha512: size must be 224, 256, 384, 512"))
    51  	}
    52  
    53  }